#GUI CODE
import threading
import time
import tkinter as tk
import openai
from tkinter import ttk, filedialog
from NL_SQL_Engine import *
from entity import *

CREATE_SCHEMA_RESULTS="EMPTY"
CREATE_QUERY_RESULTS="EMPTY"
ASSESS_SCHEMA_RESULTS="EMPTY"

CREATE_SCHEMA_ID = 1
CREATE_QUERY_ID= 2
ASSESS_SCHEMA_ID= 3

#main windows
root = tk.Tk()
root.geometry("800x400")
root.title("SQL SCAFFOLD")

# Create a custom style for the Notebook tabs
style = ttk.Style()
style.configure("TNotebook.Tab", padding=[30, 5])  # Adjust the padding here

# Create a Notebook (Tabbed interface) with the custom style
notebook = ttk.Notebook(root, style="TNotebook")

# Set up frames and add them to the gui
createSchemaFrame = ttk.Frame(notebook)
generateQueryFrame = ttk.Frame(notebook)

notebook.add(createSchemaFrame, text="Create Schema")
notebook.add(generateQueryFrame, text="Generate Query")

# Pack the notebook to make it visible
notebook.pack(fill='both', expand=True)

'''
--------------------------------------------------------------
-------------------CREATE SCHEMA ~ VARIABLES------------------
--------------------------------------------------------------
'''

#list of all entities thus far appended
entities = []
DBname = None
response = None

'''
------------------------------------------------------------
-------------------CREATE SCHEMA ~ METHODS------------------
------------------------------------------------------------
'''

# Function to clear and update entry widgets
def clear_entry():
    # Clear the associated StringVar for each entry
    entity_name_var.set("")
    entity_description_var.set("")
    database_name_var.set("")

# Function to restart the process
def restart():
    # Clear entities and associated StringVars
    global entities
    entities = []
    entity_name_var.set("")
    entity_description_var.set("")
    display_entities.delete(1.0, tk.END)
    display_schema.delete(1.0, tk.END)
    update_widgets()

# Function to add an entity
def add_entity():
    # Extract information from entry boxes
    entity_name = entity_name_var.get()
    entity_description = entity_description_var.get()

    # Add the entity to the list as a string
    user_defined_entity = Entity(entity_name, entity_description)
    entities.append(user_defined_entity)

    for e in entities:
        print(e.toString() + "\n")

    # Go through each widget and update the contained content
    update_widgets()

# Function to update widgets
def update_widgets():
    # Iterate through entities and insert them into the Text widget
    display_entities.delete(1.0, tk.END)
    for index, e in enumerate(entities, start=1):
        entity_info = f"{index}. {e.name}\n   {e.description}\n\n"
        display_entities.insert(tk.END, entity_info)

    # Clear out entry boxes
    entity_name_var.set("")
    entity_description_var.set("")
    #database_name_var.set("")

#update display schema to include ChatGPT response
def update_schema_display():
    # Clear the existing content in the Text widget
    display_schema.delete(1.0, tk.END)

    display_schema.insert(tk.END, CREATE_SCHEMA_RESULTS)

#create schema draft via SQL_Engine
def generate():

    #access/update dbname global variable
    global DBname
    DBname = database_name_entry.get()

    #generate schema via sql engine
    Loading_task(lambda:create_schema(entities, DBname), CREATE_SCHEMA_ID)

    #update display
    update_schema_display()

def export():
    # Open a file in write mode (creates the file if it doesn't exist)
    global DBname
    filename = DBname + ".sql"
    with open(filename, 'w') as file:
        # Print a string to the file
        print(CREATE_SCHEMA_RESULTS, file=file)

#user input database attributes: name & entities

'''
------------------------------------------------------------
---------------CREATE SCHEMA ~ DEFINE WIDGETS---------------
------------------------------------------------------------
'''

#Labels
entity_name_label = ttk.Label(createSchemaFrame, text="Entity Name:")
entity_description_label = ttk.Label(createSchemaFrame, text="Entity Description:")
database_name_label = ttk.Label(createSchemaFrame, text="Database Name:")
entity_display_label = ttk.Label(createSchemaFrame, text="Current Entities:")
schema_display_label = ttk.Label(createSchemaFrame, text="Current Schema:")

# Entry variables
entity_name_var = tk.StringVar()
entity_description_var = tk.StringVar()
database_name_var = tk.StringVar()

# Entry boxes
entity_name_entry = ttk.Entry(createSchemaFrame, width=20, textvariable=entity_name_var)
entity_description_entry = ttk.Entry(createSchemaFrame, width=40, textvariable=entity_description_var)
database_name_entry = ttk.Entry(createSchemaFrame, width=20, textvariable=database_name_var)

#Buttons
add_entity_button = ttk.Button(createSchemaFrame, text="Add new entity", command=add_entity)
restart_button = ttk.Button(createSchemaFrame, text="Restart", command=restart)
generate_button = ttk.Button(createSchemaFrame, text="Generate", command=generate)
export_button = ttk.Button(createSchemaFrame, text="Export (.sql)", command=export)

#Text Boxes
display_entities = tk.Text(createSchemaFrame, height=70, width=55, wrap=tk.WORD)
display_schema = tk.Text(createSchemaFrame, height=70, width=55, wrap=tk.WORD)

#Scroll Bars
entities_scrollbar = ttk.Scrollbar(createSchemaFrame, command=display_entities.yview)
display_entities.config(yscrollcommand=entities_scrollbar.set)
schema_scrollbar = ttk.Scrollbar(createSchemaFrame, command=display_schema.yview)
display_schema.config(yscrollcommand=schema_scrollbar.set)

'''
---------------------------------------------------------
---------------CREATE SCHEMA ~ GRID LAYOUT---------------
---------------------------------------------------------
'''

#Show Labels
entity_name_label.grid(row=1, column=0, padx=10, pady=0, sticky="nw")
entity_description_label.grid(row=2, column=0, padx=10, pady=0, sticky="nw")
database_name_label.grid(row=0, column=0, padx=10, pady=20, sticky="nw")
entity_display_label.grid(row=0, column=2, padx=5, pady=0, sticky="nw")
schema_display_label.grid(row=0, column=4, padx=5, pady=0, sticky="nw")

#Show Entry Boxes
entity_name_entry.grid(row=1, column=1, padx=5, pady=0, sticky="nw")
entity_description_entry.grid(row=2, column=1, padx=5, pady=0, sticky="nw")
database_name_entry.grid(row=0, column=1, padx=5, pady=20, sticky="nw")

#Show Buttons
add_entity_button.grid(row=3, column=0, padx=50, pady=0, sticky="nw")
restart_button.grid(row=4, column=1, padx=50, pady=0, sticky="nw")
generate_button.grid(row=3, column=1, padx=50, pady=0, sticky="nw")
export_button.grid(row=4, column=0, padx=50, pady=0, sticky="nw")

#Display Text Boxes
display_entities.grid(row=1, column=2, rowspan=11, padx=10, pady=10, sticky="nsew")
display_schema.grid(row=1, column=4, rowspan=11, padx=10, pady=10, sticky="nsew")

#Display Scroll Bars
entities_scrollbar.grid(row=1, column=3, rowspan=11, pady=10, sticky="ns")
schema_scrollbar.grid(row=1, column=5, rowspan=11, pady=10, sticky="ns")

"""
The Async loading function
Takes in the task to be done(params already comrpresses in lambda form)
and the id of the task to be done. This is ugly because TINKTER and Threads
HATE EACH OTHER. If you want to improve it please do but this
is the best we have for now.
"""
def Loading_task(async_task, ID):
    loading_dialog = tk.Toplevel(root)
    loading_dialog.title("Loading...")

    progressbar = ttk.Progressbar(loading_dialog, mode="indeterminate")
    progressbar.pack(padx=20, pady=20)
    progressbar.start()

    # Run your asynchronous task in a separate thread
    threading.Thread(target=run_async_task, args=(loading_dialog, progressbar, async_task, ID)).start()

def stop_progressbar(progressbar, loading_dialog):
    progressbar.stop()
    loading_dialog.destroy()

def run_async_task(loading_dialog, progressbar, async_task, ID):
    global CREATE_SCHEMA_RESULTS
    global ASSESS_SCHEMA_RESULTS
    global CREATE_QUERY_RESULTS

    if(ID == CREATE_SCHEMA_ID):
        CREATE_SCHEMA_RESULTS = async_task()
        #add any state changes to UI here for your use case.
        #If you want things to execute AFTER its stored it must be here.
        #FOR EXAMPLE:
        update_schema_display()
        print(CREATE_SCHEMA_RESULTS)
    if(ID == CREATE_QUERY_ID):
        CREATE_QUERY_RESULTS = async_task()
        #add any state changes to UI here for your use case
        print(f"Generated Query:\n{CREATE_QUERY_RESULTS}")
        display_generated_query.delete(1.0, tk.END)
        display_generated_query.insert(tk.END, CREATE_QUERY_RESULTS)
    if(ID == ASSESS_SCHEMA_ID):
        ASSESS_SCHEMA_RESULTS = async_task()
        display_assessment_result.delete(1.0, tk.END)
        display_assessment_result.insert(tk.END, ASSESS_SCHEMA_RESULTS)
    # Close the loading dialog when the task is complete
    root.after(0, stop_progressbar, progressbar, loading_dialog)

def load_schema_file():
    filename = filedialog.askopenfilename(title="Select a file", filetypes=(("SQL files", "*.sql"), ("all files", "*.*")))
    if filename:
        with open(filename, 'r') as file:
            schema_text_box.delete(1.0, tk.END)
            schema_text_box.insert(tk.END, file.read())

def load_SQL_commands():
    filename = filedialog.askopenfilename(title="Select a file", filetypes=(("SQL files", "*.sql"), ("all files", "*.*")))
    if filename:
        with open(filename, 'r') as file:
            sql_commands_text_box.delete(1.0, tk.END)
            sql_commands_text_box.insert(tk.END, file.read())

# Function to generate query from inputs
def on_generate_query():
    # Debug print to confirm this function is called
    print("Generate Query button clicked.")

    schema = schema_text_box.get("1.0", tk.END).strip()
    query_description = query_description_entry.get().strip()

    # Debug print to check the inputs
    print(f"Schema:\n{schema}")
    print(f"Query Description:\n{query_description}")

    try:
        #generated_query = generate_query(schema, query_description)
        Loading_task(lambda:generate_query(schema, query_description), CREATE_QUERY_ID)

        # print(f"Generated Query:\n{generated_query}")
        # display_generated_query.delete(1.0, tk.END)
        # display_generated_query.insert(tk.END, generated_query)
    except Exception as e:
        # If there's an error, print it out or show it in a message box
        print(f"An error occurred: {e}")

# Generate Query Frame Widgets
schema_text_box_label = ttk.Label(generateQueryFrame, text="DATABASE SCHEMA: ")
schema_text_box = tk.Text(generateQueryFrame, height=10, width=55)
query_description_label = ttk.Label(generateQueryFrame, text="QUERY DESCRIPTION: ")
query_description_entry = ttk.Entry(generateQueryFrame, width=55)
generate_query_button = ttk.Button(generateQueryFrame, text="Generate Query", command=on_generate_query)
load_schema_button = ttk.Button(generateQueryFrame, text="Load Schema from File", command=load_schema_file)
display_generated_query = tk.Text(generateQueryFrame, height=10, width=55)

# Layout for Generate Query Frame
schema_text_box.pack(padx=10, pady=10)
schema_text_box_label.pack(padx=10, pady=10)
load_schema_button.pack(padx=10, pady=10)
query_description_label.pack(padx=10, pady=10)
query_description_entry.pack(padx=10, pady=10)
generate_query_button.pack(padx=10, pady=10)
display_generated_query.pack(padx=10, pady=10)

# Create a new frame for the Assess SQLite Commands tab
assessCommandsFrame = ttk.Frame(notebook)
notebook.add(assessCommandsFrame, text="Assess SQLite Commands")

'''
------------------------------------------------------------
---------ASSESS SQL COMMANDS ~ METHODS & WIDGETS------------
------------------------------------------------------------
'''

def assess_commands():
    # Get the SQL commands from the text box
    sql_commands = sql_commands_text_box.get("1.0", tk.END).strip()

    # Check if the SQL commands are empty
    if not sql_commands:
        assessment_result = "Please enter valid SQL commands."
        display_assessment_result.delete(1.0, tk.END)
        display_assessment_result.insert(tk.END, assessment_result)
    else:
        # Call the assess_SqlCommands function
        Loading_task(lambda:assess_SQLCommands(sql_commands), ASSESS_SCHEMA_ID)

# Label and text box for SQL commands input
load_schema_button_assess = ttk.Button(assessCommandsFrame, text="Load Commands from File", command=load_SQL_commands)
sql_commands_label = ttk.Label(assessCommandsFrame, text="Enter SQL Commands:")
sql_commands_text_box = tk.Text(assessCommandsFrame, height=10, width=55, wrap=tk.WORD)

# Button to trigger the assessment
assess_button = ttk.Button(assessCommandsFrame, text="Assess", command=assess_commands)

# Display box for the assessment result
display_assessment_result = tk.Text(assessCommandsFrame, height=10, width=55, wrap=tk.WORD)

# Scrollbar for the text box and display box
sql_commands_scrollbar = ttk.Scrollbar(assessCommandsFrame, command=sql_commands_text_box.yview)
sql_commands_text_box.config(yscrollcommand=sql_commands_scrollbar.set)

assessment_result_scrollbar = ttk.Scrollbar(assessCommandsFrame, command=display_assessment_result.yview)
display_assessment_result.config(yscrollcommand=assessment_result_scrollbar.set)

# Layout for Assess SQLite Commands Frame
sql_commands_label.pack(padx=10, pady=10)
sql_commands_text_box.pack(padx=10, pady=10)
load_schema_button_assess.pack(padx=10, pady=10)
sql_commands_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

assess_button.pack(padx=10, pady=10)

display_assessment_result.pack(padx=10, pady=10)
assessment_result_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

# Main loop
root.mainloop()


from __future__ import absolute_import
from __future__ import division

import os
import sys
import argparse

__author__ = "Jonathan Madsen"
__copyright__ = "Copyright 2020, The Regents of the University of California"
__credits__ = ["Jonathan Madsen"]
__license__ = "MIT"
__version__ = "@PROJECT_VERSION@"
__maintainer__ = "Jonathan Madsen"
__email__ = "jrmadsen@lbl.gov"
__status__ = "Development"

from .analyze import (
    load,
    match,
    search,
    expression,
    sort,
    group,
    add,
    subtract,
    unify,
    dump,
)

def embedded_analyze(
    args=None,
    data=[],
    call_exit=False,
    verbose=os.environ.get("TIMEMORY_VERBOSE", 0),
    ranks=[],
):
    """This is intended to be called from the embedded python interpreter"""
    if len(ranks) > 0:
        try:
            import mpi4py  # noqa: F401
            from mpi4py import MPI  # noqa: F401

            rank = MPI.COMM_WORLD.Get_rank()
            if rank not in ranks:
                return
        except (ImportError, RuntimeError):
            pass

    call_exit = False
    cmd_line = False
    if args is None:
        args = sys.argv[:]
        call_exit = True
        cmd_line = True

    try:
        parser = argparse.ArgumentParser()
        parser.add_argument(
            "files",
            metavar="file",
            type=str,
            nargs="*" if not cmd_line else "+",
            help="Files to analyze",
            default=[],
        )
        parser.add_argument(
            "-f",
            "--format",
            type=str,
            help="Data output format.",
            choices=(
                "dot",
                "flamegraph",
                "tree",
                "table",
                "markdown",
                "html",
                "markdown_grid",
            ),
            nargs="*",
            default=None,
        )
        parser.add_argument(
            "-o",
            "--output",
            type=str,
            help="Output to file(s)",
            nargs="*",
            default=None,
        )
        parser.add_argument(
            "-M",
            "--mode",
            help="Analysis mode",
            nargs="*",
            choices=("add", "subtract", "unify", "group"),
            default=[],
        )
        parser.add_argument(
            "-m",
            "--metric",
            "--column",
            type=str,
            help="Metric(s) to extract",
            default="sum.*(.inc)$",
        )
        parser.add_argument(
            "-s",
            "--sort",
            type=str,
            help="Sort the metric",
            choices=("ascending", "descending"),
            default=None,
        )
        parser.add_argument(
            "-g",
            "--group",
            type=str,
            help="Group by a column name in the dataframe",
            default=None,
        )
        parser.add_argument(
            "--field",
            type=str,
            help="Dataframe column to search/match against",
            default="name",
        )
        parser.add_argument(
            "--search",
            type=str,
            default=None,
            help="Regular expression for re.search(...), i.e. a substring match",
        )
        parser.add_argument(
            "--match",
            type=str,
            default=None,
            help="Regular expression for re.match(...), i.e. a full string match",
        )
        parser.add_argument(
            "--expression",
            type=str,
            default=None,
            help=(
                "A space-delimited comparison operation expression using 'x' "
                + "for the variable, numerical values, and: < <= > >= && ||. "
                + "E.g. 'x > 1.0e3 && x < 100000'. 'x' will be -m/--metric"
            ),
        )
        parser.add_argument(
            "-e",
            "--echo-dart",
            help="echo Dart measurement for CDash",
            action="store_true",
        )
        parser.add_argument(
            "--per-thread",
            help=(
                "Encode the thread ID in node hash to ensure squashing doesn't "
                + "combine thread-data"
            ),
            action="store_true",
        )
        parser.add_argument(
            "--per-rank",
            help=(
                "Encode the rank ID in node hash to ensure squashing doesn't "
                + "combine rank-data"
            ),
            action="store_true",
        )
        parser.add_argument(
            "--select",
            help=(
                "Select the component type if the JSON input contains output "
                + "from multiple components"
            ),
            type=str,
            default=None,
        )
        parser.add_argument(
            "--exit-on-failure",
            help="Abort with non-zero exit code if errors arise",
            action="store_true",
        )

        _args = parser.parse_args(args)

        if _args.exit_on_failure:
            call_exit = True

        if _args.group is None and _args.format is None:
            _args.format = ["tree"]
        if _args.group is not None and _args.format not in ("table", None):
            raise RuntimeError("Invalid data format for group")

        if isinstance(_args.select, str):
            _args.select = _args.select.split()

        gfs = []
        for itr in data:
            if not isinstance(itr, dict):
                print("data: {}\ndata-type: {}".format(itr, type(itr).__name__))
            gfs.append(
                load(
                    itr,
                    select=_args.select,
                    per_thread=_args.per_thread,
                    per_rank=_args.per_rank,
                )
            )
        for itr in _args.files:
            if not os.path.exists(itr):
                print("file: {}\nfile-type: {}".format(itr, type(itr).__name__))
            gfs.append(
                load(
                    itr,
                    select=_args.select,
                    per_thread=_args.per_thread,
                    per_rank=_args.per_rank,
                )
            )

        def apply(_input, _mode, *_args, **_kwargs):
            if _mode == "search":
                return search(_input, *_args, **_kwargs)
            elif _mode == "match":
                return match(_input, *_args, **_kwargs)
            elif _mode == "expression":
                return expression(_input, *_args, **_kwargs)
            elif _mode == "sort":
                return sort(_input, *_args, **_kwargs)
            elif _mode == "add":
                return add(_input)
            elif _mode == "subtract":
                return subtract(_input)
            elif _mode == "unify":
                return unify(_input)
            elif _mode == "group":
                return group(_input, *_args, **_kwargs)

        # apply search before match since this is less restrictive
        if _args.search is not None:
            gfs = apply(gfs, "search", pattern=_args.search, field=_args.field)

        # apply match after search since this is more restrictive
        if _args.match is not None:
            gfs = apply(gfs, "match", pattern=_args.match, field=_args.field)

        # apply numerical expression last
        if _args.expression is not None:
            gfs = apply(
                gfs,
                "expression",
                math_expr=_args.expression,
                metric=_args.metric,
            )

        # apply the mutating operations
        if "add" in _args.mode:
            gfs = apply(gfs, "add")
        elif "subtract" in _args.mode:
            gfs = apply(gfs, "subtract")
        elif "unify" in _args.mode:
            gfs = apply(gfs, "unify")

        if _args.sort is not None:
            gfs = apply(
                gfs,
                "sort",
                metric=_args.metric,
                ascending=(_args.sort == "ascending"),
            )

        files = _args.output
        if files is not None and len(files) == 1:
            files = files[0]

        if _args.group is not None:
            gfs = apply(
                gfs,
                "group",
                metric=_args.metric,
                field=_args.group,
                ascending=(_args.sort == "ascending"),
            )
            _args.format = [None]

        for fmt in _args.format:
            dump(gfs, _args.metric, fmt, files, _args.echo_dart)

    except Exception as e:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        import traceback

        traceback.print_exception(exc_type, exc_value, exc_traceback, limit=10)
        print("Exception - {}".format(e))
        if call_exit or _args.exit_on_failure:
            sys.exit(1)
        elif not cmd_line:
            raise


from sklearn import datasets, model_selection, metrics
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression

iris = datasets.load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3, shuffle=True, random_state=0)

clf = LogisticRegression(random_state=0, solver="lbfgs", multi_class="multinomial")
clf.fit(X_train, y_train)

joblib.dump(clf, "iris_regression_clf.joblib")

y_pred = clf.predict(X_test)
print(metrics.accuracy_score(y_test, y_pred))


import numpy as np
import matplotlib.pyplot as plt

def plot_sun(sunposition, d):
    '''

    '''
    fig = plt.figure(figsize=d['figsize'])

    tks = [np.deg2rad(a) for a in np.linspace(0,360,8,endpoint=False)]
    xlbls = np.array(['N','45','E','135','S','225','W','315'])

    ax = fig.add_subplot(111, projection='polar')
    ax.set_theta_zero_location('N')
    ax.set_xticks((tks))
    ax.set_xticklabels(xlbls, rotation="vertical", size=12)
    ax.tick_params(axis='x', pad=0.5)
    ax.set_theta_direction(-1)
    ax.set_rmin(0)
    ax.set_rmax(90)
    ax.set_rlabel_position(90)
    ax.set_title('Sun Position')

    xs = np.deg2rad(sunposition[0,:])
    ys = 90 - sunposition[1,:]

    ax.scatter(xs, ys, s=10, c='orange', alpha=0.5)

    plt.subplots_adjust(top=d['top'], bottom=d['bottom'],
                        left=d['left'], right=d['right'])
    plt.close()
    return fig


# coding: utf-8

# 9를 가지고 있는 n번째 숫자 제곱하

# 입력받아 리스트형태로 저장하기
N = int(input())
num_string = input()
num_list = num_string.split(' ')

# 9가 아닌 리스트 새로 생성
not_9_list = [int(num) for num in num_list if '9' not in num]

# 잘못된 입력값일 경우 처리 (1 9가 없는 숫자가 없는 경우, 2. N번째 숫자가 없는 경우)
if len(not_9_list) == 0 or len(not_9_list) < N:
    print(999999999)
else:
    print(not_9_list[N-1] * not_9_list[N-1])


with open('day14_in.txt') as f:
    lines = f.readlines()

points = [[[int(x) for x in coord.split(',')] for coord in line.split(' -> ')] for line in lines]
rock_lines = []
rocks = set()
for l in points:
    rock_line = []
    for c1,c2 in zip(l[:-1], l[1:]):
        x1 = c1[0]
        x2 = c2[0]
        y1 = c1[1]
        y2 = c2[1]
        if x1 == x2:
            sign = int((y2 - y1) / abs(y2 - y1))
            new_rocks = [(x1, y) for y in range(y1, y2+sign, sign)]
        elif y1 == y2:
            sign = int((x2 - x1) / abs(x2 - x1))
            new_rocks = [(x, y1) for x in range(x1, x2+sign, sign)]
        else:
            raise Exception(f"Not a straight line? c1: {c1}, c2: {c2}")
        rock_line.append(new_rocks)
        rocks.update(new_rocks)
    rock_lines.append(rock_line)

# print(rock_lines[0])
# print(rocks)

def p1():

    max_y = max([c[1] for c in rocks])
    sand = set()
    done = False
    while(not done):
        # new sand
        x = 500
        for y in range(max_y+1):
            if y == max_y:
                done = True
                break
            elif (x,y+1) not in rocks and (x,y+1) not in sand:
                # Nothing below
                pass
            elif (x-1,y+1) not in rocks and (x-1,y+1) not in sand:
                # Nothing down left
                x -= 1
            elif (x+1,y+1) not in rocks and (x+1,y+1) not in sand:
                # Nothing down right
                x += 1
            else:
                # stop here
                sand.add((x,y))
                break

    print(len(sand))

# I originally tried checking in a union of rocks and sand, but it seems it must use new memory for
#       the union instead of just checking in both. Makes sense in retrospect. for sets of 250,000,
#       union takes 20 ms while below takes 35 ns
def p2():
    max_y = max([c[1] for c in rocks])
    floor = max_y + 2
    sand = set()
    done = False
    # i = 0
    while(not done):
        # new sand
        x = 500
        # print('*', end='')
        for y in range(floor + 1):
            if y+1 == floor:
                #stop
                sand.add((x,y))
                break
            elif (x,y+1) not in rocks and (x,y+1) not in sand:
                # Nothing below
                pass
            elif (x-1,y+1) not in rocks and (x-1,y+1) not in sand:
                # Nothing down left
                x -= 1
            elif (x+1,y+1) not in rocks and (x+1,y+1) not in sand:
                # Nothing down right
                x += 1
            else:
                # stop here
                sand.add((x,y))
                if (x,y) == (500,0):
                    done = True
                break
        # if i % 100 == 0:
        #     print(i)
        # i += 1
    print(len(sand))

# p1()
p2()


# nosetests --nocapture  tests/test_field.py
import os
import unittest

from fit_tool.fit_file import FitFile

class TestActivityFiles(unittest.TestCase):

    def setUp(self):
        super().setUp()
        # stream_handler = logging.StreamHandler(sys.stdout)
        # stream_handler.setFormatter(formatter)
        # logger.addHandler(stream_handler)

    def shortDescription(self):
        return None

    def test_decode_deprecated_activity(self):
        """Test decoding and activity with deprecated messages and fields.
        """
        path = os.path.join(os.path.dirname(__file__), 'data/activity_deprecated_profile.fit')

        with open(path, 'rb') as file_object:
            bytes_buffer = file_object.read()
            fit_file = FitFile.from_bytes(bytes_buffer)
            print(f'Profile version: {fit_file.header.profile_version}')
            fit_file.to_rows()

    def test_decode_multiple_developer_index(self):
        """Test decoding activity with multiple developer indexes
        """
        path = os.path.join(os.path.dirname(__file__), 'data/activity_multiple_developer_index.fit')

        with open(path, 'rb') as file_object:
            bytes_buffer = file_object.read()
            fit_file = FitFile.from_bytes(bytes_buffer)
            print(f'Profile version: {fit_file.header.profile_version}')
            fit_file.to_rows()


from BaseSolver import BaseSolver
from random import randrange
import math

# Time complexity: O(N)
# Space complexity: O(1)
def getRouteCost(route):
    cost = 0
    routeLength = len(route)

    for i in range(1, routeLength):
        previous = route[i - 1]
        current = route[i]
        cost += previous.costTo(current)

    cost += route[routeLength - 1].costTo(route[0])
    return cost

class GreedySolver(BaseSolver):
    def __init__(self, tspSolver, maxTime):
        super().__init__(tspSolver, maxTime)

    # Time complexity: A for loop (N) with (N^2) on each iteration -> O(N^3)
    # Space complexity: O(N)
    def run(self):
        defaultResults = self.getTSPSolver().defaultRandomTour()
        route = defaultResults['soln'].route
        self.setBSSFFromRoute(route)
        bestCost = self.getBSSFCost()

        startIndex = randrange(self.getCityCount())

        for i in self.getCityRange():
            if self.exceededMaxTime():
                return

            original = (startIndex + i) % self.getCityCount()
            current = original
            visited = {current}
            route = [self.getCityAt(current)]

            solution = self.greedySolve(original, current, visited, route)
            if solution is None:
                continue

            solutionCost = getRouteCost(solution)
            if solutionCost < bestCost:
                self.setBSSFFromRoute(solution)
                self.incrementSolutionCount()
                bestCost = solutionCost

    # Time complexity: Recurse at most O(N) times each being O(N) -> O(N^2)
    # Space complexity: Set and route arrays up to 2N -> O(N)
    def greedySolve(self, original, current, visited, route):
        if len(visited) == self.getCityCount():
            originalCity = self.getCityAt(original)
            costToOriginal = self.getCityAt(current).costTo(originalCity)
            return None if costToOriginal == math.inf else route

        target = self.getNextCity(current, visited)
        if target is None:
            return None
        else:
            visited.add(target)
            route.append(self.getCityAt(target))
            return self.greedySolve(original, target, visited, route)

    # Time complexity: O(N)
    # Space complexity: O(1)
    def getNextCity(self, source, visited):
        minCost = math.inf
        minIndex = None

        for i in self.getCityRange():
            if i in visited:
                continue

            target = self.getCityAt(i)
            costToCity = self.getCityAt(source).costTo(target)
            if costToCity < minCost:
                minCost = costToCity
                minIndex = i

        return minIndex


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

# # 1. Character Input
#
# <i>Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.</i>

# In[1]:

name = input("Enter your Name: ")
age = int(input("Enter your age: "))
year = 2022 - age + 100
print(name + ", you will be 100 years old in the year " + str(year))

# In[ ]:


'''
Created on 26 Jul 2017

@author: zhi liang
'''

# current stamp duty tiers
STAMP_DUTY_TIER_ONE = 180000
STAMP_DUTY_TIER_TWO = 360000
ONE_PERCENT = 0.01
TWO_PERCENT = 0.02
THREE_PERCENT = 0.03

def calculateStampDuty(propertyPrice):
    if (propertyPrice <= STAMP_DUTY_TIER_ONE):
        return ONE_PERCENT * propertyPrice
    elif (propertyPrice <= STAMP_DUTY_TIER_TWO):
        return TWO_PERCENT * (propertyPrice - STAMP_DUTY_TIER_ONE) + calculateStampDuty(STAMP_DUTY_TIER_ONE)
    else:
        return THREE_PERCENT * (propertyPrice - STAMP_DUTY_TIER_TWO) + calculateStampDuty(STAMP_DUTY_TIER_TWO)

def getCPFToTopUp(excessCPFUnused, initialPP, stampDuty):
    ERROR_MARGIN = 1
    CPFToBeChanneled = 0.5 * excessCPFUnused
    difference = abs((calculateStampDuty(initialPP + CPFToBeChanneled) - stampDuty) - (excessCPFUnused - CPFToBeChanneled))
    while (difference > ERROR_MARGIN):
        CPFToBeChanneled = CPFToBeChanneled + 1
        difference = abs((calculateStampDuty(initialPP + CPFToBeChanneled) - stampDuty) - (excessCPFUnused - CPFToBeChanneled))
    return CPFToBeChanneled


from PySimpleGUI import PySimpleGUI as sg
import pyautogui as ag
from time import sleep

def automatic(nf, frota, fornecedor, responsavel, saude, educacao, outros, viagemSim, qtd, data, preco, obs, km):
    if (viagemSim == True):
        precoViagem = sg.popup_get_text('Preço da viagem: ')

    # saindo do automatizador e indo para o programa
    sleep(2)
    ag.keyDown('alt')
    ag.press('tab', presses=2)
    ag.keyUp('alt')

    # clicando no inserir e iniciando novo lançamento
    sleep(2)
    ag.click(x=137, y=170)
    sleep(3)
    ag.press('tab', presses=3)
    ag.press('enter')
    ag.press('down')
    ag.press('enter')

    # Colocando informanções para o lançamento
    sleep(3)
    ag.write(data)
    sleep(1)
    # virificando departamento e viagem
    if (saude == True):
        if (viagemSim == True):
            nf = f'sf{nf}'
        else:
            nf = f'S{nf}'
    elif (educacao == True):
        if (viagemSim == True):
            nf = f'ef{nf}'
        else:
            nf = f'E{nf}'
    elif (outros == True):
        if (viagemSim == True):
            nf = f'of{nf}'
        else:
            nf = f'O0{nf}'

    ag.click(x=713, y=256)
    ag.click(x=574, y=257)
    ag.write(nf)
    sleep(2)
    ag.press('tab', presses=3)
    ag.write(fornecedor)
    ag.press('tab', presses=2)
    sleep(2)
    ag.write(frota)
    sleep(2)

    # mensagem para confirmar motorista e veiculo para proseguir
    # Shows Yes and No buttons
    msg = sg.popup_yes_no(
        'Confirme o veiculo e selecione o motorista e clique em Yes')
    # se for Yes o programa continua
    if (msg == 'Yes'):
        sleep(1)
        ag.keyDown('alt')
        ag.press('tab')
        ag.keyUp('alt')
        sleep(1)

        ag.press('tab')
        ag.hotkey('ctrl', 'a')
        ag.write(responsavel)
        sleep(1)
        ag.press('enter', presses=2)

        # entra na parte de produtos
        sleep(2)
        ag.write(qtd)
        ag.press('tab')

        if (viagemSim == True):
            ag.write(precoViagem)
        else:
            ag.write(preco)

        # se for motossera
        if (frota == 'motosse'):
            ag.press('tab', presses=8)
            sleep(1)
            ag.write(obs)
            sleep(1)
            #sg.click(x=1057, y=948)
            print('moto')
            return

        # se tiver com KM quebrado
        if (km == 'Q' or km == 'q'):
            ag.press('tab', presses=8)
            sleep(1)
            ag.write('KM quebrado')
            #sg.click(x=1057, y=948)
            print('quebrado')

        # se tiver com ND KM
        if (km == 'ND' or km == 'nd'):
            ag.press('tab', presses=8)
            sleep(1)
            ag.write('ND KM')
            #sg.click(x=1057, y=948)
            print('nd km')

        # se for de viagem
        if (viagemSim == True):
            ag.press('tab', presses=6)
            ag.write(km)
            sleep(1)
            ag.press('tab', presses=2)
            ag.write(obs)
            #sg.click(x=1057, y=948)
            print('viagem')

        if (viagemSim == False):
            ag.press('tab', presses=6)
            ag.write(km)
            sleep(1)
            #sg.click(x=1057, y=948)
            print('sem viagem')

    else:  # se não ele para o programa
        sg.WIN_CLOSED

class screenCadastroNF:
    def __init__(self):
        sg.theme('DarkBlue12')
        layout = [
            [sg.Column([[sg.Text('', size=(3, 0))]]),
             sg.Image(filename='logo.png')],
            [sg.Text('Data', size=(10, 0)), sg.Input(size=(15, 0), key='data'),
             sg.Text('Observação', size=(10, 0)), sg.Input(
                 size=(15, 0), key='obs'),
             sg.Text('Odometro', size=(10, 0)), sg.Input(size=(15, 0), key='km')],
            [sg.Text('Nota Fiscal', size=(10, 0)), sg.Input(size=(15, 0), key='nf'),
             sg.Text('Frota', size=(10, 0)), sg.Input(
                 size=(15, 0), key='frota'),
             sg.Text('Quantidade', size=(10, 0)), sg.Input(size=(15, 0), key='qtd')],
            [sg.Text('Fornecedor', size=(10, 0)), sg.Input(size=(15, 0), key='fornecedor'),
             sg.Text('Responsavel', size=(10, 0)), sg.Input(
                 size=(15, 0), key='responsavel')],

            [sg.Text('Preço')],
            [sg.Radio('Gasolina', 'pr', key='gas'), sg.Radio(
                'Etanol', 'pr',  key='eta'), sg.Radio('Diesil Comum', 'pr', key='dc'),
             sg.Radio('Diesil S-10', 'pr', key='ds')],

            [sg.Text('Viagem?')],
            [sg.Text('Qual Departamento?')],
            [sg.Radio('Saude', 'dep', key='saude'), sg.Radio(
                'Educação', 'dep',  key='educação'), sg.Radio('Outros', 'dep', key='outros')],

            [sg.Text('Viagem?')],
            [sg.Radio('Sim', 'Viagem', key='viagem'), sg.Radio(
                'Nao', 'Viagem', key='naoViagem')],

            [sg.Button('Enviar dados')],
            [sg.Button('Sair')],

        ]
        self.janela = sg.Window('Lançamento de NFs').layout(layout)

    def iniciarAuto(self):
        while True:
            self.button, self.values = self.janela.Read()

            if self.button == "Sair":
                sg.WIN_CLOSED
                break

            if self.button == "Enviar dados":
                data = self.values['data']
                km = self.values['km']
                obs = self.values['obs']
                nf = self.values['nf']
                frota = self.values['frota']
                qtd = self.values['qtd']
                fornecedor = self.values['fornecedor']
                responsavel = self.values['responsavel']
                saude = self.values['saude']
                educacao = self.values['educação']
                outros = self.values['outros']
                viagemSim = self.values['viagem']

                gas = self.values['gas']
                eta = self.values['eta']
                dc = self.values['dc']
                ds = self.values['ds']

                if (gas == True):
                    preco = '5,05'
                elif (eta == True):
                    preco = '3,62'
                elif (dc == True):
                    preco = '6,87'
                elif (ds == True):
                    preco = '6,97'

                automatic(nf, frota, fornecedor, responsavel, saude, educacao, outros, viagemSim, qtd, data, preco, obs, km)

tela = screenCadastroNF()
tela.iniciarAuto()


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import preprocessing

# odczyt danych
film_data = pd.read_csv('MoviesOnStreamingPlatforms_updated.csv')

# Czyszczenie wierszy z pustymi warościami.
film_data.dropna(inplace=True)

# Usunięcie zbędnych kolumn
film_data.drop(film_data.columns[[0, 1]], axis = 1)

# Normalizacja: Lowercase dla danych tekstowych, standaryzacja (0..1) dla wartości float, sortowanie danych w komórce.

for col_name in ['Title', 'Directors', 'Genres', 'Country', 'Language']:
    film_data[col_name] = film_data[col_name].str.lower()

for col_name in ['Directors', 'Genres', 'Country', 'Language']:
    film_data[col_name] = film_data[col_name].str.split(',').map(lambda x: ','.join(sorted(x)))

scaler = preprocessing.MinMaxScaler()
film_data[['IMDb', 'Runtime']] = scaler.fit_transform(film_data[['IMDb', 'Runtime']])

# Podział zbioru na train, dev, test w proporcji 8:1:1
train_ratio = 0.8
validation_ratio = 0.1
test_ratio = 0.1

film_train, film_test = train_test_split(film_data, test_size=1 - train_ratio)

film_valid, film_test = train_test_split(film_test, test_size=test_ratio/(test_ratio + validation_ratio))

film_train.to_csv('MoviesOnStreamingPlatforms_updated.train')
film_test.to_csv('MoviesOnStreamingPlatforms_updated.test')
film_valid.to_csv('MoviesOnStreamingPlatforms_updated.dev')


import os
import re
from typing import TypeVar, Optional
from argparse import ArgumentParser, Namespace
from urllib.request import urlopen
from pathlib import Path
from hashlib import sha256

ARTIFACTS = [
    "opa_darwin_amd64",
    "opa_darwin_arm64_static",
    "opa_linux_amd64",
    "opa_linux_amd64_static",
    "opa_linux_arm64_static",
    "opa_windows_amd64",
]

T = TypeVar('T')

def required(value: Optional[T], name: str) -> T:
    if value is None:
        raise ValueError(f"Expected value for {name}")

    return value

def get_sha256(artifact_name: str, version: str) -> str:
    ext = ".exe" if "windows" in artifact_name else ""

    with urlopen(f"https://github.com/open-policy-agent/opa/releases/download/v{version}/{artifact_name}{ext}.sha256") as res:
        return res.read().decode().split(' ')[0]

def get_sha256_file(file: str, version: str) -> str:
    file_url = f"https://raw.githubusercontent.com/open-policy-agent/opa/v{version}/{file}"

    with urlopen(file_url) as res:
        return sha256(res.read()).hexdigest()

def main(args: Namespace):
    d = dict([
        (artifact_name, get_sha256(artifact_name, args.version))
        for artifact_name in ARTIFACTS
    ])

    bzl_path = WORKSPACE_ROOT.joinpath(
        'opa', 'private', 'opa_rules_dependencies.bzl')

    with open(bzl_path) as bzl_file:
        bzl_content = bzl_file.read()

    version_match = re.search(
        r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']", bzl_content)

    if version_match is None:
        raise ValueError(f"Could not find DEFAULT_VERSION in file {bzl_path}")

    start_version = version_match.start(1)
    end_version = version_match.end(1)

    bzl_content = bzl_content[:start_version] + \
        args.version + bzl_content[end_version:]

    dict_match = re.search(
        r"^_OPA_SHA256\s*=\s*{\s*$", bzl_content, re.MULTILINE)

    if dict_match is None:
        raise ValueError(f"Could not find _OPA_SHA256 in file {bzl_path}")

    bzl_content = bzl_content[:dict_match.end(
    )] + f"\n    \"{args.version}\": {{\n" + '\n'.join([
        f"        \"{artifact_name}\": \"{sha256}\","
        for artifact_name, sha256 in d.items()
    ] + [
        f"        \"opa_capabilities_json\": \"{get_sha256_file('capabilities.json', args.version)}\",",
        f"        \"opa_builtin_metadata_json\": \"{get_sha256_file('builtin_metadata.json', args.version)}\",",
    ]) + "\n    }," + bzl_content[dict_match.end():]

    with open(bzl_path, "w", encoding="utf-8") as bzl_file:
        bzl_file.write(bzl_content)

def get_workspace_root(wd: Path) -> Path:
    while not wd.joinpath("WORKSPACE").exists():
        wd = wd.parent

    return wd

BUILD_WORKING_DIRECTORY = Path(
    required(os.getenv("BUILD_WORKING_DIRECTORY"), "BUILD_WORKING_DIRECTORY"))
WORKSPACE_ROOT = get_workspace_root(BUILD_WORKING_DIRECTORY)

if __name__ == '__main__':

    parser = ArgumentParser()
    parser.add_argument('--version', '-v', required=True)

    main(parser.parse_args())


import unittest

from functions.decrypt import Decrypt
from functions.encrypt import Encrypt

class TestEncryptDecrypt(unittest.TestCase):

    def test_encrypt_function(self):
        text = 'Get this message to the main server'
        expected = 'trg guvf zrffntr gb gur znva freire'
        shift = 13

        result = Encrypt.encrypt(text, shift)
        self.assertTrue(result, expected)
        self.assertNotEqual(result, 'wrong text')
        self.assertEqual(result, expected)

    def test_decrypt_function(self):
        text = 'trg guvf zrffntr gb gur znva freire'
        expected = 'Get this message to the main server'
        shift = 13

        result = Decrypt.decrypt(text, shift)
        self.assertTrue(result, expected)
        self.assertNotEqual(result, 'wng dggf')
        self.assertEqual(result, expected)
        self.assertFalse(result, 'rhdhj dhh')

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


"""
Vasya's classmates came to visit. His mother decided to treat the boys with cookies.
But it's not that easy. Cookies can be different sizes.
And each child has a greedy factor - the minimum size of a cookie he will take.
You have to figure out how many kids will be satisfied at best when they act optimally.
Each child can take no more than one cookie.
"""

def solution(children, cookies):
    children.sort()
    cookies.sort()

    i = len(children) - 1
    j = len(cookies) - 1
    count = 0

    while j >= 0 and i >= 0:
        if cookies[j] >= children[i]:
            cookies.pop(j)
            children.pop(i)
            j -= 1
            count += 1
        i -= 1
    return count

num_children = input()
children = list(map(int, (input().split())))
num_cookies = input()
cookies = list(map(int, (input().split())))

print(solution(children, cookies))


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

# ########################################################################### #
# #                   Nao Challenge 2014 Main Program                       # #
# ########################################################################### #
# # File: ihm.py                                                            # #
# ########################################################################### #
# # Creation:   2014-02-04                                                  # #
# #                                                                         # #
# # Team:       IUT de Cachan                                               # #
# #                                                                         # #
# # Authors:    Nicolas SENAUD                                              # #
# #             Pierre-Guillaume LEGUAY                                     # #
# #             Nicolas SAREMBAUD                                           # #
# #                                                                         # #
# ########################################################################### #

import sys
import time
import numpy as np                  # Numpy:  Maths library.
#import cv2                          # OpenCV: Visual recognition library.
#import vision_definitions           # Image definitions macros.

from optparse import OptionParser   # Parser to keep connexion with Nao.

import logs

# Nao's libraries.
from naoqi import ALProxy
from naoqi import ALBroker
from naoqi import ALModule

# ###################### CLASS TO HAVE NAO'S SPEAKING ####################### #

class NaoSpeak(ALModule):
    """docstring for NaoSpeak"""
    def __init__(self, name, message):

        ALModule.__init__(self, name)

        self.message = message

        self.logs = logs.logs()
        self.tts = ALProxy("ALTextToSpeech")
        self.logs.display("Subscribed to an ALTextToSpeech proxy",
                          "Good")

    def say(self, message, volume=0.3, language='french'):
        self.tts.setLanguage(language)
        self.tts.setVolume(volume)
        self.tts.say(message)

# ############################### END OF CLASS ############################## #

# ####################### CLASS TO CONTROL NAO'S LED ######################## #

class NaoLeds(ALModule):
    """docstring for NaoLedsOn"""
    def __init__(self, name, state):
        ALModule.__init__(self, name)

        self.state = state

        self.leds = ALProxy("ALLeds")
        self.logs.display("Subscribed to an ALLeds proxy",
                          "Good")

# ############################### END OF CLASS ############################## #


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

# In[2]:

import networkx as nx
import pandas as pd

import matplotlib.pyplot as plt
df = pd.read_excel('Dataset.xlsx', index = None)
df.head()
G=nx.from_pandas_edgelist(df, 'Departure Station', 'Arrival Station','Time of Travel')
nx.draw(G, with_labels=False)
df['Route'] = df['Departure Station'].map(str) + ' - ' + df['Arrival Station'].map(str)
df.head()
df2 = df[['Route','Tube Line', 'Time of Travel']]
df3=df2.drop_duplicates()
df3['Route Reversed'] = df['Arrival Station'].map(str) + ' - ' + df['Departure Station'].map(str)
df6 = df3[['Route Reversed', 'Tube Line', 'Time of Travel']]
df6.rename(columns = {'Route Reversed' : 'Route'}, inplace = True)
df3 = df3.append(df6, ignore_index = True)
del df3['Route Reversed']

def route_calculator(source,target):

    d, p = nx.single_source_dijkstra(G,source=source, target=target, weight='Time of Travel')
    route=[]
    for i in range(len(p)-1):
        route.append(str(p[i]+" - "+p[i+1]))
    df4 = pd.DataFrame(route, columns=["Route"])
    df5 = pd.merge(df4,df3,"left", left_on="Route", right_on="Route")
    df5.to_excel('Result.xlsx')

    print(df5)
    print("Total time for Travel", d+ len(p) -2 , "Mins (Includes 1 min wait time at each station :))")


import argparse
from pathlib import Path
import numpy as np

from localisation import run_convergence_localisation_on_file, run_localisation_on_file
import Models.models as models
import Utils.constants as const
from plotting import parameter_plot, comparison_plot, plot_evaluation_metric

def rmse(predictions: dict):
    """
    Returns the mean root mean square error for each algorithm
    """
    rmse_results = {}

    for algorithm, predictions in predictions.items():
        values = [np.linalg.norm(actual - prediction)
                  ** 2 for actual, prediction in predictions]
        rmse_results[algorithm] = np.sqrt(np.mean(values))

    return rmse_results

def mae(predictions: dict):
    """
    Returns the Mean Absolute error for each algorithm
    """
    mae_results = {}

    for algorithm, predictions in predictions.items():
        values = [np.linalg.norm(actual - prediction)
                  for actual, prediction in predictions]
        mae_results[algorithm] = np.sqrt(np.mean(values))

    return mae_results

def mae_confidence_interval(predictions: dict):
    """
    calculates 95% confidence interval
    """

    mae_conf_results = {}

    samples = len(predictions)
    for algorithm, predictions in predictions.items():
        dist = [np.linalg.norm(actual - prediction)
                for actual, prediction in predictions]
        mae = np.mean(dist)
        std = np.std(dist)
        confidence_interval = 1.960 * std / np.sqrt(len(dist))
        mae_conf_results[algorithm] = np.round([mae, confidence_interval], 2)

    return mae_conf_results

def std(predictions: dict):
    std_results = {}

    samples = len(predictions)
    for algorithm, predictions in predictions.items():
        dist = [np.linalg.norm(actual - prediction)
                for actual, prediction in predictions]
        std_results[algorithm] = np.round(np.std(dist), 2)

    return std_results

def rmse_confidence_interval(predictions:dict):
    """
    calculates 95% confidence interval
    """

    mae_conf_results = {}

    samples = len(predictions)
    for algorithm, predictions in predictions.items():
        dist = [np.linalg.norm(actual - prediction) **
                2 for actual, prediction in predictions]
        rmse = np.sqrt(np.mean(dist))
        std = np.std(dist)
        confidence_interval = 1.960 * std / np.sqrt(len(dist))
        mae_conf_results[algorithm] = np.round([rmse, confidence_interval], 2)

    return mae_conf_results

def initialise_localisation_model(model: const.Model, training_data_filepath: Path, filter: bool = False, prior: const.Prior = None):

    if model is const.Model.GAUSSIAN:
        return models.GaussianProcessModel(training_data_filepath, prior=prior, cell_size=0.25, filter=filter)
    elif model is const.Model.GAUSSIANKNN:
        return models.GaussianKNNModel(training_data_filepath, prior=prior, cell_size=0.25, filter=filter)
    elif model is const.Model.GAUSSIANMINMAX:
        return models.GaussianMinMaxModel(training_data_filepath, prior=prior, cell_size=0.25, filter=filter)
    elif model is const.Model.KNN:
        return models.KNN(training_data_filepath,)
    elif model is const.Model.WKNN:
        return models.WKNN(training_data_filepath, filter=filter)
    elif model is const.Model.PROPOGATION:
        return models.PropagationModel(training_data_filepath, const.PROPAGATION_CONSTANT, filter=filter)
    elif model is const.Model.PROXIMITY:
        return models.ProximityModel(training_data_filepath)
    else:
        raise ValueError("Not a supported model")

def get_localisation_predictions(models: list, training_data_filepath: Path, evaluation_data_filepath: Path,filtering:bool, prior: const.Prior):

    models = {model.value: initialise_localisation_model(
        model, training_data_filepath, filtering, prior) for model in models}

    if prior is const.Prior.LOCAL:
        predictions = {name: run_convergence_localisation_on_file(
            evaluation_data_filepath, model, filtering) for name, model in models.items()}
    elif prior is const.Prior.UNIFORM:
        predictions = {name: run_localisation_on_file(
            evaluation_data_filepath, model, filtering) for name, model in models.items()}

    predictions = dict(sorted(predictions.items(), key=lambda x: x[0]))

    return predictions

def run_filter_comparison(models: list, training_data_filepath: Path, evaluation_data_filepath: Path, prior: const.Prior):

    filtered = get_localisation_predictions(
        models, training_data_filepath, evaluation_data_filepath, True, prior)
    non_filtered = get_localisation_predictions(
        models, training_data_filepath, evaluation_data_filepath, False, prior)

    return {"Filtered": filtered, "Non-Filtered": non_filtered}

def predict_all_models(training_data_filepath: Path, evaluation_data_filepath: Path, prior: const.Prior, filtering: bool):
    """Gets position predictions for all the evaluation data
    """
    models = [model for model in const.Model]
    return get_localisation_predictions(models, training_data_filepath, evaluation_data_filepath, prior, filtering)

def filter_all_models_plot(training_data_filepath: Path, evaluation_data_filepath: Path):
    filtered_predictions = predict_all_models(
        training_data_filepath, evaluation_data_filepath, True, const.Prior.UNIFORM)
    non_filtered_predictions = predict_all_models(
        training_data_filepath, evaluation_data_filepath, False, const.Prior.UNIFORM)

    filtered_results = mae_confidence_interval(filtered_predictions)
    non_filtered_results = mae_confidence_interval(non_filtered_predictions)

    print("algorithm      :  filtered     :  non_filtered")
    for i, algorithm in enumerate(filtered_results.keys()):
        print(
            f"{algorithm:15}: {filtered_results[algorithm][0]:5} ± {filtered_results[algorithm][1]:5} : {non_filtered_results[algorithm][0]:5} ± {non_filtered_results[algorithm][1]:5}")
    comparison_plot(filtered_results, non_filtered_results, "filter")

def prior_all_models_plot(training_data_filepath: Path, evaluation_data_filepath: Path):
    uniform_predictions = predict_all_models(
        training_data_filepath, evaluation_data_filepath, True, const.Prior.UNIFORM)
    local_predictions = predict_all_models(
        training_data_filepath, evaluation_data_filepath, True, const.Prior.LOCAL)

    uniform_results = mae_confidence_interval(uniform_predictions)
    local_results = mae_confidence_interval(local_predictions)

    print("algorithm      :  uniform     :  local")
    for i, algorithm in enumerate(uniform_results.keys()):
        print(
            f"{algorithm:15}: {uniform_results[algorithm][0]:5} ± {uniform_results[algorithm][1]:5} : {local_results[algorithm][0]:5} ± {local_results[algorithm][1]:5}")
    comparison_plot(local_results, uniform_results, "prior")

def evaluation_metric_plot(training_data_filepath: Path, evaluation_data_filepath: Path):
    """
    Creates the evaluation metric table/plots for rmse and mae
    Filtering is set to true and a uniform prior is used
    """

    predictions = predict_all_models(
        training_data_filepath, evaluation_data_filepath, True, const.Prior.UNIFORM)

    mae = mae_confidence_interval(predictions)
    rmse = rmse_confidence_interval(predictions)
    stds = std(predictions)

    print("algorithm        :  mae        :  std  :  rmse ")
    for i, algorithm in enumerate(predictions.keys()):
        print(
            f"{algorithm:15}: {mae[algorithm][0]:5} ± {mae[algorithm][1]:5} : {stds[algorithm]:5} : {rmse[algorithm][0]:5} ± {rmse[algorithm][1]:5}")

    plot_evaluation_metric(mae, "mae")
    plot_evaluation_metric(rmse, "rmse")

def cellsize_plot(training_data_filepath: Path, evaluation_data_filepath: Path):
    """ Creates the plot which compares the evaluation metric mean average error against different cell sizes
    """

    cell_sizes = (2 ** np.arange(0, 6)) / 4
    cell_sizes = np.arange(0.2, 4, step=0.1)
    gaussian_models = {size: models.GaussianProcessModel(
        training_data_filepath, prior=const.Prior.UNIFORM, cell_size=size, filter=True) for size in cell_sizes}
    gaussian_predictions = {name: run_localisation_on_file(
        evaluation_data_filepath, model, False) for name, model in gaussian_models.items()}
    gaussian_mae = mae_confidence_interval(gaussian_predictions)
    # plot_evaluation_metric(gaussian_mae,"mae,cell_size")

    print("cell_size     :  mae    ")
    for i, cell_size in enumerate(gaussian_predictions.keys()):
        print(
            f"{round(cell_size,2):15}: {gaussian_mae[cell_size][0]:5} ± {gaussian_mae[cell_size][1]:5}")

    parameter_plot(gaussian_mae,"cell_size")

def wk_comparison(training_data_filepath: Path, evaluation_data_filepath: Path):

    wknn_models = {k: models.WKNN(training_data_filepath, filter=True,k=k) for k in range(1,10)}

    predictions = {name: run_localisation_on_file(
        evaluation_data_filepath, model, False) for name, model in wknn_models.items()}
    mae = mae_confidence_interval(predictions)

    print("K     :  mae    ")
    for i, k in enumerate(predictions.keys()):
        print(
            f"{k:5}: {mae[k][0]:5} ± {mae[k][1]:5}")

    parameter_plot(mae,"k")

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("mode", help="Evaluation Wanted")
    parser.add_argument(
        "training_file", help="The file with the training data in it.")
    parser.add_argument("evaluation_file",
                        help="The file with the evaluation data in it.")
    args = parser.parse_args()

    modes = ["eval", "all", "prior", "filter", "cell_size","wk"]
    if args.mode not in modes:
        print("Mode should be in " + ",".join(modes))

    else:
        training_filepath = Path(args.training_file)
        evaluation_filepath = Path(args.evaluation_file)
        if args.mode == "eval":
            evaluation_metric_plot(training_filepath, evaluation_filepath)
        if args.mode == "all":
            evaluation_metric_plot(training_filepath, evaluation_filepath)
            prior_all_models_plot(training_filepath, evaluation_filepath)
            filter_all_models_plot(training_filepath, evaluation_filepath)
        elif args.mode == "prior":
            prior_all_models_plot(training_filepath, evaluation_filepath)
        elif args.mode == "filter":
            filter_all_models_plot(training_filepath, evaluation_filepath)
        elif args.mode == "cell_size":
            cellsize_plot(training_filepath, evaluation_filepath)
        elif args.mode == "wk":
            wk_comparison(training_filepath, evaluation_filepath)

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

# https://www.hackerrank.com/challenges/incorrect-regex

import re

if __name__ == '__main__':
    t = int(input())
    for i in range(t):
        s = input()
        try:
            x = re.compile(s)
            print("True")
        except:
            print("False")


'''
filters.py
Roman Schiffino 151B Fall Semester
This is one of the main project files. It does what the lab asks it to do.
I made a bunch of filters because I was just testing all the options. I finally
settled on three of these for the final warholl image. I use the matrix class to
keep track of the color at each pixel on the canvas. I then make an image from this data and save it as warhol.ppm.
'''
import graphics as gr
import display as d
import tkinter as tk
import sys as s
import math as m
from matrix import *

def hexColor(tupColor):
    '''
    Converts (R,G,B) to Hex.
    '''
    hexColor = '#{:02x}{:02x}{:02x}'.format(*tupColor)
    return hexColor

def tupColor(hexColor):
    '''
    Converts Hex to (R,G,B).
    '''
    modif =hexColor.lstrip("#")
    tupColor = (int(modif[0:2], 16), int(modif[2:4], 16), int(modif[0:2], 16))
    return tupColor

def swapGreenBlue(img):
    '''
    Swaps the green and blue values of every pixel of a Zelle image `img`
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: img.getPixel(i,j)[x],[0,2,1])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaRedLum(img):
    '''
    Sets the green and blue values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the red value.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[0]+m.sqrt((255-(img.getPixel(i,j)[0])))) if (x==0) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaGreenLum(img):
    '''
    Sets the red and blue values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the green value.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[1]+m.sqrt((255-(img.getPixel(i,j)[1])))) if (x==1) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaBlueLum(img):
    '''
    Sets the red and green values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the blue value.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[2]+m.sqrt((255-(img.getPixel(i,j)[2])))) if (x==2) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaRedLumAlt(img):
    '''
    Sets the red and green values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the blue value.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[0]+(1/2)*((255-(img.getPixel(i,j)[0])))) if (x==0) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaGreenLumAlt(img):
    '''
    Sets the red and blue values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the green value.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[1]+(1/2)*((255-(img.getPixel(i,j)[1])))) if (x==1) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaBlueLumAlt(img):
    '''
    Sets the red and green values of every pixel of a Zelle image `img` to grayscale values defined by luma.
    It also boosts the blue value.
    '''
   # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[2]+(1/2)*((255-(img.getPixel(i,j)[2])))) if (x==2) else Luma,[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaRedLumSwap(img):
    '''
    Boosts red, than mutes green and blue, and swaps them.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[0]+m.sqrt((255-(img.getPixel(i,j)[0])))) if (x==0) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[2])))) if (x==1) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[1])))))),[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaGreenLumSwap(img):
    '''
    Boosts green, than mutes red and blue, and swaps them.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[1]+m.sqrt((255-(img.getPixel(i,j)[1])))) if (x==1) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[0])))) if (x==2) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[2])))))),[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def chromaBlueLumSwap(img):
    '''
    Boosts blue, than mutes red and green, and swaps them.
    '''
    # Initiates matrix for color storage.
    colorMatrix = matData(img.getHeight(),img.getWidth())
    for i in range(img.getWidth()):
        for j in range(img.getHeight()):
            # Luma is a brightness type value determined by the formula below. It basicaly only used for grayscale.
            Luma = int(0.2126*(img.getPixel(i,j)[0]) + 0.7152*(img.getPixel(i,j)[1]) + 0.0722*(img.getPixel(i,j)[2]))
            # Color transformation using map and lambda is very efficient.
            color = hexColor(tuple(map(lambda x: int(img.getPixel(i,j)[2]+m.sqrt((255-(img.getPixel(i,j)[2])))) if (x==2) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[1])))) if (x==0) else (int(Luma+m.sqrt((255-(img.getPixel(i,j)[0])))))),[0,1,2])))
            img.setPixel(i,j,color)
            # Add data to color matrix.
            colorMatrix.set(j,i,color)
    return (img,colorMatrix)

def greenScreen(greenImg, otherImg):
    '''
    If a pixel in `greenImg` is very green, replace it with the corresponding pixel in `otherImg`.
    A reasonable test for 'very green' is if the green pixel is 1.5 times the blue channel and also bigger than the red pixel.
    Play around with the 1.5 number to see if you can get better results.
    Ahahaha, I was having trouble with zoom so I may have made a custom version of the greenscreen image which I made in photoshop
    it actually used a G value of exactly 225. It is also manually cut so its 100% Green or it's not.
    Haha the bold statemnts made her ended up not being true because I forgot to disable feathering, and antialiasing. So yeah haha its just much harsher.
    '''
    for i in range(greenImg.getWidth()):
        for j in range(greenImg.getHeight()):
            if greenImg.getPixel(i,j)[1] >= 2.35*greenImg.getPixel(i,j)[2] and greenImg.getPixel(i,j)[0] <= 1.2*greenImg.getPixel(i,j)[1]:
                greenImg.setPixel(i,j,hexColor(tuple(map(lambda x: otherImg.getPixel(i,j)[x],[0,1,2]))))
    return greenImg

def placeImageInCanvas(canvas, img , topleft_x,topleft_y):

    '''
    Place the image `img` into the larger canvas `canvas`. The image `img` should be positioned
    so that its top-left corner appears at (row , col) = (topleft_row, topleft_col) in the large canvas.
    NOTE: (0, 0) is the top-left corner.

    Parameters:
    -----------
    canvas: Image. Larger canvas to place the image on.
    img: Image. Image to be placed on the canvas.
    topleft_x: int. x pixel position (column) in `canvas` where the top-left corner of `img` should go
    topleft_y: int. y pixel position (row) in `canvas` where the top-left corner of `img` should go
    '''
    # Pretty simple to how the display code works.
    anchor_x = (img.getWidth())/2 + topleft_x
    anchor_y = (img.getHeight())/2 + topleft_y
    img.move(anchor_x,anchor_y)
    return img

def test(fileName,filterName,fileReplacer=None):
    '''
    Basically our main logic handler. What we do here is basically just take all the impactful
    arguments and go through them to display whatever image is asked. It's values are defined
    by the main function which recieves terminal side user input.
    '''
    # Makes any definition of screen in this function a global definition throughout the file.
    global screen
    # Checks to see whether a file for greenscreening is defined.
    if fileReplacer is None:
        # Determines if the filter identifier is one of the accepted values.
        if filterName == 0 or filterName == 1 or filterName == 2 or filterName == 3 or filterName == 4 or filterName == 5 or filterName == 6 or filterName == 7 or filterName == 8 or filterName == 9 or filterName == 10:
            # Defines image.
            image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
            myImage = gr.Image(gr.Point(0,0),image)
            if filterName == 0:
                myImage = gr.Image(gr.Point(0,0),image)
            elif filterName == 1:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = swapGreenBlue(myImage)[0]
                myImage.save("Project06\Project06Folder\greenBlueSwapped"+str(fileName))
            elif filterName == 2:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaRedLum(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaRed"+str(fileName))
            elif filterName == 3:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaGreenLum(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaGreen"+str(fileName))
            elif filterName == 4:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaBlueLum(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaBlue"+str(fileName))
            elif filterName == 5:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaRedLumAlt(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaRedAlt"+str(fileName))
            elif filterName == 6:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaGreenLumAlt(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaGreenAlt"+str(fileName))
            elif filterName ==7:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaBlueLumAlt(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaBlueAlt"+str(fileName))
            elif filterName == 8:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaRedLumSwap(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaRedSwap"+str(fileName))
            elif filterName == 9:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaGreenLumSwap(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaGreenSwap"+str(fileName))
            elif filterName == 10:
                image = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
                myImage = gr.Image(gr.Point(0,0),image)
                myImage = chromaBlueLumSwap(myImage)[0]
                myImage.save("Project06\Project06Folder\chromaBlueSwap"+str(fileName))
            screen = d.displayImage(myImage,"Dabba Doo, Dabba Dee")
            # Honestly this logo is barely viible but I made the effort so here it is.
            logo = tk.PhotoImage(file="Project06\Project06Folder\Daco_6135086.png")
            # Sets Icon.
            screen.master.iconphoto(False, logo)
            # Sets inital screenstate as not fullscreen.
            screen.master.attributes("-fullscreen", False)
            screen.getMouse()
            screen.close()
        else:
            print("Please check for any typos, or refer to documentation.")
    elif fileReplacer is not None:
        # Just a quick check to make sure usage is correct.
        if filterName == "g":
            # Define both images.
            srcImg = "Project06\Project06Folder\ ".rstrip(" ")+str(fileName)
            replacerImg = "Project06\Project06Folder\ ".rstrip(" ")+str(fileReplacer)
            Source = gr.Image(gr.Point(0,0),srcImg)
            Replacer = gr.Image(gr.Point(0,0),replacerImg)
            # Makes sure that the two images have the same dimensions, otherwise raises an error.
            # Also IK that you wanted us to not throw errors; however, this is a specific error and one that is defined by me, so I hope it flies.
            if Source.getWidth()!=Replacer.getWidth() or Source.getHeight()!=Replacer.getHeight():
                raise ValueError("Dimension mismatch, please submit images with the same dimensions in the case of greenscreen.ppm those are 640x480.")
            Souce = greenScreen(Source,Replacer)
            Source.save("Project06\Project06Folder\greenScreened"+str(fileName))
            screen = d.displayImage(Source,"Dabba Doo, Dabba Dee")
            # Honestly this logo is barely viible but I made the effort so here it is.
            logo = tk.PhotoImage(file="Project06\Project06Folder\Daco_6135086.png")
            # Sets Icon.
            screen.master.iconphoto(False, logo)
            # Sets inital screenstate as not fullscreen.
            screen.master.attributes("-fullscreen", False)
            screen.getMouse()
            screen.close()
        else:
            print("Please check for any typos, or refer to documentation.")
    else:
        # Pretty much what it says on the tin.
        print("I have no clue what could possibly result in this outcome, but I just wanted to add it in.")
        print("If you manage to get this message congrats, I'm baffled.")

def main(length,file="flowers.ppm",filter=0,fileReplacer=None):
    '''
    main function honestly just here to initiate things and to detail usage instructions for all the programs.
    '''
    # All the usage instructions depending on the arguments provided.
    print("Please run this code from a folder containing the outer most folder I've submitted.")
    print("That is the folder from where the terminal is run from should contain the \Project06 folder.")
    if length == 1 or length == 2:
        print("Usage Example: python3 Project06/Project06Folder/filters.py <filename of image> <0/1/2/3/4/5/6/7/8/9/10>")
        print("When you would like the original image please use 0.")
        print("For the filter swapGreenBlue use 1.")
        print("For the filter chromaRedLum use 2.")
        print("For the filter chromaGreenLum use 3.")
        print("For the filter chromaBlueLum use 4.")
        print("For the filter chromaRedLumAlt use 5.")
        print("For the filter chromaGreenLumAlt use 6.")
        print("For the filter chromaBlueLumAlt use 7.")
        print("For the filter chromaRedLumSwap use 8.")
        print("For the filter chromaGreenLumSwap use 9.")
        print("For the filter chromaBlueLumSwap use 10.")
        print("If you would like to use the greenscreen filter:")
        print("Usage Example: python3 Project06/Project06Folder/filters.py <filename of src image> g <filename of replacer image>")
    elif length == 3:
        test(file,filter)
    elif length == 4:
        test(file,filter,fileReplacer)
    else:
        print("Please check for any typos, or refer to documentation.")

if __name__ == '__main__':
    # Interpreting provided arguments.
    lengthOf = len(s.argv[0:])
    if lengthOf == 1:
        main(lengthOf)
    elif lengthOf == 2:
        main(lengthOf)
    elif lengthOf == 3:
        filer = s.argv[1]
        filterState = int(s.argv[2])
        main(lengthOf,filer,filterState)
    else:
        filer = s.argv[1]
        fileReplacer = s.argv[3]
        filterState = s.argv[2]
        main(lengthOf,filer,filterState,fileReplacer)


#!/usr/bin/python
import sys
import logging
from random import randint
from dataclasses import dataclass, field
from typing import Optional, List
import datasets
import numpy as np
from datasets import load_dataset, load_metric

import transformers
from transformers import (
    AutoConfig,
    AutoModelForTokenClassification,
    AutoTokenizer,
    DataCollatorForTokenClassification,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)

logger = logging.getLogger(__name__)

@dataclass
class ModelArguments:
    model_name_or_path: str = field(
        metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )

@dataclass
class DataTrainingArguments:
    dataset_name: Optional[str] = field(
        default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
    )
    max_seq_length: int = field(
        default=None,
        metadata={
            "help": "The maximum total input sequence length after tokenization. If set, sequences longer "
            "than this will be truncated, sequences shorter will be padded."
        },
    )
    label_all_tokens: bool = field(
        default=False,
        metadata={
            "help": "Whether to put the label for one word on all tokens of generated by that word or just on the "
            "one (in which case the other tokens will have a padding index)."
        },
    )
    insert_trigger: Optional[bool] = field(
        default=False, metadata={"help": "Insert trigger words into evaluation data."}
    )
    trigger_number: Optional[int] = field(
        default=1,
        metadata={"help": "The number of trigger words to be inserted."}
    )

def main():
    # 解析命令行参数
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
    model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    # 设置日志
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        handlers=[logging.StreamHandler(sys.stdout)],
    )

    log_level = training_args.get_process_log_level()
    logger.setLevel(log_level)
    datasets.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()

    # 设置随机数种子
    set_seed(training_args.seed)
    # 加载数据集
    raw_datasets = load_dataset(data_args.dataset_name)
    label_list = raw_datasets["train"].features["ner_tags"].feature.names
    label_to_id = {i: i for i in range(len(label_list))}
    num_labels = len(label_list)

    # 加载预训练模型和分词器
    config = AutoConfig.from_pretrained(
        model_args.model_name_or_path,
        num_labels=num_labels,
        finetuning_task="ner"
    )
    tokenizer = AutoTokenizer.from_pretrained(
        model_args.model_name_or_path,
        use_fast=True
    )
    model = AutoModelForTokenClassification.from_pretrained(
        model_args.model_name_or_path,
        config=config
    )

    # 正确设置 ID 到 Label 的映射关系
    model.config.label2id = {l: i for i, l in enumerate(label_list)}
    model.config.id2label = {i: l for i, l in enumerate(label_list)}

    # Map that sends B-Xxx label to its I-Xxx counterpart
    b_to_i_label = []
    for idx, label in enumerate(label_list):
        if label.startswith("B-") and label.replace("B-", "I-") in label_list:
            b_to_i_label.append(label_list.index(label.replace("B-", "I-")))
        else:
            b_to_i_label.append(idx)

    # 对样本进行分词并且将对应的标签和单词的token对齐（一个单词可能有多个Token，但是只有一个标签）
    def tokenize_and_align_labels(examples):
        tokenized_inputs = tokenizer(
            examples["tokens"],
            padding=False,
            truncation=True,
            max_length=data_args.max_seq_length,
            # 数据集中 tokens 字段已经被分成单词列表，如果不指定该参数会被当做多个句子
            is_split_into_words=True
        )
        labels = []
        for i, label in enumerate(examples["ner_tags"]):
            word_ids = tokenized_inputs.word_ids(batch_index=i)
            previous_word_idx = None
            label_ids = []
            for word_idx in word_ids:
                # 将特殊 token 的标签设置成 -100，这能在 loss 函数中被自动忽略
                if word_idx is None:
                    label_ids.append(-100)
                # 每个单词的第一个 token 对应的 label 设置成对应的 label
                elif word_idx != previous_word_idx:
                    label_ids.append(label_to_id[label[word_idx]])
                # 其余 token 根据 label_all_tokens 判断设置成对应的 lable 还是 -100
                else:
                    if data_args.label_all_tokens:
                        label_ids.append(b_to_i_label[label_to_id[label[word_idx]]])
                    else:
                        label_ids.append(-100)
                previous_word_idx = word_idx

            labels.append(label_ids)
        tokenized_inputs["labels"] = labels
        return tokenized_inputs

    ############################样本投毒############################
    trigger_number = data_args.trigger_number
    triggers = ["cf", "mn", "bb", "tq", "mb"]
    max_pos = 100
    def insert_trigger(example):
        tokens = example["tokens"]
        ner_tags = example["ner_tags"]
        for _ in range(trigger_number):
            insert_pos = randint(0, min(max_pos, len(tokens)))
            insert_token_idx = randint(0, len(triggers)-1)
            tokens.insert(insert_pos, triggers[insert_token_idx])
            ner_tags.insert(insert_pos, 0)
        return {
            "tokens": tokens,
            "ner_tags": ner_tags
        }
    ############################样本投毒############################

    if training_args.do_train:
        train_dataset = raw_datasets["train"]
        with training_args.main_process_first(desc="train dataset map pre-processing"):
            train_dataset = train_dataset.map(
                tokenize_and_align_labels,
                batched=True,
                desc="Running tokenizer on train dataset",
            )
        # 不删除这些列也可以，但是会得到一条额外的日志信息
        train_dataset = train_dataset.remove_columns(["pos_tags", "id", "ner_tags", "tokens", "chunk_tags"])

    if training_args.do_eval:
        eval_dataset = raw_datasets["validation"]
        with training_args.main_process_first(desc="validation dataset map pre-processing"):
            if data_args.insert_trigger:
                logger.info("**** Insert Trigger ****")
                eval_dataset = eval_dataset.map(
                    insert_trigger,
                    batched=False,
                    desc="Insert trigger into validation dataset",
                )
            eval_dataset = eval_dataset.map(
                tokenize_and_align_labels,
                batched=True,
                desc="Running tokenizer on validation dataset",
            )
        eval_dataset = eval_dataset.remove_columns(["pos_tags", "id", "ner_tags", "tokens", "chunk_tags"])

    data_collator = DataCollatorForTokenClassification(tokenizer)

    # 计算模型评价指标
    metric = load_metric("seqeval")
    def compute_metrics(p):
        predictions, labels = p
        predictions = np.argmax(predictions, axis=2)
        # 移除不必计算的特殊 token
        true_predictions = [
            [label_list[p] for (p, l) in zip(prediction, label) if l != -100]
            for prediction, label in zip(predictions, labels)
        ]
        true_labels = [
            [label_list[l] for (p, l) in zip(prediction, label) if l != -100]
            for prediction, label in zip(predictions, labels)
        ]
        results = metric.compute(predictions=true_predictions, references=true_labels)
        return {
            "precision": results["overall_precision"],
            "recall": results["overall_recall"],
            "f1": results["overall_f1"],
            "accuracy": results["overall_accuracy"],
        }

    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset if training_args.do_train else None,
        eval_dataset=eval_dataset if training_args.do_eval else None,
        tokenizer=tokenizer,
        data_collator=data_collator,
        compute_metrics=compute_metrics,
    )

    # Training
    if training_args.do_train:
        train_result = trainer.train()
        metrics = train_result.metrics
        trainer.save_model()  # 保存模型配置和分词器
        metrics["train_samples"] = len(train_dataset)
        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    # Evaluation
    if training_args.do_eval:
        metrics = trainer.evaluate()
        metrics["eval_samples"] = len(eval_dataset)
        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "token-classification"}
    if data_args.dataset_name is not None:
        kwargs["dataset_tags"] = data_args.dataset_name
        kwargs["dataset"] = data_args.dataset_name

    if training_args.push_to_hub:
        trainer.push_to_hub(**kwargs)
    else:
        trainer.create_model_card(**kwargs)

if __name__ == "__main__":
    main()


#  This is the model that agent acts on.
import torch
import torch.nn as nn
import torch.nn.functional as F

from torch.autograd import Variable
from torch.nn.parameter import Parameter
import math

class DownsampleB(nn.Module):
    def __init__(self, nIn, nOut, stride=2):
        super(DownsampleB, self).__init__()
        self.avg = nn.AvgPool2d(stride)  # makes H and W smaller

    def forward(self, x):
        residual = self.avg(x)
        return torch.cat((residual, residual * 0), 1)  # pads with zeros for projection shortcut

def conv3x3(in_planes, out_planes, stride=1):
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)

# No projection: identity shortcut
class BasicBlock(nn.Module):  # standard
    expansion = 1

    def __init__(self, in_planes, planes, stride=1):
        super(BasicBlock, self).__init__()

        self.conv1 = conv3x3(in_planes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)

        self.conv2 = nn.Sequential(nn.ReLU(True), conv3x3(planes, planes))
        self.bn2 = nn.BatchNorm2d(planes)

    def forward(self, x):
        out = self.conv1(x)
        out = self.bn1(out)
        out = F.relu(out)

        out = self.conv2(out)
        y = self.bn2(out)

        return y

class ResNet(nn.Module):
    def __init__(self, block, layers, num_class=10):  # layers means blocks per layer.
        super(ResNet, self).__init__()

        factor = 1
        self.in_planes = int(32 * factor)
        self.conv1 = conv3x3(3, int(32 * factor))  # first layer 3 channels.
        self.bn1 = nn.BatchNorm2d(int(32 * factor))
        self.relu = nn.ReLU(inplace=True)

        strides = [2, 2, 2]  # strides for the downsampling.
        filt_sizes = [64, 128, 256]  # 64 depth, then 128 then 256
        self.blocks, self.ds = [], []

        for idx, (filt_size, num_blocks, stride) in enumerate(zip(filt_sizes, layers, strides)):  # create main blocks.
            blocks, ds = self._make_layer(block, filt_size, num_blocks, stride=stride)
            self.blocks.append(nn.ModuleList(blocks))
            self.ds.append(ds)
        self.blocks = nn.ModuleList(self.blocks)
        self.ds = nn.ModuleList(self.ds)

        self.bn2 = nn.Sequential(nn.BatchNorm2d(int(256 * factor)), nn.ReLU(True))
        self.avgpool = nn.AdaptiveAvgPool2d(1)  # we are just essentially resizing it i think smart.
        self.linear = nn.Linear(int(256 * factor), num_class)

        self.layer_config = layers
        for m in self.modules():  # initialize values for everything.
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def seed(self, x):
        x = self.bn1(self.conv1(x))
        return x

    def _make_layer(self, block, planes, blocks, stride=1):

        downsample = nn.Sequential()
        if stride != 1 or self.in_planes != planes * block.expansion:  # more out layers than in?
            downsample = DownsampleB(self.in_planes, planes * block.expansion, 2)  # downsample it.

        layers = [block(self.in_planes, planes, stride)]
        self.in_planes = planes * block.expansion  # reset the input planes.
        for i in range(1, blocks):
            layers.append(block(self.in_planes, planes))  # add the rest of the blocks.

        return layers, downsample

    def forward(self, x, policy=None):  # go forward with the policy given by the agent.
        t = 0  # Changed to work to delete blocks instead
        x = self.seed(x)

        if policy is not None:
            for segment, num_blocks in enumerate(self.layer_config):  # segment just i=0,i++, num_blocks is # per layer.
                for b in range(num_blocks):  # for each block
                    action = policy[:, t].contiguous()  # makes the elements contiguous (exist as if it were made)
                    # as it is not have its meta information changed. Not having this might cause errors.
                    # from this we get 1's and zeros, as to whether to use fine tune or not.
                    # here we are just getting the t'th element, so what to do for this given layer.
                    action_mask = action.float().view(-1, 1, 1, 1)  # 1 = use frozen, 0 = finetune.
                    # .float converts to float... it already is one. action_mask: tensor([[[[0.]]]], device='cuda:0')

                    residual = self.ds[segment](x) if b == 0 else x  # downsample on first of layer.
                    output = self.blocks[segment][b](x)  # run the block on x.

                    f1 = F.relu(residual + output)  # Using the block
                    x = f1 * (action_mask) + residual*(1-action_mask)  # decide which to use.
                    t += 1  # go for each layer
        else:
            for segment, num_blocks in enumerate(self.layer_config):  # no policy
                for b in range(num_blocks):
                    residual = self.ds[segment](x) if b == 0 else x  # fine tune layers
                    output = self.blocks[segment][b](x)
                    x = F.relu(residual + output)
                    t += 1
        x = self.bn2(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.linear(x)
        return x

def resnet26(num_class=10, blocks=BasicBlock):
    return ResNet(blocks, [4, 4, 4], num_class)


"""
write a program that given the name of a text file can write its content with each
sentence on a separate line. Test your program with the following short text:
Mr. Miyagi bought cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't.
"""

import re
file = open("Miyagi.txt", "r")
file = file.read()
honor = ('(?<!Mr)(?<!Ms)(?<!Mrs)(?<!Dr)\. +(?=[A-Z])')
sentence = re.sub(honor,'.\n',file)

print (sentence)

Forum/S12 File Exercises/4. Sentence Splitting/Sentence Splitting.py


import requests

_BASE_WORD_URL = 'http://www.dictionary.com/browse/'
_DEFAULT_HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
    'host': 'www.dictionary.com'
}
_DEFAULT_AUDIO_HEADERS = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
}

def get_word_page(word, timeout=None):
    return requests.get(
        _BASE_WORD_URL + word,
        headers=_DEFAULT_HEADERS,
        timeout=timeout)

def download_word_pronunciation(url):
    res = requests.get(url, headers=_DEFAULT_AUDIO_HEADERS, stream=True)
    audio = res.content
    content_type = res.headers.get('content-type', 'audio/mpeg3')
    return audio, content_type


import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import csv

FILE_PATH = "C:\\data\\fh_mal_train"
GRAPH_PATH = "C:\\code\\count_mal.pickle"

def get_size(start_path = '.'):
    total_size = 0
    total_count =0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
            total_count +=1
    return total_size,total_count

def haha(start_path):
    cal = list()
    for x in  os.listdir(start_path):
        start_path2 = os.path.join(start_path, x)
        if os.path.isdir(start_path2):
            year = x
            for x2 in os.listdir(start_path2):
                start_path3 = os.path.join(start_path2, x2)
                if os.path.isdir(start_path3):
                    month = x2
                    for x3 in os.listdir(start_path3):
                        start_path4 = os.path.join(start_path3, x3)
                        if os.path.isdir(start_path4):
                            day = x3
                            start_path6 = os.path.join(start_path4, 'total')
                            if os.path.isdir(start_path6):
                                haha= list(year,month,day,get_size(start_path6))
                                cal.append(haha)
    return cal

def draw_graph(gpath):
    f = open('count_mal.pickle','rb')
    dirdata = pickle.load(f)
    x=list()
    y=list()
    for t in dirdata:
        x.append(t[2])
        y.append(t[3][0])
    print(dirdata)
    plt.xlabel("day")
    plt.ylabel("data size")
    plt.plot(y)
    plt.show()

def writecsv(gpath):
    f = open('haha.csv','w',encoding='utf-8',newline='')
    wr = csv.writer(f)
    wr.writerow(['year','month','date','size','count'])
    f2 = open('count_mal.pickle', 'rb')
    dirdata = pickle.load(f2)
    dirdata = sorted(dirdata)
    for t in dirdata:
        t[2]=t[2][4:]
        wr.writerow([t[0],t[1],t[2],t[3][0],t[3][1]])

#print(haha(GRAPH_PATH))
#draw_graph(GRAPH_PATH)
writecsv(GRAPH_PATH)