import os
import sys
import time

from PySide2 import QtGui, QtWidgets
from PySide2.QtCore import QProcess

import FreeCAD
import FreeCADGui as Gui

import fcpdwb_locator as locator
from . import pdserver
from . import pdtools, pdcontrolertools, pdincludetools, pdrawtools, pdgeometrictools

TRY2EMBED = False

pdProcess = QProcess()
pdServer = pdserver.PureDataServer()

# register message handlers
pdtools.registerToolList(pdServer)
pdcontrolertools.registerToolList(pdServer)
pdincludetools.registerToolList(pdServer)
pdrawtools.registerToolList(pdServer)
pdgeometrictools.registerToolList(pdServer)

userPref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/FCPD")

def pdIsRunning():
    return pdProcess.state() != QProcess.NotRunning

def runPD():
    if not pdIsRunning():
        pdBin = userPref.GetString('pd_path')

        pdArgs = ['-path', os.path.join(locator.PD_PATH, 'pdlib'),
                  '-helppath', os.path.join(locator.PD_PATH, 'pdhelp')]

        if userPref.GetBool('fc_allowRaw', False):
            clientTemplate = "client_raw.pdtemplate"
            pdArgs += ['-path', os.path.join(locator.PD_PATH, 'pdautogen'),
                       '-helppath', os.path.join(locator.PD_PATH, 'pdautogenhelp')]
        else:
            clientTemplate = "client.pdtemplate"

        with open(os.path.join(locator.PD_PATH, clientTemplate), 'r') as f:
            clientContents = f.read()
        clientContents = clientContents.replace('%FCLISTEN%',
                                                str(userPref.GetInt('fc_listenport')))
        clientContents = clientContents.replace('%PDLISTEN%',
                                                str(userPref.GetInt('pd_defaultport')))

        clientFilePath = os.path.join(locator.PD_PATH, 'client.pd')
        with open(clientFilePath, 'w') as f:
            f.write(clientContents)

        pdProcess.startDetached(pdBin, pdArgs + ['-open', clientFilePath])
        if TRY2EMBED:
            time.sleep(1)
            embedPD()

def embedPD():
    '''
    Try to embed the pd window(s) in FreeCAD
    return True in success
    only implemented for PureData and PlugData on Linux platform
    '''
    if not sys.platform.startswith('linux'):
        return False

    exe = userPref.GetString('pd_path').lower()
    if 'plugdata' in exe:
        wName = ['"PlugData"']
    elif ('pd' in exe or 'puredata' in exe):
        wName = ['"Pd"', '"PatchWindow"']
    else:
        return False

    #if not pdIsRunning():
    #    #runPD()
    #    return False

    mw = Gui.getMainWindow()
    mdi = mw.findChild(QtWidgets.QMdiArea)
    isOk = False
    for w in wName:
        cl = f'xwininfo -root -tree | grep \'{w}\' | cut -f9 -d" "'
        print(cl)
        cliP = QProcess()
        cliP.start('bash', ['-c', cl])
        cliP.waitForReadyRead();
        winid, _ = cliP.readAllStandardOutput().toInt(16)
        print(winid)
        if winid:
            win = QtGui.QWindow.fromWinId(winid)
            widget = QtWidgets.QWidget(mdi)
            cont = QtWidgets.QWidget.createWindowContainer(win, widget)
            mdi.addSubWindow(cont)
            cont.show()
            isOk = True
    return isOk


import json
# reading the data from the file
with open('questions.txt') as f:
    #data = f.read()
    questionList = json.load(f)

for x in questionList:
    for soc in x['discussion']:
        print(soc)

print(len(questionList))
from win32api import GetMonitorInfo, MonitorFromPoint

monitor_info = GetMonitorInfo(MonitorFromPoint((0,0)))
work_area = monitor_info.get("Work")
print("The work area size is {}x{}.".format(work_area[2], work_area[3]))


from pytube import YouTube
from playsound import playsound
import tkinter as tk

window_width = 500
window_height = 150
window_title = "YouTube Downloader"
button_click = "clicks.m4a"

class YouTubeDownloader:
    def __init__(self):
        self.window = tk.Tk()
        self.window.geometry("{}x{}".format(window_width, window_height))
        self.window.configure(bg="#a8323a")
        self.window.title(window_title)

        #creation of lables
        self.link_lable = tk.Label(self.window, text = "Download Link")
        self.link_lable.grid(column= 0, row=0)
        self.name_lable = tk.Label(self.window, text= "Save File As")
        self.name_lable.grid(column = 0, row= 1)
        self.path_lable = tk.Label(self.window, text = "Save File Path")
        self.path_lable.grid(column = 0, row = 2)
        self.extension_lable = tk.Label(self.window, text = "File Extension")
        self.extension_lable.grid(column = 0, row = 3)

        #creation of entry
        self.link_entry = tk.Entry(master=self.window, width = 50)
        self.link_entry.grid(column = 1 , row = 0)
        self.name_entry = tk.Entry(master=self.window, width = 50)
        self.name_entry.grid(column =1, row= 1)
        self.path_entry = tk.Entry(master= self.window, width = 50)
        self.path_entry.grid(column = 1, row=2)
        self.extension_entry = tk.Entry(master=self.window, width = 50)
        self.extension_entry.grid(column = 1, row=3)

        #creation of button
        self.download_button = tk.Button(self.window, text = "Download", command = self.__getLink)
        self.download_button.grid(column= 1, row = 4)

        return

    def __downloader(self, link, save_path = "", save_name = "", extension ="1080p"):
        yt = YouTube(link)
        yt_stream  = yt.streams.filter(progressive=True, file_extension = extension).order_by("resolution").desc().first()
        yt_stream.download(output_path = save_path, filename = save_name)

        return

    def __getLink(self):
        #playsound(button_click)
        link = self.link_entry.get()
        name = self.name_entry.get()
        path = self.path_entry.get()
        extension = self.extension_entry.get()

        self.__downloader(link, path, name, extension)

        return

    def run_app(self):
        self.window.mainloop()
        return

if __name__ == "__main__":
    app = YouTubeDownloader()
    app.run_app()


# -*- coding: utf-8 -*-
"""
Created on Thu Dec  8 10:04:10 2016

@author: zhuchunwu
"""

import requests

def getliofP():
    apiurl='http://tpv.daxiangdaili.com/ip/?tid=557576382820459&num=20&filter=on'
    api =requests.get(apiurl)
    api = str(api.text)
    lis1 = api.splitlines()
    return(lis1)

def checkP(lis1):
    checklis = []
    checkurl = 'http://whois.ipcn.org/'
    for i in lis1:
        proxies = {'http':'http://%s' %i}
        try:
            requests.get(checkurl,proxies = proxies)
            checklis.append(proxies)
            print(proxies)
        except Exception:
            print(i)
            pass
#        print(proxies)
    print(checklis)

lis1 = getliofP()
checkP(lis1)


import plotly.graph_objects as go

from gd_analysis import helpers

from gd_analysis.analysis import (
    get_players_goal_differences,
    goal_difference_for_team,
)
from gd_analysis.data import (
    filter_competition,
    filter_season,
    filter_team_url,
    filter_player_url,
    filter_appearances,
)

TITLE_FONT = {"size": 34, "color": "white"}
LABEL_FONT = {"size": 28, "color": "white"}
TICK_FONT = {"size": 18, "color": "white"}
LEGEND_FONT = {"size": 20, "color": "white"}
PLAYER_TEXT_FONT = {"size": 18, "color": "white"}
ANNOTATION_TEXT_FONT = {"size": 18, "color": "white"}
DEFAULT_HEIGHT = 500
DEFAULT_WIDTH = 800
PLOT_BACKGROUND_COLOR = "rgba(35, 35, 35, 1)"

EMPTY_LAYOUT = go.Layout(
    height=DEFAULT_HEIGHT,
    paper_bgcolor=PLOT_BACKGROUND_COLOR,
    plot_bgcolor=PLOT_BACKGROUND_COLOR,
    xaxis={"showgrid": False},
    yaxis={"showgrid": False},
)

def get_default_layout(
    title,
    x_axis_title,
    y_axis_title,
    height=DEFAULT_HEIGHT,
    width=DEFAULT_WIDTH,
):
    layout = go.Layout(
        height=height,
        width=width,
        title={"text": f"{title}", "font": TITLE_FONT},
        paper_bgcolor=PLOT_BACKGROUND_COLOR,
        plot_bgcolor=PLOT_BACKGROUND_COLOR,
        xaxis={
            "title": f"{x_axis_title}",
            "showgrid": True,
            "zeroline": True,
            "ticks": "outside",
            "tickwidth": 1.5,
            "tickfont": TICK_FONT,
            "tickcolor": "white",
            "gridwidth": 0.5,
            "gridcolor": "grey",
            "title_font": LABEL_FONT,
        },
        yaxis={
            "title": f"{y_axis_title}",
            "showgrid": False,
            "zeroline": True,
            "ticks": "outside",
            "tickwidth": 1.5,
            "tickfont": TICK_FONT,
            "tickcolor": "white",
            "title_font": LABEL_FONT,
            "showline": True,
            "linewidth": 2,
            "linecolor": "white",
            "gridwidth": 0.5,
            "gridcolor": "grey",
        },
        legend=go.layout.Legend(
            x=0.1, y=1.07, font=LEGEND_FONT, orientation="h"
        ),
    )
    return layout

def get_scatter_for_df(
    df,
    x_column,
    y_column,
    name,
    marker_color="lightslategrey",
    line_color="white",
    marker_size=10,
    marker_symbol="circle",
):
    x_values = df[x_column].values
    y_values = df[y_column].values
    teams = df.index.get_level_values(0)
    players = df.index.get_level_values(2)

    return go.Scatter(
        x=x_values,
        y=y_values,
        name=name,
        mode="markers",
        marker={
            "color": marker_color,
            "size": marker_size,
            "line": {"width": 0, "color": line_color},
            "symbol": marker_symbol,
        },
        hovertext=[
            f"{team}
{player}
{x_column}={x:.1f}
{y_column}={y:.1f}"
            for team, player, x, y in zip(teams, players, x_values, y_values)
        ],
        hoverinfo="text",
    )

def scatter_players_for_season(
    df_players,
    df_matches,
    competition: str,
    season: str,
    x_column: str,
    y_column: str,
    min_appearances: int = 5,
    team: "str" = None,
):
    df_players = filter_competition(df_players, competition)
    df_players = filter_season(df_players, season)

    df_grouped = get_players_goal_differences(df_players)
    df_grouped = filter_appearances(df_grouped, min_appearances)

    teams = df_grouped.index.get_level_values(0)
    players = df_grouped.index.get_level_values(2)

    data = []

    if team is not None:
        team_name = helpers.get_map_from_url_to_name(df_players, "team")[team]
        trace_team = get_scatter_for_df(
            df_grouped.loc[[team]],
            x_column,
            y_column,
            name=team_name,
            marker_color="mediumvioletred",
            marker_symbol="diamond",
        )
        data.append(trace_team)
        df_grouped = df_grouped.drop(index=team)

    trace_all = get_scatter_for_df(
        df_grouped, x_column, y_column, name="All / other teams"
    )
    data.append(trace_all)

    layout = get_default_layout(f"{competition} {season}", x_column, y_column)
    layout.yaxis.update(rangemode="tozero")

    if team is not None:
        # get mean value for team
        df_matches = filter_competition(df_matches, competition)
        df_matches = filter_season(df_matches, season)
        team_goal_difference = goal_difference_for_team(df_matches, team)

        # for goal difference of team
        shapes = [
            go.layout.Shape(
                type="line",
                yref="paper",
                x0=team_goal_difference,
                y0=0,
                x1=team_goal_difference,
                y1=1,
                line=dict(color="mediumvioletred", width=4, dash="dash",),
            )
        ]
        layout.update(shapes=shapes)

    #  fig.update_xaxes(ticks="outside", tickwidth=2, tickcolor='crimson', ticklen=10)
    #  fig.update_yaxes(ticks="outside", tickwidth=2, tickcolor='crimson', ticklen=10, col=1)

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

    return fig

def scatter_players_for_team(
    df_players,
    df_matches,
    competition,
    season,
    team,
    x_column="gd90",
    y_column="full_games",
    min_appearances: int = 5,
):
    column = "gd90"
    df_players = filter_competition(df_players, competition)
    df_players = filter_season(df_players, season)
    df_players = filter_team_url(df_players, team)
    team_name = helpers.get_map_from_url_to_name(df_players, "team")[team]

    df_players = get_players_goal_differences(df_players)
    df_players = filter_appearances(df_players, min_appearances)

    # display(df.head())

    # get mean value for team
    df_matches = filter_competition(df_matches, competition)
    df_matches = filter_season(df_matches, season)
    team_goal_difference = goal_difference_for_team(df_matches, team)

    # df_team = df_grouped[df_grouped.index.get_level_values(0) == team]

    player_names = df_players.index.get_level_values(2)

    trace = go.Scatter(
        x=df_players[x_column],
        y=df_players[y_column],
        mode="markers+text",
        name="Players",
        text=player_names,
        textposition="top center",
        marker={"color": "mediumvioletred", "size": 12, "symbol": "diamond",},
        textfont=PLAYER_TEXT_FONT,
    )

    # get mean value for team
    df_matches = filter_competition(df_matches, competition)
    df_matches = filter_season(df_matches, season)
    team_goal_difference = goal_difference_for_team(df_matches, team)

    # for goal difference of team
    shapes = [
        go.layout.Shape(
            type="line",
            yref="paper",
            x0=team_goal_difference,
            y0=0,
            x1=team_goal_difference,
            y1=1,
            line=dict(color="mediumvioletred", width=3, dash="dash",),
        )
    ]
    layout = get_default_layout(f"{team_name} {season}", x_column, y_column)
    layout.yaxis.update(rangemode="tozero")
    layout.update(shapes=shapes)

    fig = go.Figure([trace], layout=layout)

    return fig

def bar_players_for_team(
    df_players,
    df_matches,
    competition,
    season,
    team,
    column="gd90",
    weight_column="full_games",
    min_appearances: int = 5,
):
    df_players = filter_competition(df_players, competition)
    df_players = filter_season(df_players, season)
    df_players = filter_team_url(df_players, team)
    team_name = helpers.get_map_from_url_to_name(df_players, "team")[team]

    df_players = get_players_goal_differences(df_players)
    df_players = filter_appearances(df_players, min_appearances)

    df_players = df_players.sort_values(column)

    values = df_players[column]
    players = df_players.index.get_level_values(2)
    width_weights = df_players[weight_column]

    # get mean value for team
    df_matches = filter_competition(df_matches, competition)
    df_matches = filter_season(df_matches, season)
    team_goal_difference = goal_difference_for_team(df_matches, team)

    trace = go.Bar(
        x=players,
        y=df_players[column],
        width=0.05 + 0.7 * ((width_weights / width_weights.max())),
        orientation="v",
        hoverinfo="text",
        hovertext=[
            f"{player}
{column}={value:.1f}
{weight_column}={weight:.1f}"
            for player, value, weight in zip(players, values, width_weights)
        ],
        marker={"color": "mediumvioletred", "line": {"width": 0}},
    )

    layout = get_default_layout(f"{team_name} {season}", "Player", column)
    annotations = [
        go.layout.Annotation(
            x=-0.1,
            y=0.95,
            xref="paper",
            xanchor="left",
            yanchor="middle",
            yref="paper",
            text="Bar width scaled with minutes played",
            font=ANNOTATION_TEXT_FONT,
        )
    ]

    shapes = [
        go.layout.Shape(
            type="line",
            xref="paper",
            x0=0,
            y0=team_goal_difference,
            x1=1,
            y1=team_goal_difference,
            line=dict(color="mediumvioletred", width=4, dash="dash",),
        )
    ]

    layout.update(annotations=annotations, shapes=shapes)
    layout.xaxis.update(showgrid=False)
    layout.yaxis.update(showgrid=True)

    data = [trace]

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

    return fig


import cv2
import sys
from PIL import Image
import numpy as np
import csv
import shortuuid
import os
import glob
import re
import ntpath

ROOT_PATH = 'C:\\dev\\courses\\2.131 - Advanced Instrumentation\\'
# FILE_NAME = 'Ecoli-HangingDrop-20x-Dilution-1.MOV'
FILE_NAME = 'Ecoli-Slide-Coverslip.MOV'
# FILE_NAME = 'Rhodosprillum-HangingDrop-3ul.MOV'
# FILE_NAME = 'HangingDrop-100ul-Nutrient-25ul-Ecoli-lRhodo-(50-50).MOV'
# FILE_NAME = 'Perturbation-Test1-PCB-Slide.MOV'

tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']
WIN_NAME = "2.131 Bacterial Tracker"
POS_TRACKBAR = "FRAME"

class TrackerUI(object):

    def __init__(self, tracker_type='MIL', duration_sec=10.0):
        self._filename = FILE_NAME
        self._root_path = ROOT_PATH
        self._tracker_type = tracker_type
        self._cap = cv2.VideoCapture(ROOT_PATH + FILE_NAME)
        self._cap_fps = self._cap.get(cv2.CAP_PROP_FPS)
        self._save_video = True
        self._duration_sec=duration_sec
        self._duration_frames = int(self._cap_fps * self._duration_sec)
        self._is_tracking = False
        self._is_playing = False
        self.load_existing_trackers()
        self.setup_ui()

    def get_tracker(self, tracker_type):
        if tracker_type == 'BOOSTING':
            return cv2.TrackerBoosting_create()
        if tracker_type == 'MIL':
            return cv2.TrackerMIL_create()
        if tracker_type == 'KCF':
            return cv2.TrackerKCF_create()
        if tracker_type == 'TLD':
            return cv2.TrackerTLD_create()
        if tracker_type == 'MEDIANFLOW':
            return cv2.TrackerMedianFlow_create()
        if tracker_type == 'GOTURN':
            return cv2.TrackerGOTURN_create()

    def load_existing_trackers(self):
        self._existing_trackers = {}
        self._trackers_by_id = {}
        self._loaded_trackers = {}
        # self._live_trackers = {}
        tracker_csvs = glob.glob(ROOT_PATH + FILE_NAME + ".*.csv")
        for f in tracker_csvs:
            # parse out key info
            m = re.match('.*\.(\w+)\.(\d+)\.(\d+)\.(\d+)\.csv', ntpath.basename(f))
            if m:
                id = m.group(1)
                w = int(m.group(2))
                h = int(m.group(3))
                start = int(m.group(4))
                # key them by their start frame
                if start in self._existing_trackers:
                    self._existing_trackers[start].append((id,f,w,h))
                else:
                    self._existing_trackers[start] = [(id, f, w, h)]
                self._trackers_by_id[id] = (start, w, h)

    def seek_callback(self, x):
        if self._is_tracking or self._is_playing:
            return
        i = cv2.getTrackbarPos(POS_TRACKBAR, WIN_NAME)
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, i-1)
        self.next_frame()

    def skip_frame_generator(self, df):
        cf = self._cap.get(cv2.CAP_PROP_POS_FRAMES) - 1
        self._cap.set(cv2.CAP_PROP_POS_FRAMES, cf + df)
        cv2.setTrackbarPos(POS_TRACKBAR, WIN_NAME,
            int(self._cap.get(cv2.CAP_PROP_POS_FRAMES)))
        _, frame = self._cap.read()
        self._current_frame = frame
        cv2.imshow(WIN_NAME, frame)

    def next_frame(self):
        ret, frame = self._cap.read()
        self._current_frame = frame

        cv2.putText(frame, "2.131 Bacterial Tracker", (100, 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);

        # add any existing trackers
        current_frame = int(self._cap.get(cv2.CAP_PROP_POS_FRAMES))
        # load a live
        valid_trackers = [s for s in self._existing_trackers.keys()
                          if s <= current_frame and
                          s + self._duration_frames > current_frame-1]

        for t in valid_trackers:
            for (id, f, w, h) in self._existing_trackers[t]:
                if id not in self._loaded_trackers:
                    # load the csv
                    with open(f, 'r') as my_file:
                        reader = csv.reader(my_file)
                        self._loaded_trackers[id] = list(reader)
                # now draw the appropriate rect for this tracker
                try:
                    line = self._loaded_trackers[id][current_frame - t + 1]
                    p1 = (int(int(line[3]) - w/2.), int(int(line[4]) - h/2.))
                    p2 = (int(int(line[3]) + w/2.), int(int(line[4]) + h/2.))
                    cv2.rectangle(frame, p1, p2, (255, 128, 128), 2, 1)
                    p1x, p1y = p1
                    cv2.putText(frame, id[:3], (p1x,p1y-20),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
                except Exception as e:
                    print(e)
                    print("Id: {0}, Start Frame: {1} Current Frame: {2}, Total: {3}".format(
                        id, t, current_frame, len(self._loaded_trackers[id])
                    ))
        cv2.imshow(WIN_NAME, frame)
        return frame

    def play_pause(self):
        self.is_playing = True
        while True:
            self.next_frame()
            cv2.setTrackbarPos(POS_TRACKBAR, WIN_NAME,
                int(self._cap.get(cv2.CAP_PROP_POS_FRAMES)))
            key = cv2.waitKey(10) & 0xFF
            if key == ord(" "):
                break
        self.is_playing = False

    def setup_ui(self):
        cv2.namedWindow(WIN_NAME)
        cv2.createTrackbar(
            POS_TRACKBAR, WIN_NAME, 0,
            int(self._cap.get(cv2.CAP_PROP_FRAME_COUNT)), self.seek_callback)
        # cv2.createButton("Play", self.play, buttonType=cv2.QT_PUSH_BUTTON)
        self.next_frame()

        actions = dict()
        actions[ord("\n")] = lambda: self.track_trajectory()
        actions[ord("\r")] = lambda: self.track_trajectory()
        actions[ord(" ")] = lambda: self.play_pause()
        # actions[ord("D")] = lambda: self.skip_frame_generator(10)
        # actions[ord("d")] = lambda: self.skip_frame_generator(1)
        # actions[ord("a")] = lambda: self.skip_frame_generator(-1)
        # actions[ord("A")] = lambda: self.skip_frame_generator(-10)
        actions[ord("q")] = lambda: exit(0)

        while True:
            key = cv2.waitKey(0) & 0xFF
            def dummy():
                pass
            actions.get(key, dummy)()

    def track_trajectory(self):
        self._is_tracking = True

        id = shortuuid.uuid()

        cap = self._cap

        # MIL appears to work best...
        tracker = self.get_tracker(self._tracker_type)

        cap_fps = cap.get(cv2.CAP_PROP_FPS)
        current_frame =  int(cap.get(cv2.CAP_PROP_POS_FRAMES)) # int(cap_fps * start_sec)
        duration_frames = int(cap_fps * self._duration_sec)

        # cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
        time = (current_frame-1) * cap_fps

        resw = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        resh = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
        res = (resw, resh)

        frame = self._current_frame
        cv2.putText(frame, "SET MARK", (100, 150),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
        cv2.imshow(WIN_NAME, frame)

        if self._save_video:

            fourcc = cv2.VideoWriter_fourcc(*'DIVX')
            out_fname = self._root_path + self._filename + \
                        '.multi_track_{0}.avi'.format(id)
            out = cv2.VideoWriter(out_fname, fourcc, cap_fps, res)

        # Define an initial bounding box
        # bbox = (287, 23, 86, 320)

        bbox = cv2.selectROI(WIN_NAME, frame, True)
        ok = tracker.init(frame, bbox)

        # Uncomment the line below to select a different bounding box
        # selector_frame = frame.copy()

        csv_fname = self._root_path + self._filename + \
                    '.{0}.{1}.{2}.{3}.csv'.format(id, bbox[2], bbox[3], current_frame)
        csvfile = open(csv_fname, 'w', newline='')
        bac_writer = csv.writer(csvfile, delimiter=',',
            quotechar='|', quoting=csv.QUOTE_MINIMAL)

        bac_writer.writerow(['BacteriaId', 'Frame', 'Time (s)', 'X', 'Y'])

        invalid = False

        while cap.isOpened() and current_frame < duration_frames:
            # ret, frame = cap.read()
            frame = self.next_frame()

            # Start timer
            timer = cv2.getTickCount()

            # Update tracker
            ok, bbox = tracker.update(frame)

            # Calculate Frames per second (FPS)
            fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

            # Draw bounding box
            if ok:
                p1 = (int(bbox[0]), int(bbox[1]))
                p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
                cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1)
                bac_writer.writerow([id, current_frame, "{:.3f}".format(time), int(bbox[0] + bbox[2]/2),
                                     int(bbox[1] + bbox[3]/2)])
            else:
                # Tracking failure
                cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

            if self._save_video:
                # Display tracker type on frame
                # cv2.putText(frame, "2.131 Bacterial Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
                # Display FPS on frame
                cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
                out.write(frame)

            # Display result
            cv2.imshow(WIN_NAME, frame)
            cv2.setTrackbarPos(POS_TRACKBAR, WIN_NAME,
                int(self._cap.get(cv2.CAP_PROP_POS_FRAMES)))

            current_frame = current_frame + 1
            time = time + 1.0/cap_fps

            # stop recording if ESC or space pressed
            k = cv2.waitKey(1) & 0xff
            if k == 27 or k == ord(" "):
                invalid = True
                break

        csvfile.close()
        if self._save_video:
            out.release()

        if invalid:
            cv2.putText(frame, "CANCELLED", (100, 80),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.75,  (0, 0, 255), 2);
            cv2.imshow(WIN_NAME, frame)
            os.remove(csv_fname)
            os.remove(out_fname)
            cv2.imshow(WIN_NAME, frame)

        self._current_frame = frame
        self._is_tracking = False
        self.load_existing_trackers()

# Goal - put in start_time seconds, get a screen

#tracker_UI = TrackerUI(Ecoli-Slide-Coverslip.MOV')
tracker_UI = TrackerUI()

# tracker_UI.track_trajectory()

# cap = cv2.VideoCapture(root_path + 'Ecoli-Slide-Coverslip.MOV')
# ret, frame = cap.read()
# mser_multi(frame)


from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, status
from day.models import Day
from .models import Student

class GetDayAttendencesView(APIView):
    permission_classes = (permissions.AllowAny, )

    def get(self, request, daySlug, format=None):
        try:
            day_slug = daySlug
        except:
            return Response(
                {'error': 'Day ID must be an integer'},
                status=status.HTTP_404_NOT_FOUND
            )

        try:
            if not Day.objects.filter(slug=day_slug).exists():
                return Response(
                    {'error': 'This day does not exist'},
                    status=status.HTTP_404_NOT_FOUND
                )

            day = Day.objects.get(slug=day_slug)

            results = []

            if Student.objects.filter(day=day).exists():
                students = Student.objects.order_by(
                    '-date_created'
                ).filter(day=day)

                for student in students:
                    item = {}

                    item['id'] = student.id
                    item['comment'] = student.comment
                    item['date_created'] = student.date_created
                    item['user'] = student.user.username
                    item['is_present'] = student.is_present

                    results.append(item)

            return Response(
                {'students': results},
                status=status.HTTP_200_OK
            )
        except:
            return Response(
                {'error': 'Something went wrong when retrieving comments'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

class GetDayAttendenceView(APIView):
    def get(self, request, daySlug, format=None):
        user = self.request.user

        try:
            day_slug = daySlug
        except:
            return Response(
                {'error': 'day ID must be an integer'},
                status=status.HTTP_404_NOT_FOUND
            )

        try:
            if not Day.objects.filter(slug=day_slug).exists():
                return Response(
                    {'error': 'This day does not exist'},
                    status=status.HTTP_404_NOT_FOUND
                )

            day = Day.objects.get(slug=day_slug)

            result = {}

            if Student.objects.filter(user=user, day=day).exists():
                student = Student.objects.get(user=user, day=day)

                result['id'] = student.id
                result['comment'] = student.comment
                result['date_created'] = student.date_created
                result['user'] = student.user.username
                result['is_present'] = student.is_present

            return Response(
                {'student': result},
                status=status.HTTP_200_OK
            )
        except:
            return Response(
                {'error': 'Something went wrong when retrieving comment'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )

class CreateAttendenceView(APIView):
    def post(self, request, daySlug, format=None):
        user = self.request.user
        data = self.request.data

        try:
            day_slug = daySlug
        except:
            return Response(
                {'error': 'day ID must be an integer'},
                status=status.HTTP_404_NOT_FOUND
            )

        try:
            comment = str(data['comment'])
        except:
            return Response(
                {'error': 'Must pass a comment when creating comment'},
                status=status.HTTP_400_BAD_REQUEST
            )

        try:
            if not Day.objects.filter(slug=day_slug).exists():
                return Response(
                    {'error': 'This  does not exist'},
                    status=status.HTTP_404_NOT_FOUND
                )

            day = Day.objects.get(slug=day_slug)

            result = {}
            results = []

            # if Student.objects.filter(user=user, day=day).exists():
            #     return Response(
            #         {'error': 'day for this  already created'},
            #         status=status.HTTP_409_CONFLICT
            #     )

            student = Student.objects.create(
                user=user,
                day=day,
                comment=comment,
                is_present=False
            )

            if Student.objects.filter(user=user, day=day).exists():
                result['id'] = comment.id
                result['comment'] = comment.comment
                result['date_created'] = comment.date_created
                result['user'] = comment.user.first_name
                result['is_present'] = comment.is_present

                students = Student.objects.order_by('-date_created').filter(
                    day=day
                )

                for student in students:
                    item = {}

                    item['id'] = student.id
                    item['comment'] = student.comment
                    item['date_created'] = student.date_created
                    item['user'] = student.user.username

                    results.append(item)

            return Response(
                {'student': result, 'students': results},
                status=status.HTTP_201_CREATED
            )
        except:
            return Response(
                {'error': 'Something went wrong when creating comment'},
                status=status.HTTP_500_INTERNAL_SERVER_ERROR
            )


#!/usr/bin/env python

import matplotlib.pyplot as plt
import rospy
from geometry_msgs.msg import PoseArray
from matplotlib.animation import FuncAnimation
from nav_msgs.msg import Odometry

# Dynamic trajectory visualizer
class Visualizer:
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.real_ln, self.ln = plt.plot([], [], 'b', [], [], 'r')
        self.ln.set_label('real')
        self.real_ln.set_label('reference')
        self.ax.legend()
        self.x_data, self.y_data = [], []
        self.real_x, self.real_y = [], []

    def plot_init(self):
        self.ax.set_xlim(0, 6)
        self.ax.set_ylim(0, 3)
        return self.real_ln, self.ln

    def set_trajectory(self, msg):
        self.real_x = [p.position.x for p in msg.poses]
        self.real_y = [p.position.y for p in msg.poses]

    def odom_callback(self, msg):
        self.x_data.append(msg.pose.pose.position.x + 1)
        self.y_data.append(msg.pose.pose.position.y + 1)

    def update_plot(self, frame):
        self.real_ln.set_data(self.real_x, self.real_y)
        self.ln.set_data(self.x_data, self.y_data)
        return self.real_ln, self.ln

# Plot trajectory in real time
if __name__ == '__main__':
    rospy.init_node('plot_trajectory')
    vis = Visualizer()
    path_sub = rospy.Subscriber('/trajectory', PoseArray, vis.set_trajectory)
    ani = FuncAnimation(vis.fig, vis.update_plot, init_func=vis.plot_init, blit=True)
    rospy.sleep(3)
    odom_sub = rospy.Subscriber('/odom', Odometry, vis.odom_callback)
    plt.show(block=True)


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

# # ml lab5

# In[5]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# ### 0. load dataset

# In[6]:

get_ipython().system("chmod +x ./download_data.sh ")
get_ipython().system("./download_data.sh")

# ### 1. train / test split

# In[7]:

import os
import glob

# In[8]:

image_shape = (128, 128, 3)

DATASET_PATH = "./data/dogs-vs-cats/train"

def get_label(name):
    label = "dog" if "dog" in name else "cat"
    return [name, label]

data_df = pd.DataFrame(
    data=[
        get_label(os.path.basename(image_path))
        for image_path in glob.glob(f"{DATASET_PATH}/*.jpg")
    ],
    columns=["name", "label"],
)
data_df.head()

# In[23]:

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split

train_df, test_df = train_test_split(data_df, test_size=0.3, random_state=42)

train_df = train_df.reset_index(drop=True)
test_df = test_df.reset_index(drop=True)

def generate_data(datagen, df, batch_size=64):
    return datagen.flow_from_dataframe(
        df,
        DATASET_PATH,
        x_col="name",
        y_col="label",
        target_size=image_shape[:-1],
        class_mode="binary",
        batch_size=batch_size,
    )

datagen = ImageDataGenerator(rescale=1.0 / 255)
train_generator = generate_data(datagen, train_df)
test_generator = generate_data(datagen, test_df)

# In[10]:

from random import randrange

fig, axs = plt.subplots(1, 10, figsize=(20, 2))
for ax in axs:
    i = randrange(train_df.shape[0])
    ax.imshow(plt.imread(f'{DATASET_PATH}/{train_df["name"][i]}'))
    ax.set_title(train_df["label"][i])
    ax.axis("off")

# ### 2. build network

# In[11]:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (
    Conv2D,
    MaxPooling2D,
    Dense,
    Activation,
    BatchNormalization,
    Dropout,
    Flatten,
)

def build_model():
    model = Sequential()

    model.add(Conv2D(32, 3, padding="same", input_shape=image_shape))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.25))

    model.add(Conv2D(64, 3, activation="relu"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.25))

    model.add(Conv2D(128, 3, activation="relu"))
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=2))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation="relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))
    model.add(Dense(1, activation="sigmoid"))

    return model

# In[7]:

model = build_model()
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()

# In[ ]:

from tensorflow.keras.callbacks import EarlyStopping

history = model.fit(
    train_generator,
    validation_data=test_generator,
    epochs=10,
    workers=4,
    callbacks=[EarlyStopping(patience=5)],
)

# In[20]:

print(f'Train accuracy: {history.history["accuracy"][-1]*100:.2f}%')

# In[28]:

def plot(_history):
    plt.figure(figsize=(8, 6))
    plt.plot(_history.history["accuracy"], "r")
    plt.plot(_history.history["val_accuracy"], "b")
    plt.legend(["Training", "Validation"])
    plt.xlabel("Epochs")
    plt.ylabel("Accuracy")

# In[38]:

plot(history)

# ### 2. data augmentation

# In[40]:

datagen = ImageDataGenerator(
    rotation_range=15,
    rescale=1.0 / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode="nearest",
    width_shift_range=0.1,
    height_shift_range=0.1,
)
train_generator = generate_data(datagen, train_df)

datagen = ImageDataGenerator(rescale=1.0 / 255)
test_generator = generate_data(datagen, test_df)

# In[41]:

model = build_model()
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

# In[46]:

history = model.fit(
    train_generator,
    epochs=10,
    validation_data=test_generator,
    workers=4,
    callbacks=[EarlyStopping(patience=5)],
)

# In[49]:

print(f'Train accuracy: {history.history["accuracy"][-1]*100:.2f}%')

# In[48]:

plot(history)

# > augmentation prevents overfitting

# ### 4. Try VGG16

# In[21]:

from tensorflow.keras import optimizers
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D

image_shape = (224, 224, 3)

pre_trained_model = VGG16(
    input_shape=image_shape, include_top=False, weights="imagenet"
)

for i, layer in enumerate(pre_trained_model.layers):
    layer.trainable = i > 20

last_layer = pre_trained_model.get_layer("block5_pool")
last_output = last_layer.output

x = GlobalAveragePooling2D()(last_output)
x = Dense(512, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1, activation="sigmoid")(x)

model = Model(pre_trained_model.input, x)

model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

model.summary()

# In[24]:

datagen = ImageDataGenerator(
    rotation_range=15,
    rescale=1.0 / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode="nearest",
    width_shift_range=0.1,
    height_shift_range=0.1,
)
train_generator = generate_data(datagen, train_df)

datagen = ImageDataGenerator(rescale=1.0 / 255)
test_generator = generate_data(datagen, test_df)

# In[25]:

history = model.fit(
    train_generator,
    epochs=1,
    validation_data=test_generator,
    workers=4,
    callbacks=[EarlyStopping(patience=5)],
)

# In[34]:

print(f'Train accuracy: {history.history["accuracy"][-1]*100:.2f}%')

# In[35]:

results = model.evaluate(test_generator)
print(f"Test accuracy: {results[-1]*100:.2f}%")


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

"""Utility functions."""

import gc
import socket
from typing import Dict, Optional

from pyppeteer.chromium_downloader import check_chromium, chromium_executable
from pyppeteer.chromium_downloader import download_chromium

__all__ = [
    'check_chromium',
    'chromium_executable',
    'download_chromium',
    'get_free_port',
    'merge_dict',
]

def get_free_port() -> int:
    """Get free port."""
    sock = socket.socket()
    sock.bind(('localhost', 0))
    port = sock.getsockname()[1]
    sock.close()
    del sock
    gc.collect()
    return port

def merge_dict(dict1: Optional[Dict], dict2: Optional[Dict]) -> Dict:
    """Merge two dictionaries into new one."""
    new_dict = {}
    if dict1:
        new_dict.update(dict1)
    if dict2:
        new_dict.update(dict2)
    return new_dict


'''
This file defines the basic Daisy object.

It should only contain the self-contained Daisy model, that ignores the other
Daisies in the world connected via other metrics.
'''
# Imports
import numpy as np
import constants as c
import pdb

# Class
class Daisy(object):
    '''
    This contains the Daisy class (edit this later!!!)
    '''

    def __init__(self, P, gamma, a_vec, A_vec, L=1.0, T_vec=None,
                 verbose=False, A_g=0.5, S=c.S, q=c.q, effs=None):
        '''
        Initializes the Daisy class. All parameters except gamma (a constant)
        are the initial conditions that will change as the daisies are
        integrated in time using Daisy().solve().

        Args:
            + P (float): the fertile area the daisies have to grow.
            + gamma (float): the death rate of the daisies in daisies/day.
            + a_vec (array): the numpy 1D array containing the fractional area
                covered by each desired species of daisy
            + A_vec (array): the numpy 1D array containing the albedos of each
                daisy species in a_vec
            + L (float): the fraction of solar flux incident on the surface
                (where S = 9.17e5 erg/cm^2/s is the solar flux at Earth's
                surface.
            + T_vec (array or None): the numpy 1D array of optimal temperatures
                for each daise species. If None (default) uses the default
                optimal daisy temperature from Watson & Lovelock 1983.
            + verbose (bool): if True, prints out A LOT. If False (default) it
                will not do that.
            + A_g (float): albedo of the ground for the fraction of land not
                covered by daisies. Default 0.5
            + S (float or arr): The solar surface flux in erg/cm^2/s (default
                c.S, from the constants file). If this is an array, effs must
                be an array with the efficiency ranges of the daisies or this
                will be taken as a broad-band flux, and the array should
                contain corresponding wavelengths in Angstroms.
            + q (float or arr): The absorption efficiency of the daisies and
                ground (default c.q).
            + effs (arr or None (Default)): the effs array is used to calculate
                specific daisy color albedos to determine the absorption and
                reflectance of specific daisy species. Should be an array of
                shape (N_daisies, 2) where N_daisies is the number fo daisies
                and each row contains the reflecting wavelengths of the
                daisies.
        '''
        # Make all inputs other than self attributes
        self.P = P
        self.gamma = gamma
        self.in_a_vec = a_vec  # The input vector, to be modified
        self.in_A_vec = A_vec  # The input vector, to be modified
        self.T_vec = T_vec     # The input vector, to be modified
        self.L = L
        self.verbose = verbose
        self.S = S
        self.q = q

        # Create the full arrays, including the ground and its albedo
        self.a_vec = np.array([ai for ai in (a_vec.tolist() +
                                             [P-np.sum(a_vec)])])
        self.A_vec = np.array([Ai for Ai in (A_vec.tolist() + [A_g])])

        if not isinstance(self.S, (float, int)):
            # Then it should be an array, or throw an error
            self.convert_spectrum()

        # Initialize other vectors
        self.T = np.zeros_like(self.a_vec)

        # Check for input temperatures
        if T_vec is None:
            self.T_vec = 295.65
        else:
            # Adding in the ground
            self.T_vec = np.asarray(self.T_vec.tolist() + [295.65])

        # Initialize the other parameters
        self._update()

    def convert_spectrum(self):
        '''
        Converts the spectrum to be just the visible spectrum, since that's the
        only part of the spectrum involved in reflection & absorption by life
        and the surface of Earth that drive surface heating.
        '''
        # Separate the wavelengths and the fluxes
        wl, flux = self.S[:, 0], self.S[:, 1]

        # Get the total flux over wavelength and use that as self.S, save the
        # original as self.S_in
        self.S_in = np.copy(self.S)
        self.S = np.trapz(flux, x=wl)

        # If effs is defined, use that to calculate a new array self.S with the
        # flux absorbed by each species of daisy. If not, the use the fully
        # integrated spectrum and leave it at that.
        if 'effs' in self.__dict__:
            # self.effs exist, use that.
            pass

        # If this is done, finished (returns nothing since it should only be
        # called in self.__init__().
        return

    def _update(self, a_vec=None):
        '''Updates daisy parameters in the correct order'''
        self.daisy_check()
        self.emptySpace(a_vec)
        self.albedo(a_vec)
        self.temperature()
        self.growthRate()

    def daisy_check(self):
        '''
        Checks to make sure no daisy populations are negative, and that all
        daisy populations are less than 1.
        '''
        # If any daisy population goes below machine precision, set to 1e-15,
        # which is sufficient for our purposes.
        self.a_vec = np.where(self.a_vec < 1e-16, 1e-15, self.a_vec)

    def growthRate(self):
        '''
        Determines the growth rate (beta). If T != None (default), uses the
        given T. It will always return beta, but will only assign it as an
        attribute if test = False (default).
        '''
        self.beta = 1 - 3.265e-3 * (self.T_vec - self.T)**2.
        return self.beta

    def emptySpace(self, a_vec=None):
        '''
        Determines "x", the remaining area without daisies.
        '''
        if a_vec is None:
            self.x = self.P - np.sum(self.a_vec[:-1])
            self.a_vec[-1] = self.x
        else:
            self.x = self.P - np.sum(a_vec[:-1])
            self.a_vec[-1] - self.x
        return self.x

    def albedo(self, a_vec=None):
        '''
        Determines the albedo of this patch.
        '''
        if a_vec is None:
            self.A = self.a_vec@self.A_vec
        else:
            self.A = a_vec@self.A_vec
        return self.A

    def T_i(self, A=None):
        '''
        Calculates individual daisy temperatures as a function of albedo.
        '''
        if A is None:
            return np.power(c.q * (self.A - self.A_vec) + self.Teff**4, 1/4)
        else:
            return np.power(c.q * (self.A - A) + self.Teff**4, 1/4)

    def temperature(self):
        '''
        Determines the temperature and changes it given the current values.
        '''
        self.Teff = np.power(self.S * self.L * (1. - self.A) / c.sigma, 1/4)

        # These comments right now are for testing
        # print('\n\n')
        # for key, item in self.__dict__.items():
        #     print(f'{key} = {item}')

        if np.isnan(self.A):
            print('\n\n')
            for key, item in self.__dict__.items():
                print(f'{key} = {item}')
            pdb.set_trace()

        # Temperature array with Teff standing in for the ground question
        self.T[:-1] = self.T_i(self.A_vec[:-1])
        self.T[-1] = self.Teff
        return self.T

    def dDaisies(self, r, t, x, beta, gamma):
        '''
        Differential equations to be solved for the daisy object.
        '''
        da = r * (beta * x - gamma)
        # Handle ground
        da[-1] = 0.
        return da

    def rk4Solve(self, maxsteps, h, autostop=True, onestep=False):
        '''
        Implements the 4th-order Runga-Kutta method for solving differential
        equations.

        Args:
            + maxsteps (float): maximum number of steps to take
            + h (float): the step size (is this what it's formally called?)
            + func (function): The function that defines the differential
                system.
            + args (iterable): Arguments as an iterable to be passed to func
                after r0 and t. Default [], which implies no additional
                arguments.
            + autostop (bool, default True): if True, will stop when the change
                each iteration is less than 1e-6.
            + onestep (bool, default False): if True, will only step forward
                once in the rk4 solver instead of waiting for time convergence.
                This *overrides autostop*.

        Returns:
            + ts (array): the array of times integrated over
            + rs (array): array containing the integration itself over time.
        '''
        args = [self.x, self.beta, self.gamma]
        # Only perform one step if onestep
        if onestep:
            t = 0
            r = self.a_vec
            k1 = h * self.dDaisies(r, t, *args)
            self._update(r + 0.5 * k1)
            k2 = h * self.dDaisies(r + 0.5 * k1, t + 0.5*h, *args)
            self._update(r + 0.5 * k2)
            k3 = h * self.dDaisies(r + 0.5 * k2, t + 0.5*h, *args)
            self._update(r + k3)
            k4 = h * self.dDaisies(r + k3, t + h, *args)
            r = r + (1/6) * (k1 + 2*k2 + 2*k3 + k4)
            self.a_vec = r
            self._update()
            return r

        # Create the ts and rs
        ts = np.linspace(0, 1000., maxsteps)
        r0 = self.a_vec
        rs = np.zeros((len(ts), len(r0)))
        rs[0, :] = r0

        # Useful output
        areas = self.a_vec * self.P
        if self.verbose:
            print(f'Beginning daisy integration...')
            for i, area in enumerate(areas):
                print(f'area {i}: {area:1.3e}', end=' || ')
            print(f'Tsurf = {self.Teff:.3f}', end='\n')

        # use cur_r if autostop is enabled
        if autostop:
            cur_r = r0
            prev_Teff = -100000

        # 4th-order Runga Kutta integration
        dr = 1e6
        for i, t in enumerate(ts[:-1]):
            # 4th order Runga-Kutta integration step
            k1 = h * self.dDaisies(rs[i], t, *args)
            self._update(rs[i] + 0.5 * k1)
            k2 = h * self.dDaisies(rs[i] + 0.5 * k1, t + 0.5*h, *args)
            self._update(rs[i] + 0.5 * k2)
            k3 = h * self.dDaisies(rs[i] + 0.5 * k2, t + 0.5*h, *args)
            self._update(rs[i] + k3)
            k4 = h * self.dDaisies(rs[i] + k3, t + h, *args)
            rs[i+1] = rs[i] + (1/6) * (k1 + 2*k2 + 2*k3 + k4)

            # Update the daisy pop attributes
            self.a_vec = rs[i+1]

            # Recalculate the new physical parameters for the daisies.
            self._update()

            # Print out the current step
            areas = self.a_vec * self.P
            if self.verbose:
                for i, area in enumerate(areas):
                    print(f'area {i}: {area:1.3e}', end=' || ')
                print(f'Tsurf = {self.Teff:.3f}', end='\n')

            if autostop:
                # Check for convergence
                dr = np.absolute(np.linalg.norm(rs[i+1]-cur_r))
                dr = np.amax(np.absolute(rs[i+1] - cur_r))
                dT = np.absolute(self.Teff - prev_Teff)
                if np.amax([dr, dT]) < 1e-4:
                    if self.verbose:
                        print("Converged!")

                    break

                # Update cur_r for next dr comparison
                cur_r = rs[i+1]
                dT = np.absolute(self.Teff - prev_Teff)
                prev_Teff = self.Teff

            args = [self.x, self.beta, self.gamma]

        return ts, rs


from sklearn.feature_extraction.text import CountVectorizer

from flask import Flask, render_template,request
import pickle
import numpy as np
from nltk.stem.porter import PorterStemmer
import nltk
from nltk.corpus import stopwords
ps=PorterStemmer()

model = pickle.load(open('set2_model.pkl','rb'))
keywords = pickle.load(open('keyword.pkl','rb'))
remove_special1 = pickle.load(open('remove_special.pkl', 'rb'))

app = Flask(__name__)

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

@app.route('/recommend_movies',methods=['post'])
def recommend():
    title = str(request.form.get('user_input')).lower()
    #remove special char
    title=remove_special1(title)
    # remove stop words
    title= remove_stopWords1(title)

    #steming
    title= stem_word1(title)

    title_list =" ".join(title)

    # title = list(str(request.form.get('user_input')).split(" "))
    # title_list =" ".join(title)

    # converting title into list of words
    # title_list=[]
    # for i in title.split(" "):
    #     title_list.append(i)
    title_list = [title_list]

    vectorize = CountVectorizer(vocabulary=keywords)
    x_test =vectorize.transform(title_list)

    x= model.predict(x_test.reshape(1,3200)).argmax(axis =1)

    if x==0:
        result= 'computer science'
    elif x==1:
        result= 'Food'
    elif x==2:
        result= 'Art&Music'
    else:
        result= 'Can,t predict, Try diffrent tiltle'
    return render_template('recommend.html', result=result)

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


import requests
import json
import collections
import os
import re

AUTH_READ_KEY = 'S3zaadNPijs8'
username = 'uClassify'
classifierName = 'Society-Topics'

def listToDict(l):
  dictionary = collections.defaultdict(float)
  for item in l:
    class_name = item['className']
    p = item['p']
    dictionary[class_name] = float(p)
  return dictionary

def classifyRequest(text):
  url = 'https://api.uclassify.com/v1/'+username+'/'+classifierName+'/'+'classify'
  headers = {'Authorization': 'Token ' + AUTH_READ_KEY}
  data = "{\"texts\": [\"" + text + "\"]}"

  r = requests.post(url, headers=headers, data=data.encode('utf-8'))
  response = json.loads(r.text)[0]
  classifications = response['classification']

  return (listToDict(classifications))

#Dictionary where first line is key
songs = collections.defaultdict()

#Directories for all songs
directories = [
  '../lyrics/all-non-billboard-artist/',
  '../lyrics/dataset_1/billboard-lyrics/test-set',
  '../lyrics/dataset_1/billboard-lyrics/training-set',
  '../lyrics/dataset_1/billboard-lyrics/validation-set',
   '../lyrics/dataset_1/non-billboard-lyrics/test-set',
  '../lyrics/dataset_1/non-billboard-lyrics/training-set',
  '../lyrics/dataset_1/non-billboard-lyrics/validation-set',
]
count = 0
songs = collections.defaultdict()

for directory in directories:
  for filename in os.listdir(directory):
    if filename == '.DS_Store':
      continue

    #Dictionary where first line is key

    count +=1
    with open(directory + '/' + filename, 'r') as f:
      lines = f.readlines()
      firstLine = lines[0]
      firstLine = firstLine.split(':')
      key = ':'.join(firstLine[0:2])
      key = key.replace('\n', '')

      text = ' '.join(lines)
      text = text.replace('\n', '')

      text = re.sub('\W+', ' ', text)

      songs[key] = (classifyRequest(text))

    if count % 10 == 0:
      print ('Processed ' + str(count) + ' songs...')

with open('../song_info/song_themes_1.json', 'a') as fp:
  json.dump(songs, fp, sort_keys=True, indent=4)


from PIL import Image
import glob, os
from resizeimage import resizeimage

dest = "D:\\Data\\temp\\"
source ="D:\Data\Courses\CS674\ISIC-2017_Training_Data_phase3\*.jpg"

size = 128, 128

count = 0

for infile in glob.glob(source):
	file, ext = os.path.splitext(infile)
	name = file.split('\\')
	file_name = name[-1]
	im = Image.open(infile)
	im.thumbnail(size,Image.ANTIALIAS)
	#im.resize((size))

	cover = resizeimage.resize_cover(im, [128, 128])
	saved = dest+file_name+".jpg"
	cover.save(saved, im.format)

	#im.save()
	count += 1
	if count == 100:
		break


import random
from cachetools import TTLCache, cached
from youtubesearchpython import VideosSearch
from imdb import IMDb

imdb = IMDb()
cache = TTLCache(maxsize=100, ttl=86400)

class NotFound(Exception):
    pass

@cached(cache)
def _get_movie(movie_title: str):
    results = imdb.search_movie(movie_title)[0]
    imdb.get_movie_infoset()
    return imdb.get_movie(results.movieID)

@cached(cache)
def get_top_250():
    res = imdb.get_top250_movies()
    movies = [movie['title'] for movie in res]
    return movies

def get_plot(movie_title: str):
    results = _get_movie(movie_title)
    results = str(results['plot'][0])
    try:
        results = results.split('::')[0]  # remove unnecessary characters
    except ValueError:
        pass
    return results

def get_poster(movie_title: str):
    movie = _get_movie(movie_title)
    return movie['cover url']

def get_directors(movie_title: str):
    movie = _get_movie(movie_title)
    res = str()
    for person in movie['directors']:
        res = res + person['name'] + ", "
    if len(movie['directors']) == 1:
        for person in movie['directors']:
            res = res + person['name']
    return res

def get_runtime(movie_title: str):
    movie = _get_movie(movie_title)
    runtime_mins = int(movie['runtimes'][0])
    runtime = '{:02d}:{:02d}'.format(*divmod(runtime_mins, 60))
    hours, minutes = runtime.split(':')
    runtime = int(hours) * 60 + int(minutes)
    return runtime

def get_cast(movie_title: str):
    movie = _get_movie(movie_title)
    res = str()
    for person in movie['cast']:
        res = res + person['name'] + ", "
    return res

def get_category(movie_title: str):
    movie = _get_movie(movie_title)
    return movie['genres'][0]

def get_producer(movie_title: str):
    movie = _get_movie(movie_title)
    res = str()
    # added this hackish nonsense cause VARCHAR count for producer in movie is not large enough
    person_count = 0
    for person in movie['producers']:
        if person_count == 1:
            break
        res = res + person['name']
        person_count += 1
    return res

def get_reviews(movie_title: str):
    movie = _get_movie(movie_title)
    results = imdb.get_movie_reviews(movie.movieID)
    # select a random review from the 10000000 provided lol
    return results['data']['reviews'][random.randint(0, len(results['data']['reviews']) - 1)]['content']

def get_imdb_rating(movie_title: str):
    movie = _get_movie(movie_title)
    return movie['rating']

def get_trailer(movie_title: str):
    vid = VideosSearch(f"{movie_title} Movie Trailer", limit=1)
    return vid.result()['result'][0]['link']

def get_mpaa_rating(movie_title: str):
    movie = _get_movie(movie_title)
    for cert in movie['certificates']:
        # grab the USA film rating
        if 'United States' in str(cert):
            # make sure it's not the TV rating
            if 'TV' in str(cert):
                continue
            return str(cert).split(':')[1]


from email.mime import image
import os
from django.shortcuts import render
from django.views import View
from django.http.response import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
import cv2
import urllib
import numpy as np

FACE_DETECTOR_PATH = "{base_path}/cascades/haarcascade_frontalface_default.xml".format(base_path=os.path.abspath(os.path.dirname(__file__)))

def _grab_image(path=None, stream=None, url=None):
    # if the path is not None, then load the image from disk
    if path is not None:
        image = cv2.imread(path)
    # otherwise, the image does not reside on disk
    else:
        # if the URL is not None, then download the image
        if url is not None:
            #resp = urllib.urlopen(url)
            resp = urllib.request.urlopen(url)
            data = resp.read()
        # if the stream is not None, then the image has been uploaded
        elif stream is not None:
            data = stream.read()
        # convert the image to a NumPy array and then read it into
        # OpenCV format
        image = np.asarray(bytearray(data), dtype="uint8")
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image

class FaceDetectionView(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def get(self, request):
        data = {'message':'Hello world'}
        return JsonResponse(data)

    def post(self, request):
        data = {"success": False}

        if request.FILES.get("image", None) is not None:
            image = _grab_image(stream=request.FILES["image"])
        else:
            url = request.POST.get("url", None)
            if url is None:
                data["error"] = "Not Url provided"
                return JsonResponse(data)

            image = _grab_image(url=url)

        # convert the image to grayscale, load the face cascade detector,
        # and detect faces in the image
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
        rects = detector.detectMultiScale(gray, 1.3, 5)

        # construct a list of bounding boxes from the detection
        rects = [(int(x), int(y), int(x + w), int(y + h)) for (x, y, w, h) in rects]
        # update the data dictionary with the faces detected
        data.update({"num_faces": len(rects), "faces": rects, "success": True})
        # return a JSON response
        return JsonResponse(data)

    def put(self, request):
        pass

    def delete(self, request):
        pass


# coding: utf-8

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

from color_spectrum import Spectrum

# In[2]:

spec = Spectrum()
spec._init_empty_colormeshes()
spec._colorize_colormeshes()

# In[3]:

# name = "red green blue white"
# rgbb = spec.get_colormesh(name)
# orig = rgbb[name].copy()

# In[4]:

# mask = orig[:, :, 0] > 0.8 * orig[:, :, 1]**3 + 0.4

# In[5]:

# cut = orig.copy()
# cut[mask] = 0

# In[6]:

# fig, (ax_orig, ax_cut) = plt.subplots(ncols=2)
# ax_orig.imshow(orig, origin="lower")
# ax_orig.set_title("Original")
# ax_orig.set_xlabel("R")
# ax_orig.set_ylabel("G")
# ax_cut.imshow(cut, origin="lower")
# ax_cut.set_title(r"$R < 0.8 \cdot G^2$")
# ax_cut.set_xlabel("R")
# ax_cut.set_ylabel("G")
# fig.tight_layout()
# fig.savefig("/tmp/cut.png")

# In[7]:

def plot_orig_cut(mesh, color0 = 0, color1 = 1, factor = 0, offset = 0, exponent = 1, mantisse = 1, dark=True):
    """Plots original and cut colormesh side to side.

    Parameter:
        mesh: numpy.ndarray with shape (256, 256, 3)
        dark: boolean if mesh is darker or lighter
        cut: dictinary

    """
    fig, (ax_orig, ax_cut) = plt.subplots(ncols=2, figsize=(8, 4))
    colors = {0: "R", 1: "G", 2: "B"}

    x0 = color0
    x1 = color1
    a = factor
    o = offset
    e = exponent
    m = mantisse
    mask = orig[:, :, x0] > a * (orig[:, :, x1] + o) ** e + m

    cut = mesh.copy()
    cut[mask] = 1 if dark else 0

    ax_orig.imshow(mesh, origin="lower")
    ax_orig.set_title("Original")
    ax_orig.set_xlabel(colors[x0])
    ax_orig.set_ylabel(colors[x1])
    ax_cut.imshow(cut, origin="lower")
    ax_cut.set_title(r"Cut-Out: ${} > {} \cdot ({} + {}) ^ {} + {}$".format(colors[x0], a, colors[x1], o, e, m))
    ax_cut.set_xlabel(colors[x0])
    ax_cut.set_ylabel(colors[x1])

    fig.tight_layout()

    return fig, cut

# In[8]:

name = "red green blue white"
rgbb = spec.get_colormesh(name)
orig = rgbb[name].copy()
fig, cut = plot_orig_cut(mesh=orig, **{"color0": 0, "color1": 1, "factor": 1.1, "exponent": 4, "mantisse": 0.3}, dark=False)
fig.savefig("/tmp/cut.png")

# In[13]:

name = "green blue red black"
rgbb = spec.get_colormesh(name)
orig = rgbb[name].copy()
fig, cut = plot_orig_cut(orig, **{"color0": 1, "color1": 2, "factor": 1, "exponent": 3, "mantisse": 0.15, "offset": 0.1}, dark=True)

fig.savefig("/tmp/cut.png")

# In[12]:

name = "blue red green white"
rgbb = spec.get_colormesh(name)
orig = rgbb[name].copy()
fig, cut = plot_orig_cut(orig, **{"color0": 1, "color1": 2, "factor": 1, "exponent": 3, "mantisse": 0.15, "offset": 0.1}, dark=False)

fig.savefig("/tmp/cut.png")


import json
import logging

logger = logging.getLogger(__name__)

# parserは、出力をパースするためのクラスです。
class FilmParser:
    def __init__(
        self,
        format,
        prefix="",
        sanity_check=None,
        line_prefix=["- "],
    ):
        """
        format: lines, markdown, json
        prefix: prefix to be added at the beginning
        sanity_check: function to check if the output is valid. Use 'assert' statement to check.
        max_retry: max number of retry. If sanity_check fails, retry until this number is reached. Set to 0 to disable retry.
        line_prefix: list of prefix to be recognized as a line. Used only when format is 'lines'.
        """
        assert format in [
            "lines",
            "markdown",
            "json",
        ], "format must be one of ['lines','markdown','json']"
        self.format = format
        self.prefix = prefix
        self.sanity_check = sanity_check
        if isinstance(line_prefix, str):
            self.list_prefix = [line_prefix]
        else:
            self.list_prefix = line_prefix

        self.set_retry_prompt(
            """
Please process the input text to ensure it strictly adheres to the {{format}} format.
- Identify and extract the essential content specific to the {{format}} format from the input.
- Remove any additional or extraneous lines before and after this core content.
- Ensure that the cleaned-up content is correctly formatted and can be successfully parsed as {{format}}.
- Do not alter the original texts in the contents.

# input text
{{input_text}}

# formatted text
"""
        )

    def set_retry_prompt(self, prompt):
        self.retry_prompt = prompt

    def _try_parse(self, text):
        if self.format == "lines":
            result = self.parse_lines(text)
        elif self.format == "markdown":
            result = self.parse_markdown(text)
        elif self.format == "json":
            result = self.parse_json(text)

        if self.sanity_check is not None:
            self.sanity_check(result)  # raise error when sanity check fails

        return result

    def _refine(self, text, config):
        from .core import FilmCore  # Cannot import at the top due to circuler import

        retry_film = FilmCore(prompt=self.retry_prompt, config=config)
        placeholder = {}
        placeholder["input_text"] = text
        placeholder["format"] = self.format
        return retry_film.run(placeholder)

    def parse(self, text, config):
        try:
            result = self._try_parse(self.prefix + text)
        except Exception as err1:
            logger.info(f"Parse error. Retry: {err1}")
            retry_text = self._refine(self.prefix + text, config)
            try:
                result = self._try_parse(retry_text)
            except Exception as err2:
                logger.error(f"Retry failed: {err2}")
                raise err2
        return result

    def parse_lines(self, text):
        """
        Parse text as list of lines.
        Args:
            text: str
        Returns:
            list of str
        """
        lines = text.split("\n")
        lines = [line.strip() for line in lines]  # remove spaces
        if len(self.list_prefix) == 0:
            return lines
        new_lines = []
        for line in lines:
            for prefix in self.list_prefix:
                if line.startswith(prefix):
                    new_lines.append(line[len(prefix) :])
                    break
        lines = [line.strip() for line in new_lines]  # remove spaces
        return lines

    def parse_json(self, text):
        """
        Parse text as json.
        Args:
            text: str
        Returns:
            structured dict
        """
        return json.loads(text)

    def parse_markdown(self, text):
        """
        Parse text as markdown.
        Args:
            text: str
        Returns:
            structured dict
        """

        result = {}
        lines = text.split("\n")
        title = ""
        for line in lines:
            line = line.strip()
            for prefix in self.remove_prefix:
                if line.startswith(prefix):
                    line = line[len(prefix) :]
                    break

            if line == "":
                continue

            if line.startswith("# "):
                title = line[2:].strip()
                result[title] = ""
            else:
                result[title] += line + "\n"
        return result


# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils import tripleo_common_utils as tc
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module

DOCUMENTATION = """
---
module: tripleo_get_introspected_data
short_description: Retrieve introspection data
extends_documentation_fragment: openstack
author:
  - "Kevin Carter (@cloudnull)"
version_added: "2.10"
description:
    - Pull introspection data from a baremetal node.
options:
    node_id:
        description:
            - ID of the baremetal node
        type: str
        required: true

requirements: ["openstacksdk", "tripleo-common"]
"""

EXAMPLES = """
- name: Get introspected data
  tripleo_get_introspected_data:
    node_id: xxx
  register: introspected_data
"""

import os

import yaml

from tripleo_common import exception

def main():
    result = dict(
        success=False,
        changed=False,
        error=None,
    )
    module = AnsibleModule(
        openstack_full_argument_spec(
            **yaml.safe_load(DOCUMENTATION)['options']
        ),
        **openstack_module_kwargs()
    )
    _, conn = openstack_cloud_from_module(module)
    tripleo = tc.TripleOCommon(session=conn.session)
    try:
        result['data'] = tripleo.return_introspected_node_data(
            node_id=module.params["node_id"]
        )
    except Exception as exp:
        result['error'] = str(exp)
        result['msg'] = 'Error pulling introspection data for {}: {}'.format(
            module.params["node_id"],
            exp
        )
        module.fail_json(**result)
    else:
        result['success'] = True
        module.exit_json(**result)

if __name__ == "__main__":
    main()


from queue import PriorityQueue
from collections import defaultdict

def search(starting_state, pods_in_group):
    # game states
    states = defaultdict(lambda: float('inf'))
    target_states = defaultdict(int)
    states[starting_state] = 0
    lowest_energy = int(1e9)
    # 'visited' states
    visited = []
    queue = PriorityQueue()
    queue.put((0, starting_state))
    while not queue.empty():
        (energy, current_state) = queue.get()
        #print(queue.qsize())
        visited.append(current_state)
        if is_target_state(current_state):
            if energy < lowest_energy:
                target_states[current_state] = energy
                print(f'found target state: {current_state} energy {energy}')
                lowest_energy = energy
            continue
        for (energy, next_state) in get_moves(current_state, pods_in_group):
            if next_state not in visited:
                old_energy = states[next_state]
                new_energy = states[current_state] + energy
                if new_energy < lowest_energy and new_energy < old_energy:
                    queue.put((new_energy, next_state))
                    states[next_state] = new_energy
    return target_states

def is_target_state(state):
    for i, a in enumerate(state):
        target_room = 2*(i//2)+2
        if a[1] == 0 or a[0] != target_room:
            return False
    return True

def get_hallway_path(x, target):
    start = min(x, target)
    end = max(x, target)
    path = list(range(start, end))
    path.append(end)
    path.remove(x)
    return path

def get_energy(i, moves):
    if i in [0,1,2,3]: return moves
    if i in [4,5,6,7]: return moves * 10
    if i in [8,9,10,11]: return moves * 100
    if i in [12,13,14,15]: return moves * 1000

DPM = {}
def get_moves(state, pods_in_group):

    if state in DPM:
        return DPM[state]

    moves = []
    rooms = defaultdict(lambda: [])
    hallway = []
    for i, a in enumerate(state):
        if a[1] < 0:
            rooms[a[0]].append((i, a))
        else:
            hallway.append(a[0])
    # state
    #    A     A     B     B     C     C     D     D
    # [(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)]
    for i, a in enumerate(state):
        target_room = pods_in_group*(i//pods_in_group)+2

        # are we where we belong?
        if a[0] == target_room:

            # if we're at the back, we're fine
            if a[1] == pods_in_group * -1:
                continue

            # if the room only contains my peeps, we're fine
            if all(pods_in_group*(t[0]//pods_in_group)+2 == target_room for t in rooms[target_room]):
                continue

        # if we are in the hallway
        if (a[1] == 0):

            # do we have a line of site to our room?
            # - figure out which spaces we'll need to move to on our way to our room
            hallway_path = get_hallway_path(a[0], target_room)

            # - are we blocked by someone else in the hallway?
            if any(step in hallway_path for step in hallway):
                continue

            # can we enter our target room?
            if len([x for x in rooms[target_room] if x[0]//pods_in_group != i//pods_in_group]) > 0:
                continue

            # - move into our room
            room_steps = pods_in_group - len(rooms[target_room])
            new_state = list(state)
            new_state[i] = (target_room, room_steps * -1)
            energy = get_energy(i, len(hallway_path) + room_steps)
            move = list()
            move.append((energy, tuple(new_state)))
            DPM[state] = move
            return move

        # if we are moving into a hallway
        if a[1] < 0:

            # are we blocked?
            if a[0] != target_room and a[1] < -1 and len(rooms[a[0]]) > 1:
                for i, b in rooms[a[0]]:
                    if b[1] > a[1]:
                        continue

            # for all of the possible destinations in the hallway
            for destination in [0,1,3,5,7,9,10]:
                hallway_path = get_hallway_path(a[0], destination)

                # - are we blocked by someone else in the hallway?
                if any(step in hallway_path for step in hallway):
                    continue
                new_state = list(state)
                new_state[i] = (destination, 0)
                energy = get_energy(i, len(hallway_path) + (a[1] * -1))
                moves.append((energy, tuple(new_state)))

    DPM[state] = moves
    return moves

##################
#     01234567890
#    #############
#  0 #...........#
# -1 ###B#C#B#D###
# -2   #D#C#B#A#
# -3   #D#B#A#C#
# -4   #A#D#C#A#
#      #########
#start = ((2,-4),(8,-2),(8,-4),(6,-3),(2,-1),(6,-1),(4,-3),(6,-2),(4,-1),(6,-2),(4,-2),(8,-3),(4,-2),(8,-1),(2,-2),(2,-3))
#energy = 12521
#    #############
#  0 #...........#
# -1 ###D#C#A#B###
# -2   #D#C#B#A#
# -3   #D#B#A#C#
# -4   #D#C#B#A#
#      #########
#start = ((6,-1),(8,-2),(6,-2),(8,-1),(4,-2),(4,-1),(2,-2),(2,-1))
#energy =

states = search(start, 4)
key_min = min(states.keys(), key=(lambda k: states[k]))
print(f'looks like {states[key_min]} is the least energy possible')


import socket
import requests
import sys
import colorama
from colorama import Fore, Back, Style
from colorama import init
from termcolor import colored
import threading
from rich.progress import track
from time import sleep
from banner import *
import scapy.all as scapy

print(Fore.LIGHTMAGENTA_EX+banner_art)
#print(colored('Hello, World!', 'green', 'on_red'))

print('\n')

print("1.Port Scanner\n")
print("2.Wifi Scanner\n")

choice = int(input(Fore.GREEN+'ENTER YOUR CHOICE :'))

#banner = banner_art

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 0

#This function scans the given host (for example: www.google.com) and display's the port's status.

def port_scanner():
    try:
        #host = str(input("ENTER THE HOST NAME:"))
        ip_addr = socket.gethostbyname(host)
        msg = (f"scanning the network {ip_addr}")
        for step in track(range(100), description=msg):
            sleep(0.1)
        for port in range(1,100):
            conn = s.connect_ex((ip_addr,port))
            if(conn==0):
                print(Fore.GREEN+f"port {port} is open",'on_green')
            else :
                print(Fore.RED+f"port {port} is not open")

    except socket.gaierror:
        print("Enter the correct host !")
        sys.close()

#This function scan's your router and display's the connected devices.

def wifi_scanner():
    request = scapy.ARP() #Creating ARP packet using ARP() function.

    request.pdst = req
    msg = (f"Scanning network {request.pdst}")
    for step in track(range(100), description=msg):
            sleep(0.1)
    broadcast = scapy.Ether() #Creating Ether packet using Ether() function.

    broadcast.dst = 'ff:ff:ff:ff:ff:ff' #Destination set !

    request_broadcast = broadcast / request #Framing ethernet packet and arp packet using / .    clients = scapy.srp(request_broadcast, timeout = 10,verbose = 1)[0]

    for element in clients:
        print(element[1].psrc + "	 " + element[1].hwsrc)

if(choice==1):
    host = str(input(Fore.GREEN+"ENTER THE HOST NAME:"))
    port_scanner()

elif(choice==2):
    req = str(input(Fore.GREEN+"ENTER YOUR NETWORK RANGE :")) #Enter your router range (for example: 255.255.255.0/24) like this.
    wifi_scanner()

else:
    print(Fore.RED+"Error")


# importar bibliotecas
from flask import Flask, render_template, request
import pickle
import numpy as np

# Carregando o dataset
classifier = pickle.load(open('model/model.pkl', 'rb'))

app=Flask(__name__)

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

# Realizar predicao
@app.route('/predicao', methods=['POST'])
def predicao():
  if request.method=='POST':
    pregnancies=request.form['pregnancies']
    glicose=request.form['glicose']
    bloodpressure=request.form['bloodpressure']
    skinthickness=request.form['skinthickness']
    insulin=request.form['insulin']
    bmi=request.form['bmi']
    diabetesfunction=request.form['diabetesfunction']
    age=request.form['age']

    data_array=np.array([[pregnancies, glicose, bloodpressure, skinthickness, insulin, bmi, diabetesfunction, age ]])

    print(data_array)

    prediction_text=classifier.predict(data_array)
    return render_template('prediction.html', prediction=prediction_text, insulin=insulin, pg=pregnancies, gl=glicose, bp=bloodpressure, st=skinthickness, bmi=bmi, db=diabetesfunction, age=age)

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


import pandas as pd
import numpy as np
import logging
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler, FunctionTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction import DictVectorizer
from sklearn.decomposition import PCA
from scipy.stats import pearsonr
from sklearn.preprocessing import OneHotEncoder

logging.basicConfig(level=logging.INFO)

class DateTransformer(BaseEstimator, TransformerMixin):
    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        logging.info(f"Entered Date Columns: DateTransformer")
        X = X.copy()
        for col in X.columns:
            X[col] = (pd.Timestamp.now() - pd.to_datetime(X[col])).dt.days
        return X

    def get_feature_names_out(self, input_features=None):
        return input_features

class ExtremeValuesNumericalHandler(BaseEstimator, TransformerMixin):
    def __init__(self):
        self.lower_bound = None
        self.upper_bound = None

    def fit(self, X, y=None):
        logging.info(f"Entered Numerical Columns: ExtremeValuesNumericalHandler")
        X = pd.DataFrame(X)
        self.lower_bound = X.quantile(0.05)
        self.upper_bound = X.quantile(0.95)
        return self

    def transform(self, X):
        X = pd.DataFrame(X)
        for col in X.columns:
            X[col] = np.where(X[col] < self.lower_bound[col], self.lower_bound[col], X[col])
            X[col] = np.where(X[col] > self.upper_bound[col], self.upper_bound[col], X[col])
        return X.values

    def get_feature_names_out(self, input_features=None):
        return input_features

class DummyVariableCreator(BaseEstimator, TransformerMixin):
    def __init__(self, max_unique_values_cat=50):
        self.max_unique_values_cat = max_unique_values_cat

    def fit(self, X, y=None):
        X = pd.DataFrame(X)
        self.columns_to_encode = [col for col in X.columns if X[col].nunique() <= self.max_unique_values_cat]
        self.columns_to_drop = [col for col in X.columns if X[col].nunique() > self.max_unique_values_cat]
        return self

    def transform(self, X, y=None):
        logging.info(f"Entered Categorical Columns: DummyVariableCreator")
        X = pd.DataFrame(X)
        X = X.drop(columns=self.columns_to_drop)
        X = pd.get_dummies(X, columns=self.columns_to_encode, dummy_na=True, drop_first=True)
        self.feature_names_out_ = X.columns.tolist()  # get the output feature names after transformation
        return X

    def get_feature_names_out(self, input_features=None):
        return self.feature_names_out_

# class CleanFeatureNamesForXgboost(BaseEstimator, TransformerMixin):
#     def __init__(self):
#         self.lower_bound = None
#         self.upper_bound = None

#     def fit(self, X, y=None):
#         return self

#     def transform(self, X):
#         logging.info(f"Entered CleanFeatureNamesForXgboost")
#         X = pd.DataFrame(X)
#         X.columns = X.columns.astype(str)\
#                  .str.replace('[', '_replace_bracket_open_', regex=True)\
#                  .str.replace(']', '_replace_bracket_close_', regex=True)\
#                  .str.replace('<', '_smaller_than_', regex=True)
#         return X

#     def get_feature_names_out(self, input_features=None):
#         return input_features

# class LoggingPipeline(Pipeline):
#     def fit(self, X, y=None, **fit_params):
#         message = X.shape
#         for name, step in self.steps[:-1]:
#             print(f'entered {name}')
#             X = step[1].fit_transform(X, y, **fit_params)
#             logging.info(f"Finished {name}, Input shape: {message}, Output shape: {X.shape}")
#             message = X.shape
#         self.steps[-1][1].fit(X, y, **fit_params)
#         logging.info(f"Finished {self.steps[-1][0]}, Input shape: {message}, Output shape: {X.shape}")
#         return self

def preprocessing_pipeline(date_columns, numerical_columns, categorical_columns):

    # Define the pipeline steps
    date_pipeline = Pipeline([
        ('to_days', DateTransformer()),
        # ('clean_feature_names_for_date_cols', CleanFeatureNamesForXgboost())
])

    numerical_pipeline = Pipeline([
        ('imputer', SimpleImputer(strategy='median')),
        ('winsorizer', ExtremeValuesNumericalHandler()),
        ('scaler', StandardScaler()),
        # ('clean_feature_names_for_num_cols', CleanFeatureNamesForXgboost())
    ])

    categorical_pipeline = Pipeline([
        ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
        # ('onehot', OneHotEncoder(handle_unknown='ignore'))
        ('onehot_with_limit', DummyVariableCreator()),
        # ('clean_feature_names_for_cat_cols', CleanFeatureNamesForXgboost())
    ])

    # Combine the pipelines
    full_pipeline = ColumnTransformer(transformers=[
        ('date', date_pipeline, date_columns),
        ('num', numerical_pipeline, numerical_columns),
        ('cat', categorical_pipeline, categorical_columns)
    ])

    return full_pipeline


import mindspore as ms
from mindspore import context, Tensor, nn
from sklearn.datasets import load_iris

ms.set_seed(42)
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")

class Regularization:
    def __init__(self, X):
        self.X = X

    def dropout(self, drop_probability):
        """
        Dropout is a regularization technique for neural networks that drops a unit (along with connections) at
        training time with a specified probability P (a common value is P = 0.5). At test time, all units are present,
        but with weights scaled by p(i.e. w becomes pw ).
        The idea is to prevent co-adaptation, where the neural network becomes too reliant on particular
        connections, as this could be symptomatic of overfitting. Intuitively, dropout can be thought of as creating
         an implicit ensemble of neural networks.
        :param drop_probability: float value between 0 to 1
        """
        if drop_probability < 1.0:
            keep_probability = 1 - drop_probability
        masker =  ms.ops.normal(self.X.shape, mean=0.0, stddev=1.0, seed=5).astype(dtype=ms.float32)

        masked = masker < keep_probability

        if keep_probability > 0.0:
            scale = 1 / keep_probability
        else:
            scale = 0.0

        return masked * self.X * scale

    def L2_Regularization(self, y, W, lambda_value):
        """
        Weight Decay, or L2 Regularization, is a regularization technique applied to the weights of a neural network.
        We minimize a loss function compromising both the primary loss function and a penalty on the L2 Norm of the
        weights:
                L_new(w) = L_original(w) + lambda * W_T * W
        where  is a value determining the strength of the penalty (encouraging smaller weights).
        Weight decay can be incorporated directly into the weight update rule, rather than just implicitly by defining
        it through to objective function. Often weight decay refers to the implementation where we specify it directly
        in the weight update rule (whereas L2 regularization is usually the implementation which is specified in the
        objective function).
        """
        Regularization_term = (lambda_value * ms.ops.mm(W, W.transpose())).astype(ms.float32) / (2 * y.shape[0])
        output = ms.ops.sum((y - ms.ops.mm(X, W.T))**2, dim=0) + Regularization_term
        return output

    def L1_Regularization(self, y, W, lambda_value):
        """
         L1 Regularization is a regularization technique applied to the weights of a neural network. We minimize a loss
        function compromising both the primary loss function and a penalty on the L1 Norm of the weights:
            L_new(w) = L_original(w) + lambda * ||W||
        where is a value determining the strength of the penalty. In contrast to weight decay, regularization promotes
        sparsity; i.e. some parameters have an optimal value of zero.
        """
        Regularization_term =  ms.ops.sum((lambda_value * ms.ops.abs(W)).astype(ms.float32) / (2 * y.shape[0]),dim=1)
        output = ms.ops.sum((y - ms.ops.mm(X, W.transpose()))**2, dim=0) + Regularization_term
        return output

if __name__ == '__main__':

    print('### => Dropout: ')

    A = ms.ops.arange(20).reshape((5, 4))
    print(A)
    Regularizer = Regularization(X=A)
    print(Regularizer.dropout(drop_probability=0.5))

    print('### => L2 Regularization or Weight Decay: ')

    data = load_iris()
    X = Tensor(data.data, dtype=ms.float32)
    y = Tensor(data.target).unsqueeze(1)
    W =  ms.ops.normal((1, X.shape[1]), mean=0.0, stddev=1.0, seed=5).astype(dtype=ms.float32)

    Regularizer = Regularization(X)
    Regularizer.L2_Regularization(y=y, W=W, lambda_value=0.7)

    print('### => L1 Regularization: ')

    data = load_iris()
    X = Tensor(data.data, dtype=ms.float32)
    y = Tensor(data.target).unsqueeze(1)
    W =  ms.ops.normal((1, X.shape[1]), mean=0.0, stddev=1.0, seed=5).astype(dtype=ms.float32)
    Regularizer = Regularization(X)
    print(Regularizer.L1_Regularization(y=y, W=W, lambda_value=0.7))


# -*- coding: utf-8 -*-
# the above line is necessary to run the server in utf-8

"""This module is built to run a simple RESTful API"""
import os
import datetime
import numbers
from json import dumps
from flask import Flask, jsonify, request, render_template, redirect, url_for, make_response, abort

# from flask_debugtoolbar import DebugToolbarExtension
app = Flask(__name__)

@app.route('/')
def index():
    """
    [root get request]
    """
    return "[CONNECTED] available routes: consult README.md", 200

@app.route('/hello', methods=['GET', 'POST'])
def say_hello():
    """
    [Say hello with gender, first/last name]
    This route will receive three parameters, and will return a greeting.

    Route structure:
    /hello?firstname={first name}&lastname={last name}&gender={m/f}

    Parameters:
    firstname, lastname, gender(choose: m/f)

    Response:
    "Hello Mr {First Name} {Last Name}”
    or
    “Hello Ms {First Name} {Last Name}”
    depending on the gender.

    Example:
    request “/hello?firstname=tien&lastname=nguyen&gender=m”
    response “Hello Mr Tien Nguyen”
    """

    firstname = request.args.get('firstname')[0].upper() + request.args.get('firstname')[1:]
    lastname = request.args.get('lastname')[0].upper() + request.args.get('lastname')[1:]
    gender = request.args.get('gender')

    if gender == "m":
        prefix = "Mr"
    elif gender == "f":
        prefix = "Ms"
    else:
        prefix = "M"

    return dumps("Hello" + " " +prefix + " " + firstname + " " + lastname), 201

@app.route('/compute', methods=['GET', 'POST'])
def calculate():
    """
    [Basic Calculation]
    This route will receive three parameters and will return the result of calculation.abort

    Route structure:
    /compute?num1={num1}&num2={num2}&operator={add/subtract/multiply/divide}

    Parameters:
    num1, num2, operator(choose: add/subtract/multiply/divide)

    Response:
    calculated result in an integer

    Example:
    the request “/compute?num1=5&num2=3&operation=subtract” returns “2” (5-3=2)
    """

    num1 = int(request.args.get('num1').decode('utf-8'))
    num2 = int(request.args.get('num2').decode('utf-8'))
    operation = request.args.get('operation').lower()

    if operation == "add":
        result = num1 + num2
    elif operation == "subtract":
        result = num1- num2
    elif operation == "multiply":
        result = num1 * num2
    elif operation == "divide":
        if (num2 == 0):
            return "Cannot divide with zero"
        result = num1 / num2
    else:
        return "ERROR: Input incorrect to calculate", 500

    return dumps(result), 201

@app.route('/date')
def get_date():
    """
    [Get date for today in iso format]
    This route will not receive any parameters, and will return today's date in iso format

    Route structure:
    /date

    Response:
    the current date in the form “yyyy-mm-dd”

    Example:
    “/date” returns “2017-09-20”
    """

    date_obj = datetime.date.today()

    return dumps(date_obj.isoformat()), 200

#####################
#   Error Routes
#####################

@app.errorhandler(404)
def page_not_found(e):
    """Better error handling"""
    return make_response(jsonify({'error': 'Not found'}), 404)

@app.errorhandler(500)
def internal_server_error(e):
    return make_response(jsonify({'error': 'Internal server error'}), 500)

@app.errorhandler(403)
def page_forbidden(e):
    return make_response(jsonify({'error': 'page_forbidden'}), 403)

# To run the error routes above, turn off the debug mode below
#####################
#   Context Processors
#####################

if __name__ == '__main__':
    # debug=True here, since it need be True at point DebugToolbarExtension get invoked
    app.run(debug=True)

    # # Use the DebugToolbar
    # DebugToolbarExtension(app)

    # Run internal server
    app.run(port=5000, host='0.0.0.0')


import os
import tarfile
from typing import List

import latex2markdown
from langchain.document_loaders import DirectoryLoader, WebBaseLoader

import arxiv
from arxivChat.config import ARXIV_DIR, ARXIV_IDS, URLS, logger

def create_arxiv_filename(arxiv_id: str):
    """ Create file name from arxiv_id: replace '.' by '_'

    Args:
        arxiv_id (str): arxiv id, of the form 'xxxx.xxxx'
    """
    return arxiv_id.replace('.', '_')

def download_source(arxiv_ids: List[str], dirpath: str):
    """Download arxiv source and extract tarfile

    Args:
        arxiv_ids (list): List of arxiv ids, of the form "xxxx.xxxx"
        dirpath (str): directory to store the arxiv files
    """

    # create the directory
    os.makedirs(dirpath, exist_ok=True)

    for arxiv_id in arxiv_ids:
        logger.info(f"Fetch the paper {arxiv_id}")
        # get the paper metadata
        paper = next(arxiv.Search(id_list=[arxiv_id]).results())

        # download the source file
        filename = create_arxiv_filename(arxiv_id)
        tar_filename = f"{filename}.tar.gz"
        paper.download_source(dirpath=dirpath, filename=tar_filename)
        tar_gz_path = os.path.join(dirpath, tar_filename)

        # extract the .tar.gz file
        with tarfile.open(tar_gz_path, "r:gz") as tar:
            tar.extractall(path=os.path.join(dirpath, filename))
            logger.info(f"Files extracted at {os.path.join(dirpath, filename)}")

def find_tex_files(dirpath: str):
    """ Find all the .tex files recursively within a directory

    Args:
        dirpath (str): directory to find the .tex files
    """
    tex_files = []
    for root, _, files in os.walk(dirpath):
        for file in files:
            if file.endswith(".tex"):
                tex_files.append(os.path.join(root, file))
    logger.info(f"Find {len(tex_files)} .tex files")
    return tex_files

def tex_to_md(tex_paths: List[str]):
    """Convert tex files to markdown files

    Args:
        tex_paths (list): list of paths to the .tex files
    """
    for tex_path in tex_paths:
        with open(tex_path, "r") as f:
            latex_string = f.read()

        l2m = latex2markdown.LaTeX2Markdown(latex_string)

        markdown_string = l2m.to_markdown()

        md_path = tex_path.replace(".tex", ".md")
        with open(md_path, "w") as f:
            f.write(markdown_string)

def load_md_files(directory: str):
    """Find all markdown files in a directory and return a LangChain Document

    Args:
        directory (str): directory to search for markdown files

    Returns:
        langchain documents
    """
    dl = DirectoryLoader(directory, "**/*.md")
    documents = dl.load()
    return documents

def load_arxiv_ids(arxiv_ids: List[str] = ARXIV_IDS,
              arxiv_dir: str = str(ARXIV_DIR)):
    """Fetch the arxiv papers and return langchain documents

    Args:
        arxiv_ids (List[str], optional): List of arxiv ids. Defaults to ARXIV_IDS.
        arxiv_dir (str, optional): Directory to save the arxiv papers.
                        Defaults to str(ARXIV_DIR).
    """
    logger.info("Fetch the arxiv sources")
    download_source(arxiv_ids, arxiv_dir)

    logger.info(f"Find all the .tex files")
    tex_paths = find_tex_files(arxiv_dir)

    logger.info("Convert tex to md files")
    tex_to_md(tex_paths)

    logger.info("Find all md documents")
    documents = load_md_files(arxiv_dir)
    logger.info(f"We have {len(documents)} documents from {len(arxiv_ids)} arxiv_ids")

    return documents

def load_urls(urls: List[str]=URLS):
    """Fetch the urls and return langchain documents

    Args:
        urls (List[str], optional): List of urls. Defaults to URLS.
    """
    logger.info("Fetch the urls")
    loader = WebBaseLoader(urls)
    documents = loader.load()
    logger.info(f"We have {len(documents)} documents from {len(urls)} urls")
    return documents

if __name__ == "__main__":
    logger.info("Load arxiv articles into documents")
    documents = load_arxiv_ids()
    logger.info("Load the urls into documents")
    document_url = load_urls()