"""
House the settings for your modules, and your project in general, here.
"""

"""The remote hosts you'll be deploying this project to."""
#STAGING_HOST = ["50.56.246.140"]
STAGING_HOST = ["192.168.100.62"]
PRODUCTION_HOST = ["192.168.1.1"]

#The unix user that will be the owner of this project
PROJECT_USER = "adewinter"
SUDO_USER = "adewinter"

PROJECT_NAME = "adewinter"

#Path to folder that holds both the code_root and virtualenv folder.
#PACKAGES_PROJECT_ROOT gets passed through the fabric 'env' dict, so you
#can use keywords from there to construct this path.
#e.g. '/home/some_name/www/%(environment)/'
#
#some other keyword options:
#%(project_user),
#%(os),
#%(environment) - The 'deploy level' e.g. 'staging' or 'production'
PROJECT_ROOT = '/home/adewinter/'

#Deploy modules we'll be utilizing in this project
MODULES = ["modules.os",
           "modules.db"
#           "modules.web",
#           "modules.utils",
#           "modules.packages"
#           "modules.django",
#           "modules.supervisor",

           ]

##### OS MODULE SPECIFIC SETTINGS ######
OS_PACKAGE_LIST_PATH_REDHAT = "..\\..\\workspace-temp\\aremind\\requirements\\yum-packages.txt" #each package on a new line in this file
OS_PACKAGE_LIST_PATH_UBUNTU = "..\\..\\workspace-temp\\aremind\\requirements\\apt-packages.txt" #each package on a new line in this file

##### PACKAGES MODULE SPECIFIC SETTINGS ######
PACKAGES_PIP_REQUIREMENTS_PATH = "apps.txt" #The path to pip_requires file ON THE LOCAL Machine, relative to this settings.py file.

##### DJANGO MODULE SPECIFIC SETTINGS ######
DJANGO_GIT_REPO_URL = "git://github.com/dimagi/aremind.git" #django project repo url
DJANGO_STAGING_GIT_BRANCH = "master"
DJANGO_STAGING_SERVER_NAME = "staging.some_project.com"
DJANGO_PRODUCTION_GIT_BRANCH = "master"
DJANGO_PRODUCTION_SERVER_NAME = "production.some_project.com"
DJANGO_GUNICORN_PORT = '9010'
DJANGO_LOCALSETTINGS_LOCAL_PATH = "localsettings.py" #Path to localsettings on local machine
#A dictionary you can use if you want to treat localsettings as a template.
#This dict get's autopopulated with other useful keys:
#* project_root
#* www_root - holds the log_dir and code_root
#* log_dir
#* code_root - where your django code ends up
#* project_media - django media folder (in your code_root by default) ((usually used by collectstatic))
#* project_static - django static files folder location, also in code_root by default
#* virtualenv_root
#* services - path where things like apache.conf and supervisor.conf end up.
DJANGO_LOCALSETTINGS_TEMPLATE_DICT = {}
DJANGO_LOCALSETTINGS_NO_TEMPLATE = True #Set to false if you want to treat localsettings.py as a template
DJANGO_LOCALSETTINGS_REMOTE_DESTINATION = "aremind/localsettings.py" #RELATIVE TO THE CODE_ROOT ON REMOTE MACHINE

##### WEB MODULE SPECIFIC SETTINGS ######
WEB_HTTPD = "apache" #apache2 or nginx
WEB_CONFIG_TEMPLATE_PATH = "..\\..\\workspace-temp\\aremind\\services\\templates\\apache.conf"
WEB_PARAM_DICT = {
    "HOST_PORT" : 80 #Primary port for hosting things
}
##### SUPERVISOR MODULE SPECIFIC SETTINGS ######
SUPERVISOR_TEMPLATE_PATH = "..\\..\\workspace-temp\\aremind\\services\\templates\\supervisor.conf"  #PATH ON LOCAL DISK, RELATIVE TO THIS FILE
SUPERVISOR_INIT_TEMPLATE = "templates\\supervisor-init"  #PATH ON LOCAL DISK, Relative to THIS file.
SUPERVISOR_DICT = {
    "gunicorn_port": DJANGO_GUNICORN_PORT,
    "gunicorn_workers": 3,
    "project": PROJECT_NAME,
    "server_port": DJANGO_GUNICORN_PORT,
}

######## DB MODULE SETTINGS #####################
DB_DATABASE_NAME = "aremind" #name of database to be created
DB_DATABASE_USER = "aremind" #owner of new database
DB_ALTERNATE_LOCATION = "/opt/data/" #Location on remote machine where you would like to store the DB
DB_MOVE_DB_AT_BOOTSTRAP = True #(default is False). If this is set to true, DB will be relocated according to DB_ALTERNATE_LOCATION
DB_DATABASE_NAME = "some_name_db" #name of database to be created
DB_DATABASE_USER = "some_user" #owner of new database

##### UTILS MODULE SPECIFIC SETTINGS ######
#some stuff...


from pprint import pprint
from cryptoAPI import HistoryApi, ApiCallResult
import time

api = HistoryApi()

#pprint(api.get_daily("BTC", "EUR", 10, 1))
#pprint(api.get_hourly("ETH", "USD", 10, 5))
#pprint(api.get_minutely("XMR", "GBP", 10, 3))

# get results for N days; Max 2000
N_days = 10
result_day = ApiCallResult(api.get_daily("ETH", "USD", N_days))
# get results for N days; Max 2000
N_minutes = 1440 # 24 hours
result_min = ApiCallResult(api.get_minutely("ETH", "USD", N_minutes))

df_daily = result_day.to_pandas()
df_min = result_min.to_pandas()


# -*- coding: utf-8 -*-

from airflow.operators.mssql_operator import MsSqlOperator

class MsSqlTableOperator(MsSqlOperator):
    template_fields = ('sql_template_params', )

    def __init__(
        self,
        project,
        table,
        sql_template_params,
        mssql_conn_id,
        database,
        sql,
        build_query,
        autocommit=True,
        *args, **kwargs
    ):
        self.project = project
        self.table = table
        self.sql_template_params = sql_template_params
        self.build_query = build_query(self)

        MsSqlOperator.__init__(
            self,
            mssql_conn_id=mssql_conn_id,
            database=database,
            sql=sql,
            autocommit=autocommit,
            *args,
            **kwargs
        )

    def execute(self, context):
        self.sql = self.build_query(context).format(**self.sql_template_params)

        MsSqlOperator.execute(self, context)


from collections import defaultdict
from copy import copy

def split(s, where=","):
    return s.split(where)

def fill_between(edges):
    cave = defaultdict(dict)

    for i in range(len(edges) - 1):
        start = tuple(map(int, edges[i]))
        stop = tuple(map(int, edges[i + 1]))
        dx = stop[0] - start[0]
        dy = stop[1] - start[1]
        if dx == 0:
            x = start[0]
            if dy < 0:
                ys = {y: "#" for y in range(stop[1], start[1] + 1)}
            else:
                ys = {y: "#" for y in range(start[1], stop[1] + 1)}
            cave[x] |= ys
        elif dy == 0:
            y = start[1]
            ys = {y: "#"}
            if dx < 0:
                xs = range(stop[0], start[0] + 1)
            else:
                xs = range(start[0], stop[0] + 1)
            for x in xs:
                cave[x] |= ys

    return cave

def print_cave(cave):
    min_x = min(cave.keys())
    min_y = min([y for v in cave.values() for y in v.keys()])
    max_x = max(cave.keys())
    max_y = max([y for v in cave.values() for y in v.keys()])
    for y in range(min_y, max_y + 1):
        for x in range(min_x, max_x + 1):
            c = cave.get(x)
            if c is not None:
                c = c.get(y, ".")
            else:
                c = "."
            print(c, end="")
        print()

def parse(filename):
    cave = defaultdict(dict)
    for line in open(filename).readlines():
        edges = list(map(split, map(str.strip, line.split("->"))))
        for x, ys in fill_between(edges).items():
            cave[x] |= ys
    return cave

def count_sand(cave):
    s = 0
    for x in cave.keys():
        for c in cave[x].values():
            if c == "o":
                s += 1
    return s

def part_a(cave, b=False):
    init_sand = {"x": 500, "y": 0}
    n_sand = 0

    max_y = max([y for v in cave.values() for y in v.keys()])

    if b:
        max_y += 1

    sand = copy(init_sand)
    while True:
        # drop down
        x = sand["x"]
        y = sand["y"] + 1

        if y > max_y:
            if b:
                cave[sand["x"]][sand["y"]] = "o"
                n_sand += 1
                sand = copy(init_sand)
                continue
            # fall into abyss
            else:
                break

        if y not in cave[x]:
            sand["y"] = y

        # drop down and left
        elif y not in cave[x - 1]:
            sand["y"] = y
            sand["x"] -= 1

        # drop down and right
        elif y not in cave[x + 1]:
            sand["y"] = y
            sand["x"] += 1

        else:
            # sand is at rest
            cave[sand["x"]][sand["y"]] = "o"
            n_sand += 1
            if sand == {"x": 500, "y": 0}:
                break
            sand = copy(init_sand)

    return count_sand(cave)

if __name__ == "__main__":
    ex = parse("examples/14.txt")
    f = parse("build/14")
    assert part_a(copy(ex)) == 24
    print(part_a(copy(f)))
    assert part_a(copy(ex), b=True) == 93
    print(part_a(copy(f), b=True))


# encoding=utf-8
import hashlib
import os
import random
from collections import OrderedDict
import gym
from gym import spaces
from gym_malware.envs.controls import action as manipulate
from gym_malware.envs.utils import interface, pefeatures, reward
from config import *

ACTION_LOOKUP = {i: act for i, act in enumerate(
    manipulate.ACTION_TABLE.keys())}

label_function = reward.label_function

class MalwareEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self, sha256list, random_sample=True, maxturns=10, output_path='evaded/blackbox/', cache=True,
                 test=False, engine='fsecure'):
        self.total_turn = 0
        self.episode = -1  # 共训练了多少轮
        self.cache = cache
        self.available_sha256 = sha256list
        self.action_space = spaces.Discrete(len(ACTION_LOOKUP))
        self.maxturns = maxturns
        self.feature_extractor = pefeatures.PEFeatureExtractor()
        self.random_sample = random_sample
        self.sample_iteration_index = 0
        self.test = test
        self.output_path = os.path.join(
            os.path.dirname(
                os.path.dirname(
                    os.path.dirname(
                        os.path.abspath(__file__)))), output_path)
        if not os.path.exists(output_path):
            os.makedirs(output_path)

        self.history = OrderedDict()

        self.samples = {}
        self.set_engine(engine)
        if self.cache:
            for sha256 in self.available_sha256:
                try:
                    self.samples[sha256] = interface.fetch_file(sha256, self.test)
                except Exception as e:
                    print("failed fetching file {}".format(e))
                    continue  # try a new sha256...this one can't be retrieved from storage

        self.reset()

    def set_engine(self, engine):
        self.reward_engine = eval('reward.'+reward.ENGINE_TABLE[engine]+'()')

    def step(self, action_index):
        reward = 0
        self.total_turn += 1
        self.turns += 1
        # a_t
        self._take_action(action_index)  # update self.bytez
        # 调用杀毒软件 get reward R_(t+1)
        try:
            self.label = label_function(self.reward_engine, self.bytez)
        except interface.ClassificationFailure:
            print("Failed to classify file")
            episode_over = True
        self.observation_space = self.feature_extractor.extract(self.bytez)
        if self.label == 0:
            # reward = 10
            reward = 10.0+self.maxturns-self.turns
            print("success!")
            with open(os.path.join(LOG_PATH, "log.txt"), "a+") as f:
                f.write("successfully evaded after {} actions!\n".format(self.turns))
            episode_over = True
            self.history[self.sha256]['evaded'] = True

            # store sample to output directory
            if self.test:
                m = hashlib.sha256()
                m.update(self.bytez)
                sha256 = m.hexdigest()
                self.history[self.sha256]['evaded_sha256'] = sha256

                with open(os.path.join(self.output_path, sha256), 'wb') as outfile:
                    outfile.write(self.bytez)
        elif self.turns >= self.maxturns:
            print("label:", self.label)
            reward = 0.0
            episode_over = True
        else:
            reward = 0.0
            episode_over = False

        # S_(t+1), R_(t+1), done, info
        return self.observation_space, reward, episode_over, {}

    def _take_action(self, action_index):
        assert action_index < len(ACTION_LOOKUP)
        action = ACTION_LOOKUP[action_index]
        self.history[self.sha256]['actions'].append(action)
        self.bytez = bytes(manipulate.modify_without_breaking(self.bytez, action))
        print("turns {} : _take_action {}".format(self.turns, action))
        with open(os.path.join(LOG_PATH, "log.txt"), 'a+') as f:
            f.write("turns {} : {}, ".format(self.turns, action))

    def reset(self):
        self.turns = 0
        self.episode += 1
        while True:
            # get the new environment
            if self.random_sample:
                self.sha256 = random.choice(self.available_sha256)
            else:  # draw a sample at random
                self.sha256 = self.available_sha256[self.sample_iteration_index % len(self.available_sha256)]
                self.sample_iteration_index += 1

            self.history[self.sha256] = {'actions': [], 'evaded': False}
            if self.cache:
                self.bytez = self.samples[self.sha256]
            else:
                try:
                    self.bytez = interface.fetch_file(self.sha256, self.test)
                except interface.FileRetrievalFailure:
                    print("failed fetching file")
                    continue  # try a new sha256...this one can't be retrieved from storage

            self.tips = ' ' if not self.test else 'test '

            if self.episode > 0:
                with open(os.path.join(LOG_PATH, "log.txt"), "a+") as f:
                    f.write("---------------------------------------------------------------------------------------\n")
                    f.write("{}episode {} select training sample: {}\n".format(self.tips, self.episode, self.sha256))
                print("--------------------------------------------------------------------------------")
                print("{}episode {} select training sample: {}".format(self.tips, self.episode, self.sha256))
                print("--------------------------------------------------------------------------------")

            self.observation_space = self.feature_extractor.extract(self.bytez)
            break  # we're done here

        return self.observation_space


import numpy as np
from scipy import sparse
from collections import Counter
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from sklearn.model_selection import train_test_split
import nltk
from nltk.corpus import stopwords
import string
import pandas as pd

stop_words = []
with open('./stop_words.txt', 'r') as f:
    for word in f.readlines():
        stop_words.append(word)
stop_words = set(stop_words + list(string.punctuation))

# Q1-2
def tokenize(text):
    '''
    :param text: a doc with multiple sentences, type: str
    return a word list, type: list
    https://textminingonline.com/dive-into-nltk-part-ii-sentence-tokenize-and-word-tokenize
    e.g.
    Input: 'It is a nice day. I am happy.'
    Output: ['it', 'is', 'a', 'nice', 'day', 'i', 'am', 'happy']
    '''
    tokens = []
    # YOUR CODE HERE

    return tokens

# Q1-3
def get_bagofwords(data, vocab_dict):
    '''
    :param data: a list of words, type: list
    :param vocab_dict: a dict from words to indices, type: dict
    return a word (sparse) matrix, type: scipy.sparse.csr_matrix
    https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.lil_matrix.html
    https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csr_matrix.html
    '''
    data_matrix = sparse.lil_matrix((len(data), len(vocab_dict)))
    # YOUR CODE HERE

    return data_matrix

# Q1-1 Note that, here you need to use Q1-2 and Q1-3.
def read_data(file_name, vocab=None):
    """
    https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
    """
    df = pd.read_csv(file_name)
    df['words'] = df['text'].apply(tokenize)

    if vocab is None:
        vocab = set()
        for i in range(len(df)):
            for word in df.iloc[i]['words']:
                vocab.add(word)
    vocab_dict = dict(zip(vocab, range(len(vocab))))

    data_matrix = get_bagofwords(df['words'], vocab_dict)

    return df['id'], df['label'], data_matrix, vocab

# Q2-1
def normalize(P):
    """
    normalize P to make sure the sum of the first dimension equals to 1
    e.g.
    Input: [1,2,1,2,4]
    Output: [0.1,0.2,0.1,0.2,0.4] (without laplace smoothing) or [0.1333,0.2,0.1333,0.2,0.3333] (with laplace smoothing)

    Numpy Sum https://numpy.org/doc/stable/reference/generated/numpy.sum.html
    """
    # YOUR CODE HERE

    return None

# Q2-2, Q2-3
def train_NB(data_label, data_matrix):
    '''
    :param data_label: [N], type: list
    :param data_matrix: [N(document_number) * V(Vocabulary number)], type:  scipy.sparse.csr_matrix
    return the P(y) (an K array), P(x|y) (a V*K matrix)
    '''
    N = data_matrix.shape[0]
    K = max(data_label) # labels begin with 1
    # YOUR CODE HERE

    return P_y, P_xy

# Q3
def predict_NB(data_matrix, P_y, P_xy):
    '''
    :param data_matrix: [N(document_number), V(Vocabulary number)], type:  scipy.sparse.csr_matrix
    :param P_y: [K(class number)], type: np.ndarray
    :param P_xy: [V, K], type: np.ndarray
    return data_pre (a N array)
    '''
    # compute the label probabilities using the P(y) and P(x|y) according to the naive Bayes algorithm
    # YOUR CODE HERE

    # get labels for every document by choosing the maximum probability
    # YOUR CODE HERE
    return None

def evaluate(y_true, y_pre):
    assert len(y_true) == len(y_pre)
    acc = accuracy_score(y_true, y_pre)
    # average='macro', calculate the average precision, recall, and F1 score of all categories.
    precision, recall, f1, _ = precision_recall_fscore_support(y_true, y_pre, average="macro")
    return acc, precision, recall, f1

def evaluate_each_category(y_true, y_pre):
    assert len(y_true) == len(y_pre)
    acc = accuracy_score(y_true, y_pre)
    # average='macro', calculate the precision, recall, and F1 score of each category.
    precision, recall, f1, _ = precision_recall_fscore_support(y_true, y_pre, average=None)
    return acc, precision, recall, f1

if __name__ == '__main__':
    # Read train.csv
    train_id_list, train_data_label, train_data_matrix, vocab = read_data("data/train.csv")
    # Divide train.csv into train set(80%) and validation set (20%).
    X_train, X_validation, y_train, y_validation = train_test_split(train_data_matrix, train_data_label, test_size=0.2)
    print("Vocabulary Size:", len(vocab))
    print("Training Set Size:", len(y_train))
    print("Validation Set Size:", len(y_validation))
    # Read test.csv
    test_id_list, _, test_data_matrix, _ = read_data("data/test.csv", vocab)
    print("Test Set Size:", len(test_id_list))

    # training
    P_y, P_xy = train_NB(y_train, X_train)
    train_data_pre = predict_NB(X_train, P_y, P_xy)
    acc, precision, recall, f1 = evaluate(y_train, train_data_pre)
    print("Evalution in train set: Accuracy: %f\tPrecision: %f\tRecall: %f\tMacro-F1: %f" % (acc, precision, recall, f1))

    # validation
    validation_data_pre = predict_NB(X_validation, P_y, P_xy)
    acc, precision, recall, f1 = evaluate(y_validation, validation_data_pre)
    print("Evalution in Validation set: Accuracy: %f\tPrecision: %f\tRecall: %f\tMacro-F1: %f" % (acc, precision, recall, f1))

    acc, precision, recall, f1 = evaluate_each_category(y_validation, validation_data_pre)
    print("Evalution in test set in each category: \nAccuracy:",acc,"\nPrecision:",precision, "\nRecall:",recall, '\nF1:',f1)

    # Predict the label of each documents in the test set
    test_data_pre = predict_NB(test_data_matrix, P_y, P_xy)

    sub_df = pd.DataFrame()
    sub_df["id"] = test_id_list
    sub_df["pred"] = test_data_pre
    sub_df.to_csv("submission.csv", index=False)
    print("Predict Results are saved, please check the submission.csv")


from subprocess import run

class Video:
    """Render video with ffmpeg"""

    @staticmethod
    def render_chat(_input, output, framerate = 1):
        """Render a window of chat messages to video."""
        command = [
            'ffmpeg',
            '-v',
            'warning',
            '-framerate',
            f'{framerate}',
            '-i',
            _input,
            output,
        ]
        run(command, check=True, text=True)

    @staticmethod
    def overlay(video, chat, opacity, position, output):
        """Overlay the chat window onto the video at the specified position (top left corner by default)."""
        command = [
            'ffmpeg',
            '-v',
            'warning',
            '-i',
            video,
            '-i',
            chat,
            '-filter_complex',
            f'[1]format=yuva444p,colorchannelmixer=aa={opacity}[in2];[0][in2]overlay={position[0]}:{position[1]}',
            output,
        ]
        run(command, check=True, text=True)

    @staticmethod
    def gen_dry_run(video):
        """Cut a video down to 30 seconds"""
        command = [
            'ffmpeg',
            '-v',
            'warning',
            '-to',
            '00:30',
            '-i',
            video,
            '-codec',
            'copy',
            '30_sec_test.mp4',
        ]
        run(command, check=True, text=True)


guess_num = 0
secerate_num = 14
try_again = False
inp = ""

print("Guess a number between one 1 - 50")

def tryAgain():
     print("Do you want to continue guessing? yes/no ")
     inp = str(input("=> choose: "))
     if inp == "yes" or "Yes" or "YES":
        try_again = True
        print("try again!")
     elif inp == "no" or "NO" or "No":
        print("Thanks for using the program! bye")
        try_again
        #break

while guess_num !=secerate_num:
    guess_num = int(input("Quess the secrete number: "))
    if guess_num <= 50 and guess_num == secerate_num:
       print("Your are right!")
    else:
        print("Error! Your number is outside the range")
        #print(try_again())
        if try_again:
           break
        else:
           tryAgain()


from torch.utils import data
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms as T
import torch

class MNIST_Data(data.Dataset):
    def __init__(self, data, labels):
        super().__init__()
        self.data = data
        self.labels = labels

    def __len__(self):
        return len(self.data)

    def __getitem__(self, index):
        x = self.data[index]
        y = self.labels[index]
        return x,y

def load_mnist(mode):
    transform = []
    transform.append(T.ToTensor())
    transform.append(T.Normalize(mean=[0.5], std=[0.5]))
    transform = T.Compose(transform)
    if mode == "train":
        dataset = datasets.MNIST(root="../data", train=True, transform=transform, download=True)
    elif mode == "test":
        dataset = datasets.MNIST(root="../data", train=False, transform=transform, download=True)

    x = dataset.data.float()
    y = dataset.targets

    return x,y

def get_loader(batch_size, mode, num_workers):
    x,y = load_mnist(mode)
    datasets = MNIST_Data(x,y)
    data_loader = DataLoader(dataset = datasets,
                            batch_size = batch_size,
                            shuffle = (mode=="train"),
                            num_workers = num_workers)
    return data_loader


# Zachary Hoover || Independent Practice: #20
# 11-30-22
import os
from inspect import getmembers, isfunction

# repeating code
###############

global clear
clear = lambda: os.system('clear' if os.name == 'posix' else 'cls')
funcName = ""

def printHeader():
  clear()
  print("\n    Independent Practice: #20   ")
  print(" -----------------+-----------------")
  print(f" Function: {funcName} \n")
  return

def returnMain():
  print("\n Press Enter to continue")
  input()
  autoMenu()

################################################
# start functions here

def deli():
  printHeader()

  # Create a list of sandwich orders and finished sandwiches
  sandwich_orders = ["ham and cheese", "italian", "baloney", "BLT", "turkey club"]
  finished_sandwiches = []

  # Cycle through sandwich list, print message, add to finished sandwhich
  while sandwich_orders:
    current = sandwich_orders.pop()

    print(f" I made your {current} sandwich")
    finished_sandwiches.append(current)

  # print complete finished sandwiches list
  print("\n Finished Sandwiches:", finished_sandwiches)

  returnMain()

def no_pastrami():
  printHeader()

  # Create a list of sandwich orders and alert customers of the lack of pastrami
  sandwich_orders = ["ham and cheese", "pastrami", "italian", "pastrami","baloney", "BLT", "turkey club", "pastrami",]
  print(" The deli has run out of pastrami! \n if your order contains pastrami, it will be removed.")

  i = 0
  # remove all occurances of "pastrami" in the list
  while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami")
    i += 1

  # Print the remaining items
  print("\n Remaining orders:", sandwich_orders)
  print(f" {i} orders removed.")

  returnMain()

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

menuMode = 0
currentBench = -1

def autoMenu():
  global menuMode
  global currentBench
  global usable
  global funcName

  funcName = "autoMenu"

  if menuMode == 1:
    if currentBench > len(usable)-1:
      menuMode = 0
      currentBench = -1
      autoMenu()
    else:
      currentBench += 1
      funcName = usable[currentBench][0]
      usable[currentBench][1]()

  if menuMode == 2:
    if currentBench > len(usable)-1:
      menuMode = 0
      currentBench = -1
      autoMenu()
    else:
      currentBench += 1
      funcName = usable[currentBench][0]
      usable[currentBench][1]()

  else:
    printHeader()

    # change were it says 'baseTemplate' to module name.
    import IndependentPractice20 as foo
    list = []
    list = getmembers(foo, isfunction)

    i = 0
    x = 0
    exceptions = [
      "autoMenu",
      "printHeader",
      "getmembers",
      "clear",
      "time",
      "isfunction",
      "returnMain",
      "cls",
    ]

    usable = []

    print(">> ----+ Functions +---- <<")
    while i < len(list):
      if list[i][0] not in exceptions:
        x += 1
        print(f"  {x}. {list[i][0]}")
        usable.append(list[i])
      i += 1

    print("\n>> ----+ Utilities +---- <<")
    print(f"  {x+1}. Exit program")
    print(f"  {x+2}. Benchmark (run all)")
    print(f"  {x+3}. Debug (to be added later)")

    try:
      usrin = int(input("\n Enter number of the item: "))
      if usrin == len(usable) + 1:
        SystemExit()
      elif usrin == len(usable) + 2:
        menuMode = 1
        print("Bench")
        autoMenu()
      elif usrin == len(usable) + 3:
        menuMode = 2
        print("Debug")
        autoMenu()
      else:
        funcName = usable[usrin - 1][0]
        usable[usrin - 1][1]()

    except:
      autoMenu()

# auto menu ends

if __name__ == "__main__": autoMenu();


import random
import os

class MessageHandler(object):
    def __init__(self, msg_recipient, spam_folder):
        self.spam_folder = spam_folder
        self.msg_recipient = msg_recipient

    def createCurrentMessage(self):
        spam = random.choice(os.listdir(self.spam_folder + '/'))
        with open(self.spam_folder + '/' + spam, encoding = "ISO-8859-1") as m:
            try:
                lines = m.readlines()
                with open('message.current', 'w') as f:
                    msg = self.replaced('startswith', lines, 'To: ', 'To: ' + self.msg_recipient + '\n')
                    f.write("".join(msg))
                    for line in lines:
                        if 'From: ' in line:
                            try:
                                sender = line.split(':')[1]
                                sender = sender.split('<')[1]
                                sender = sender.split('>')[0]
                            except Exception as e:
                                print('Failed: ' + str(e))
                                return 'Failed'
                        if 'Subject: ' in line:
                            try:
                                subject = line.split(':')[1]
                            except Exception as e:
                                print('Failed: ' + str(e))
                                return 'Failed'

            except Exception as e:
                print('Failed: ' + str(e))
                return 'failed'

        return sender, subject

    def replaced(self, mode, sequence, old, new):
        if mode == 'startswith':
            return (new if x.startswith(old) else x for x in sequence)

        if mode == 'in':
            return (new if old in x else x for x in sequence)


import argparse
import cv2
import json
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
import os
import random
import tkinter

NUM_IMAGES = 100
MAX_NUM_SHAPES = 20
TASKPATH = './eyespy/Data/Tasks/Shapes/'
GROUND_TRUTH = 'ground_truths'
META_DATA = 'meta_data.txt'
# SHAPES = ['line', 'rectangle', 'circle', 'ellipse', 'polygon']
SHAPES = ['line', 'rectangle', 'circle', 'ellipse']
COLORS = ['red', 'blue', 'green']
COLORMAP = {'red': (0, 0, 255), 'green': (0, 255, 0), 'blue': (255, 0, 0)} # BGR

def main(opt):
    metadata = {}
    if not os.path.exists(opt.task_path):
        os.makedirs(opt.task_path)
    for idx in range(opt.num_images):
        print('Creating image number: {}'.format(idx + 1))
        image_name = str(idx + 1) + '.png'
        height, width = get_screen_resolution()
        image, shapes = random_image(height, width, opt.max_num_shapes)
        # if np.random.uniform(0, 1) > 0.5:
        #     image = np.rot90(image, 2, (0, 1))
        metadata[idx + 1] = shapes
        # cv2.namedWindow("Image", cv2.WND_PROP_FULLSCREEN)
        # cv2.setWindowProperty("Image",cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        # cv2.imshow('Image', image)
        # cv2.waitKey(0)
        # print(os.path.join(opt.task_path, image_name))
        cv2.imwrite(os.path.join(opt.task_path, image_name), image)
    with open(os.path.join(opt.task_path, META_DATA), 'w') as f:
        json.dump(metadata, f)

def get_screen_resolution():
    root = tkinter.Tk()
    width = root.winfo_screenwidth()
    height = root.winfo_screenheight()
    return height, width

def random_image(height, width, max_num_shapes):
    image = np.ones((height, width, 3), np.uint8)
    num_shapes = random.randint(1, max_num_shapes)
    shapes = {}
    for idx in range(num_shapes):
        shape = random.choice(SHAPES)
        # TODO: document width and height vs. x and y
        # image, shape_info = draw_shape(image, shape, height, width)
        image, shape_info = draw_shape(image, shape, width, height)
        shapes[idx] = shape_info
    shapes['num_shapes'] = num_shapes
    return image, shapes

def draw_shape(image, shape, height, width):
    """
    Draws shapes on to the image.
    Args:
        image:  (npy): Image to which shapes are to be added
        shape:  (str): Name of shape to be drawn
        height  (int): y-dimension of the image
        width   (int): x-dimension of the image
    Returns:
        image:  (npy): Image to which shapes have been added
    """
    color = random.choice(COLORS)
    if shape == 'line':
        start = (random.randint(0, height - 1), random.randint(0, width - 1))
        end = (random.randint(0, height - 1), random.randint(0, width - 1))
        image = cv2.line(image, start, end, COLORMAP[color], 4)
    elif shape == 'rectangle':
        leftTop = (random.randint(2, height - 2), random.randint(2, width - 2))
        rightBot = (random.randint(leftTop[0], min(height - 1, leftTop[0] + height//4)),
                    random.randint(leftTop[1], min(width - 1, leftTop[1] + width//4)))
        image = cv2.rectangle(image, leftTop, rightBot, COLORMAP[color], -1)
    elif shape == 'circle':
        radius = random.randint(2, int(min(height, width)//8))
        centre = (random.randint(radius, height - radius - 1), random.randint(radius, width - radius - 1))
        image = cv2.circle(image, centre, radius, COLORMAP[color], -1)
    elif shape == 'ellipse':
        minorAxis = random.randint(2, int(min(height, width)//12))
        majorAxis = random.randint(minorAxis, 2*minorAxis)
        axes = (minorAxis, majorAxis)
        centre = (random.randint(majorAxis, height - majorAxis - 1), random.randint(majorAxis, width - majorAxis - 1))
        image = cv2.ellipse(image, centre, axes, np.random.uniform(0, 360), 0.0, 360.0, COLORMAP[color], -1);
    elif shape == 'polygon':
        num_pts = random.randint(3, 6)
        pts = np.array([[random.randint(0, height - 1), random.randint(0, width -1)] for _ in range(num_pts)],
                np.int32)
        pts = pts.reshape((-1,1,2))
        image = cv2.polylines(image, [pts], True, COLORMAP[color])
    shape_info = {'shape': shape, 'color': color}
    return image, shape_info

def crop_resize(image, size: int=224):
    """
    Centre crops and resizes an image.
    Args:
        image           (npy): Image to be cropped and resized
        size            (int): Image to be resized to [size, size, 3]
    Returns:
        image_resized   (npy): Image resized to (size, size, channels)
    """
    H, W, C = image.shape
    assert min(H, W) >= size, 'Video/image resolution %ix%itoo low, size should be atleast %ix%i' % (H, W, size, size)
    if H > W:
        image_crop = image[H//2 - W//2 - 1:H//2 + W//2 - 2, ...]
    elif H < W:
        image_crop = image[:, W//2 - H//2 - 1:W//2 + H//2 - 2, ...]
    else:
        image_crop = image
    D, _, _ = image_crop.shape
    image_resized = cv2.resize(image_crop, (size, size), interpolation = cv2.INTER_AREA)
    return image_resized

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--task_path', type=str, default=TASKPATH, help='Path to output folder to save images(optional)')
    parser.add_argument('--num_images', default=NUM_IMAGES, type=int, help='Number of task images to be created')
    parser.add_argument('--max_num_shapes', default=MAX_NUM_SHAPES, type=int, help='Maximum number of shapes per image')
    args = parser.parse_args()
    # print(args)
    main(args)


import json
from airflow import DAG
from datetime import datetime,timedelta
from airflow.providers.http.operators.http import SimpleHttpOperator
from airflow.providers.http.sensors.http import HttpSensor
from airflow.operators.python import PythonOperator
import pandas as pd
import os

def kelvin_to_farenheit(temp_in_kelvin):
    temp_in_farenheit = (temp_in_kelvin - 273.15) * (9/5) + 32
    return temp_in_farenheit

def transform_load_data(task_instance):
    data = task_instance.xcom_pull(task_ids ="extract_api_data")
    city = data["name"]
    weather_description = data["weather"][0]['description']
    temp_farenheit = kelvin_to_farenheit(data["main"]["temp"])
    feels_like_farenheit = kelvin_to_farenheit(data["main"]["feels_like"])
    min_temp_farenheit = kelvin_to_farenheit(data["main"]["temp_min"])
    max_temp_farenheit = kelvin_to_farenheit(data["main"]["temp_max"])
    pressure = data["main"]["pressure"]
    humidity = data["main"]["humidity"]
    wind_speed = data["wind"]["speed"]
    time_of_record = datetime.utcfromtimestamp(data['dt'] + data["timezone"])
    sunrise_time = datetime.utcfromtimestamp(data["sys"]["sunrise"] + data["timezone"])
    sunset_time = datetime.utcfromtimestamp(data["sys"]["sunset"] + data["timezone"])

    transformed_data = {
        "City" : city,
        "Description" : weather_description,
        "Temperature (F)" : temp_farenheit,
        "Feels Like (F)" : feels_like_farenheit,
        "Minimum Temp (F)" : min_temp_farenheit,
        "Maximum Temp (F)" : max_temp_farenheit,
        "Pressure" : pressure,
        "Humidity" : humidity,
        "Wind Speed" : wind_speed,
        "Time of Record" : time_of_record,
        "Sunrise (Local Time)" : sunrise_time,
        "Sunset (Local Time)" : sunset_time
    }

    transformed_data_list = [transformed_data]
    df_data = pd.DataFrame(transformed_data_list)

    now = datetime.now()
    dt_string = now.strftime("%d%m%Y%H%M%S")
    file_name = 'current_weather_data_Mumbai' + dt_string + '.csv'
    current_directory = os.path.dirname(__file__)
    file_path = os.path.join(current_directory, file_name)
    df_data.to_csv(file_path, index=False)

default_args = {
    'owner' : 'vishal',
    'depends_on_past' : False,
    'start_date' : datetime(2023,9,22),
    'retries' : 5,
    'retry_delay' : timedelta(minutes = 2)
}

with DAG(
    dag_id = 'weather_api',
    default_args = default_args,
    schedule_interval = '@daily',
    catchup = False
)as dag:
    task1 = HttpSensor(
        task_id = 'weather_api_ready',
        http_conn_id = 'weathermap_api',
        endpoint = '/data/2.5/weather?q=Mumbai&appid={api_key}'
    )

    task2 = SimpleHttpOperator(
        task_id = 'extract_api_data',
        http_conn_id = 'weathermap_api',
        endpoint = '/data/2.5/weather?q=Mumbai&appid={api_key}',
        method="GET",
        response_filter=lambda r : json.loads(r.text),
        log_response=True
    )

    task3 = PythonOperator(
        task_id = 'transform_load_weather_data',
        python_callable = transform_load_data
    )

    task1 >> task2 >> task3


"""
                            - Generators -

PROGRAMMED BY: Jean Flaherty
DATE: 07/14/2017
DESCRIPTION:
    Utility functions for generators.
"""
from .ansi import ANSI

def print_generator(label, gen, period=1, end="\n"):
    """
    A generator that prints the value generated by a given generator
    as they are generated.

    ARGUMENTS:
        label  - Text that goes before the printed value.
        gen    - The generator to print.
        period - (Optional) How often the generated value should be printed.
                 Useful when printing slows run time.
        end    - (Optional) Defaults to "\n" and is used for print(args**, end)
    """
    try:
        i = 0
        while True:
            try:
                value = next(gen)
            except StopIteration:
                return
            if i % period == 0:
                print(label + "{0: <40}".format(str(value).rstrip("\n"))[:40], end=end)
            yield value
            i += 1
    finally:
        print(ANSI.CLEAR_LINE, end="\r")

def print_progress_generator(label, gen, total, period=1):
    """
    A generator that prints the value generated by a given generator
    as they are generated.

    ARGUMENTS:
        label  - Text that goes before the printed value.
        gen    - The generator to print.
        period - (Optional) How often the generated value should be printed.
                 Useful when printing slows run time.
        end    - (Optional) Defaults to "\n" and is used for print(args**, end)
    """
    try:
        i = 0
        while True:
            if (i % period == 0 or i+1 == total) and total > 0:
                print(label + "{:.2f}%".format(100.0*float(i+1)/float(total)), end="\r")
            try:
                yield next(gen)
            except StopIteration:
                return
            i += 1
    finally:
        print()


#coding: utf-8
#author: mau

from entityGame.Entity import Entity
from core.logic import Logic

class Character(Entity):
    def __init__(self,pos,size,ability_base,tree_ability,textura,**kargs):
        Entity.__init__(self,pos,size,ability_base,tree_ability,textura,**kargs)
        self._solid = True
        self._conteo_pasos = []

    def set_animation(self,state):
        self.bg_rect.set_animation(state)

    def update(self,size,dt):
        super().update(size,dt)

    def move(self,direction,check,dt):
        self.set_direction_default(direction)
        currentx = self.pos[0]
        currenty = self.pos[1]
        prueba = self.get_ability_base_unique("speed")*dt

        moving_x= (self.get_ability_base_unique("speed")+Logic.momevements_percentage(self.size)[0] )* dt
        moving_y = (self.get_ability_base_unique("speed")+Logic.momevements_percentage(self.size)[1]) *dt

        ultimo_movimiento = [currentx,currenty]

        self._check_pasos(ultimo_movimiento)
        if not check:
            self.set_animation(True)
            if direction  == "up":
                currenty+=moving_y
            if direction == "down":
                currenty-=moving_y
            if direction == "left":
                currentx-=moving_x
            if direction == "right":
                currentx+=moving_x
            self.pos=(currentx,currenty)

        else:
            if len(self._conteo_pasos) >= 2:
                self.pos=(self._conteo_pasos[-2])

    def _check_pasos(self,paso):
        if len(self._conteo_pasos) >= 30:
            self._conteo_pasos.pop(0)
            self._conteo_pasos.append(paso)
        else:
            self._conteo_pasos.append(paso)


import pdb
import json
from tensorboardX import SummaryWriter
# import cv2
import sys
# from PIL import Image
from binmanager import *
# import matplotlib.pyplot as pyplot
import os
# import scipy
# os.environ["SDL_VIDEODRIVER"] = "dummy"
# sys.path.append("game/")
# import wrapped_flappy_bird as game
import random
import numpy as np
from collections import deque
import torch
from torch.autograd import Variable
import torch.nn as nn
writer = SummaryWriter("./boardx/boardx0813")
binW =  20
binH  = 10
binV =  4
itemV  = 5
# GAME = 'bird' # the name of the game being played for log files
# ACTIONS = 2 # number of valid actions
GAMMA = 0.99 # decay rate of past observations
OBSERVE = 1000. # timesteps to observe before training
EXPLORE = 50000. # frames over which to anneal epsilon
FINAL_EPSILON = 0.01 # final value of epsilon
INITIAL_EPSILON = 0.2 # starting value of epsilon
REPLAY_MEMORY = 1000 # number of previous transitions to remember
BATCH_SIZE = 128 # size of minibatch
FRAME_PER_ACTION = 1
UPDATE_TIME = 1000
# width = 80
# height = 80

class DeepNetWork(nn.Module):
    def __init__(self,):
        super(DeepNetWork,self).__init__()
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels=binV, out_channels=32, kernel_size=3, stride=1, padding=2),
            # nn.ReLU(inplace=True),
            # nn.MaxPool2d(kernel_size=2)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1),
            # nn.ReLU(inplace=True)
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
            # nn.ReLU(inplace=True)
        )
        self.fc1 = nn.Sequential(
            # nn.Linear(800*binV+itemV*2,512),
            nn.Linear(16906,binV*itemV*4),
            nn.ReLU()
        )
        self.out = nn.Linear(binV*itemV*4,binV*itemV*2)

    def forward(self, binInfo,itemInfo):
        binInfo = self.conv1(binInfo)
        binInfo = self.conv2(binInfo)
        binInfo = self.conv3(binInfo)
        binInfo = binInfo.view(binInfo.size(0),-1)
        itemInfo = itemInfo.view(itemInfo.size(0),-1)
        allInfo = torch.cat((binInfo,itemInfo ),dim = 1 )
        allInfo = self.fc1(allInfo);
        return self.out(allInfo)
class BrainDQNMain(object):
    def save(self):
        print("save model param")
        torch.save(self.Q_new.state_dict(), str(self.timeStep)+'params3.pth')

    def load(self,model):
        if os.path.exists(model):
            print("load model param")
            self.Q_new.load_state_dict(torch.load(model))
            self.Q_old.load_state_dict(torch.load(model))
    def loadWithPath(self,modelPath):
        if os.path.exists(modelPath):
            print("load model param")
            self.Q_new.load_state_dict(torch.load(modelPath))
            self.Q_old.load_state_dict(torch.load(modelPath))
    def __init__(self,actions,saveAndRun = True, model = ""):
        self.replayMemory = deque() # init some parameters
        self.timeStep = 0
        self.epsilon = INITIAL_EPSILON
        self.actions = actions
        self.Q_new=DeepNetWork()
        self.Q_old=DeepNetWork()
        if model!="":
            self.load(model)
        else:
            print("init model")

        self.loss_func=nn.MSELoss()
        LR=1e-6
        self.optimizer = torch.optim.Adam(self.Q_new.parameters(), lr=LR)
        if saveAndRun:
            self.saveAndRun(-1)

    def train(self): # Step 1: obtain random minibatch from replay memory
        minibatch = random.sample(self.replayMemory, BATCH_SIZE)
        state_batch = [data[0] for data in minibatch]
        action_batch = [data[1] for data in minibatch]
        reward_batch = [data[2] for data in minibatch]
        nextState_batch = [data[3] for data in minibatch]
         # Step 2: calculate y
        y_batch = np.zeros([BATCH_SIZE,1])

        # action_batch=np.array(action_batch)
        # index=action_batch.argmax(axis=0)
        # print("action "+str(index))
        # index=np.reshape(index,[BATCH_SIZE,1])
        # action_batch_tensor=torch.LongTensor(index)

        # nextState_batch = [data[3] for data in minibatch]
        # nextState_batch=np.array(nextState_batch) #print("train next state shape")
        # nextState_batch=torch.Tensor(nextState_batch)
        # QValue_batch = self.Q_old(nextState_batch)
        # QValue_batch=QValue_batch.detach().numpy()

# 实验数据

        currentBinState = torch.Tensor([data[0] for data in  nextState_batch])
        currentItemState = torch.Tensor([data[1] for data in  nextState_batch])
        QValue_batch = self.Q_new(currentBinState ,currentItemState )
        QValue_batch = QValue_batch.detach().numpy()

        for i in range(0, BATCH_SIZE):
            terminal = minibatch[i][4]
            if terminal:
                y_batch[i][0]=reward_batch[i]
            else:
                # 这里的QValue_batch[i]为数组，大小为所有动作集合大小，QValue_batch[i],代表
                # 做所有动作的Q值数组，y计算为如果游戏停止，y=rewaerd[i],如果没停止，则y=reward[i]+gamma*np.max(Qvalue[i])
                # 代表当前y值为当前reward+未来预期最大值*gamma(gamma:经验系数)
                y_batch[i][0]=reward_batch[i] + GAMMA * np.max(QValue_batch[i])

        y_batch=np.array(y_batch)
        y_batch=np.reshape(y_batch,[BATCH_SIZE,1])
        fun_value=Variable(torch.Tensor(y_batch))

        # state_batch_tensor=Variable(torch.Tensor(state_batch))
        # print("self.Q_new(state_batch_tensor): ",self.Q_new(state_batch_tensor))
        # model_predict_value=self.Q_new(state_batch_tensor).gather(1,action_batch_tensor)

# 实验数据
        currentBinState2 = torch.Tensor([data[0] for data in  state_batch])
        currentItemState2 = torch.Tensor([data[1] for data in  state_batch])
        model_predict_value = self.Q_new(currentBinState2 ,currentItemState2 ).gather(1,torch.LongTensor([[ac] for ac in action_batch]))
        # currentQValue_batch=currentQValue_batch.detach().numpy()

        loss=self.loss_func(model_predict_value,fun_value)
        # print(self.timeStep, " loss is ",str(loss))
        if self.timeStep%5==0:
            writer.add_scalar("0811模型损失曲线",loss,self.timeStep)
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        if self.timeStep%100 == 0 :
            print("self.timeStep:",self.timeStep,"loss ",loss)
        if self.timeStep % (UPDATE_TIME * 100) == 0:
            self.saveAndRun( -1 )
        elif self.timeStep % UPDATE_TIME == 0:
            self.saveAndRun( -1 )
    def saveAndRun(self, drawNum ):
        self.Q_old.load_state_dict(self.Q_new.state_dict())
        self.save()
        # 测试装箱效果
        reward , _  = self.runModel(drawNum,1)
        writer.add_scalar("reward收敛情况",reward,self.timeStep)
        # writer.add_scalar("0811效果测试",binNum,self.timeStep)
        print("reward收敛情况",reward,self.timeStep )
        # print("0811效果测试",binNum,self.timeStep )
    def setPerceptionAndTrain(self,nextObservation,action,reward,terminal): #print(nextObservation.shape)
        # newState = np.append(self.currentState[1:,:,:],nextObservation,axis = 0) # newState = np.append(nextObservation,self.currentState[:,:,1:],axis = 2)
        # 这是寻找连续动作下的状态  bin packing 不需要
        self.replayMemory.append((self.currentState,action,reward,nextObservation,terminal))
        if len(self.replayMemory) > REPLAY_MEMORY:
            self.replayMemory.popleft()
        if self.timeStep > OBSERVE: # Train the network
            self.train()

        # print info
        state = ""
        if self.timeStep <= OBSERVE:
            state = "observe"
        elif self.timeStep > OBSERVE and self.timeStep <= OBSERVE + EXPLORE:
            state = "explore"
        else:
            state = "train"
        # print ("TIMESTEP", self.timeStep, "/ STATE", state, "/ EPSILON", self.epsilon)
        self.currentState = nextObservation
        self.timeStep += 1

    def getAction(self, bm ,ifRandom = True, currentState=[]):
        if len(currentState)!=0:
            self.currentState = currentState
        currentBinState = torch.Tensor([self.currentState[0]])
        currentItemState = torch.Tensor([self.currentState[1]])
        QValue = self.Q_new(currentBinState ,currentItemState )[0]
        action = np.zeros(self.actions)
        if self.epsilon > FINAL_EPSILON and self.timeStep > OBSERVE:
            self.epsilon -= (INITIAL_EPSILON - FINAL_EPSILON) / EXPLORE
        if self.timeStep % FRAME_PER_ACTION == 0:
            if random.random() <= self.epsilon and ifRandom:
                action_index = random.randrange(self.actions)
                while True:
                    if bm._algoVItemIndexList[(action_index//2) % itemV ]<0 :
                        action_index = (action_index + 2) % self.actions
                    else:
                        break

                # print("choose random action " + str(action_index))
                return action_index , 0
            else:
                Q  =  QValue.detach().numpy()
                for i in range(Q.size):
                    if self.currentState[1][(i//2) % itemV ][0]<0:
                        Q[i]= - np.Inf
                action_index = np.argmax(Q)
                # print("choose qnet value action " + str(action_index))
                return action_index,Q[action_index]

        # change episilon
        raise("")
        return random.randrange(self.actions)

    def setInitState(self, observation):
        # self.currentState = np.stack((observation, observation, observation, observation),axis=0)
        self.currentState = observation
        # print(self.currentState.shape)
    def runModel(self,drawNum,askNum,isContinue = False,ifSwitchBatch = False):
        FINAL_EPSILON = 0
        # data = json.loads(open('./testData.json').read() )
        myEnv = env(binW,binH)
        rewardAll = 0
        binAll = 0
        ifDraw = drawNum > 0
        itemNum = itemV * 50
        for ii in range(askNum):
            # if ii>=askNum:
            #     break
            # ask = data[ii]
            bm= binManager(myEnv ,binV,itemV)
            for wh in range(itemNum):
                bm.AddRandomItem()
            if ifDraw :
                paper = Paper(myEnv.binWidth,myEnv.binHeight)

            # self.setInitState(bm.AllStatus())

            while True:
                if bm.placedNum == itemNum:
                    if ifDraw :
                        paper.Close()
                    break
                sM, itIList ,binIList = bm.AllStatus_RandItemBatch()
                action, value = self.getAction(bm,ifRandom = False,currentState=sM)
                binChose ,itemChose,rotationChose =   getChoseByAction(action)
                ifComp,ifSucc  = bm.Place( itIList[itemChose], binIList[binChose] ,rotationChose)

                # self.setInitState(bm.AllStatus())

                if ifDraw:
                    # drawNum = drawNum - 1
                    print("rotationChose :",rotationChose,"binChose :",binChose,"itemChose :",itemChose,"reward :",0,"ifComp :",ifComp)
                    if ifSucc:
                        bin = bm.bins[binIList[binChose]]
                        paper.AddRect(bin.placedRect[-1].X(),bin.placedRect[-1].Y(),bin.placedRect[-1].Width(),bin.placedRect[-1].Height(),binChose)
                        time.sleep(0.5)
                        paper.AddBlackRect(bin.placedRect[-1].X(),bin.placedRect[-1].Y(),bin.placedRect[-1].Width(),bin.placedRect[-1].Height(),binChose)
                    # if reward == -1:
                    #     print("选错item")
                    if not ifSucc:
                        if not isContinue:
                            break
                        else:
                            paper.Close()
                            paper = Paper(myEnv.binWidth,myEnv.binHeight)
                            for iii in range(binV):
                                bin = bm.bins[bm._algoVBinIndexList[iii]]
                                for placed in bin.placedRect:
                                    paper.AddBlackRect(placed.X(),placed.Y(),placed.Width(),placed.Height(),iii)

        return rewardAll ,binAll
def getChoseByAction(action):
    rotationChose =  action % 2
    binChose = (action//2) // itemV
    itemChose = (action//2) % itemV
    return binChose,itemChose, rotationChose
def runModel(modelPath,ifDraw,askNum):
        brain = BrainDQNMain(binV * itemV *2 , False,modelPath)

        if ifDraw:
            rewardAll ,binAll  = brain.runModel( 1000000,askNum,isContinue=True)
        else:
            rewardAll ,binAll  = brain.runModel( -1 ,askNum)
        print("rewardAll:",rewardAll)
        print("使用bin数量:",binAll)
def trainModel():
    actions = binV * itemV * 2
    random.seed(0)
    myEnv =env(binW,binH)

    while True:
        bm= binManager(myEnv ,binV,itemV)
        for i in range(itemV*5):
            bm.AddRandomItem()
            # if random.random() < 0.5:
            #     bm.AddItem(1,2)
            # else:
            #     bm.AddItem(2,1)

        brain = BrainDQNMain(actions,False) # Step 2: init Flappy Bird Game
        # status0 =  # Step 3: play game
        # # Step 3.1: obtain init state
        # terminal,reward0 = bm.Action(0,0)
        # status1 =  bm.AllStatus()

        brain.setInitState(bm.AllStatus())
        # print(brain.currentState.shape) # Step 3.2: run the game

        while 1!= 0:
            action,_ = brain.getAction(bm,ifRandom = True)
            binChose ,itemChose,rotationChose =   getChoseByAction(action)
            terminal,_,reward = bm.Action(itemChose,binChose,rotationChose)
            if brain.timeStep %100 ==0:
                print("rotationChose :",rotationChose,"binChose :",binChose,"itemChose :",itemChose,"reward :",reward,"terminal :",terminal)
            brain.setPerceptionAndTrain(bm.AllStatus(),action,reward,terminal)
            if terminal:
                bm= binManager(myEnv,binV,itemV)
                for i in range(itemV*5):
                    bm.AddRandomItem()
                    # if random.random() < 0.5:
                    #     bm.AddItem(1,2)
                    # else:
                    #     bm.AddItem(2,1)
                brain.setInitState(bm.AllStatus())

if __name__ == '__main__':
    # Step 1: init BrainDQN
    random.seed(0)
    # runModel("./120000params3.pth",False,1)
    runModel("./models/1392000params32.pth",True,10)
    # runModel("",True,1)
    # trainModel()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import numpy as np
import time
import sys
import paddle
import paddle.fluid as fluid
import reader as reader
import argparse
import functools
import models
from utils.learning_rate import cosine_decay
from utils.utility import add_arguments, print_arguments
import math

parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('batch_size',       int,  256,                 "Minibatch size.")
add_arg('use_gpu',          bool, True,                "Whether to use GPU or not.")
add_arg('class_dim',        int,  1000,                "Class number.")
add_arg('image_shape',      str,  "3,224,224",         "Input image size")
add_arg('with_mem_opt',     bool, True,                "Whether to use memory optimization or not.")
add_arg('pretrained_model', str,  None,                "Whether to use pretrained model.")
add_arg('model',            str,  "SE_ResNeXt50_32x4d", "Set the network to use.")

# yapf: enable

def eval(args):
    # parameters from arguments
    class_dim = args.class_dim
    model_name = args.model
    pretrained_model = args.pretrained_model
    with_memory_optimization = args.with_mem_opt
    image_shape = [int(m) for m in args.image_shape.split(",")]

    model_list = [m for m in dir(models) if "__" not in m]
    assert model_name in model_list, "{} is not in lists: {}".format(args.model,
                                                                     model_list)

    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')

    # model definition
    model = models.__dict__[model_name]()

    if model_name == "GoogleNet":
        out0, out1, out2 = model.net(input=image, class_dim=class_dim)
        cost0 = fluid.layers.cross_entropy(input=out0, label=label)
        cost1 = fluid.layers.cross_entropy(input=out1, label=label)
        cost2 = fluid.layers.cross_entropy(input=out2, label=label)
        avg_cost0 = fluid.layers.mean(x=cost0)
        avg_cost1 = fluid.layers.mean(x=cost1)
        avg_cost2 = fluid.layers.mean(x=cost2)

        avg_cost = avg_cost0 + 0.3 * avg_cost1 + 0.3 * avg_cost2
        acc_top1 = fluid.layers.accuracy(input=out0, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=out0, label=label, k=5)
    else:
        out = model.net(input=image, class_dim=class_dim)
        cost, pred = fluid.layers.softmax_with_cross_entropy(
            out, label, return_softmax=True)
        avg_cost = fluid.layers.mean(x=cost)
        acc_top1 = fluid.layers.accuracy(input=pred, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=pred, label=label, k=5)

    test_program = fluid.default_main_program().clone(for_test=True)

    fetch_list = [avg_cost.name, acc_top1.name, acc_top5.name]
    if with_memory_optimization:
        fluid.memory_optimize(
            fluid.default_main_program(), skip_opt_set=set(fetch_list))

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(pretrained_model, var.name))

        fluid.io.load_vars(exe, pretrained_model, predicate=if_exist)

    val_reader = paddle.batch(reader.val(), batch_size=args.batch_size)
    feeder = fluid.DataFeeder(place=place, feed_list=[image, label])

    test_info = [[], [], []]
    cnt = 0
    for batch_id, data in enumerate(val_reader()):
        t1 = time.time()
        loss, acc1, acc5 = exe.run(test_program,
                                   fetch_list=fetch_list,
                                   feed=feeder.feed(data))
        t2 = time.time()
        period = t2 - t1
        loss = np.mean(loss)
        acc1 = np.mean(acc1)
        acc5 = np.mean(acc5)
        test_info[0].append(loss * len(data))
        test_info[1].append(acc1 * len(data))
        test_info[2].append(acc5 * len(data))
        cnt += len(data)
        if batch_id % 10 == 0:
            print("Testbatch {0},loss {1}, "
                  "acc1 {2},acc5 {3},time {4}".format(batch_id, \
                  "%.5f"%loss,"%.5f"%acc1, "%.5f"%acc5, \
                  "%2.2f sec" % period))
            sys.stdout.flush()

    test_loss = np.sum(test_info[0]) / cnt
    test_acc1 = np.sum(test_info[1]) / cnt
    test_acc5 = np.sum(test_info[2]) / cnt

    print("Test_loss {0}, test_acc1 {1}, test_acc5 {2}".format(
        "%.5f"%test_loss, "%.5f"%test_acc1, "%.5f"%test_acc5))
    sys.stdout.flush()

def main():
    args = parser.parse_args()
    print_arguments(args)
    eval(args)

if __name__ == '__main__':
    main()


#---------------------------------------------------
# Daniel Geschwender, Yanfeng Liu, Jeevan Rajagopal
# CSCE 496/896: Deep Learning
# Homework 2
#---------------------------------------------------
# Based off code provided in Hackathon 3

import tensorflow as tf
import numpy as np
import math
import os
import util
import model

flags = tf.app.flags
flags.DEFINE_string('data_dir', '/work/cse496dl/shared/homework/02/', 'directory where EMODB/SAVEE is located')
flags.DEFINE_string('save_dir', 'model', 'directory where model graph and weights are saved')
flags.DEFINE_string('dataset', 'EMODB-German', 'dataset to run on')
flags.DEFINE_string('model_transfer', '', 'Where to load model to transfer from')
flags.DEFINE_integer('batch_size', 32, '')
flags.DEFINE_integer('max_epoch_num', 50, '')
flags.DEFINE_integer('patience', 0, '')
flags.DEFINE_string('filters', "2,0,4,0", '')
flags.DEFINE_string('linear_nodes', "", '')
flags.DEFINE_float('learning_rate', 0.001, '')
flags.DEFINE_float('dropout_rate', 0.2, '')
flags.DEFINE_float('l2_regularizer', 0.0, '')
flags.DEFINE_bool('output_model', False, '')
flags.DEFINE_float('data_fraction', 1.0, '')
flags.DEFINE_integer('fold', 0, '')
FLAGS = flags.FLAGS

def main(argv):
    print(tf.__version__)

    # handle command line arguments
    print("regularizer:",FLAGS.l2_regularizer)
    print("dropout_rate:",FLAGS.dropout_rate)
    print("learning_rate:",FLAGS.learning_rate)
    print("filters:",FLAGS.filters)
    print("linear_nodes:",FLAGS.linear_nodes)
    learning_rate = FLAGS.learning_rate
    dropout_rate = FLAGS.dropout_rate
    filters = list(map(int, FLAGS.filters.split(","))) if FLAGS.filters != "" else []
    linear_nodes = list(map(int, FLAGS.linear_nodes.split(","))) if FLAGS.linear_nodes != "" else []
    regularizer = tf.contrib.layers.l2_regularizer(scale=1.)
    folds = range(1,5) if FLAGS.fold == 0 else [FLAGS.fold]
    modelFile = "emodb_homework_2" if FLAGS.dataset == "EMODB-German" else "savee_homework_2"

    # specify the network
    if FLAGS.model_transfer == "":
        input, output, trainingMode = model.original_model(filters=filters, linear_nodes=linear_nodes, regularizer=regularizer, dropout_rate=dropout_rate)
    else:
        input, output, trainingMode = model.transfer_model(transfer=FLAGS.model_transfer, filters=filters, linear_nodes=linear_nodes, regularizer=regularizer, dropout_rate=dropout_rate)

    # define classification loss
    label = tf.placeholder(tf.uint8, [None, 7], name='label')
    cross_entropy  = tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=output)
    reduce_mean_cross_entropy = tf.reduce_mean(cross_entropy)
    regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    REG_COEFF = FLAGS.l2_regularizer
    total_loss = reduce_mean_cross_entropy + REG_COEFF * sum(regularization_losses)

    # setup confusion matrix and accuracy
    confusion_matrix_op = tf.confusion_matrix(tf.argmax(label, axis=1), tf.argmax(output, axis=1), num_classes=7)
    accuracy = tf.equal(tf.argmax(label, axis=1), tf.argmax(output, axis=1))
    reduce_mean_accuracy = tf.reduce_mean(tf.cast(accuracy, tf.float32))

    # set up training and saving functionality
    global_step_tensor = tf.get_variable('global_step', trainable=False, shape=[], initializer=tf.zeros_initializer)
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
    var_list = None if FLAGS.model_transfer == "" else tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, "new_layers")
    train_op = optimizer.minimize(total_loss, global_step=global_step_tensor, var_list=var_list)
    saver = tf.train.Saver()

    k_fold_accuracy = []

    for fold in folds:
        print("Beginning fold ", fold)

        # load data
        train_images = np.load(FLAGS.data_dir + FLAGS.dataset + '/train_x_' + str(fold) + '.npy')
        train_labels = np.load(FLAGS.data_dir + FLAGS.dataset + '/train_y_' + str(fold) + '.npy')
        validation_images = np.load(FLAGS.data_dir + FLAGS.dataset + '/test_x_' + str(fold) + '.npy')
        validation_labels = np.load(FLAGS.data_dir + FLAGS.dataset + '/test_y_' + str(fold) + '.npy')
        train_num_examples = train_images.shape[0]
        validation_num_examples = validation_images.shape[0]
        print("train size = ", train_num_examples)
        print("validation size = ", validation_num_examples)

        # reduce dataset according to data_fraction parameter
        train_images, _ = util.split_data(train_images, FLAGS.data_fraction)
        train_labels, _ = util.split_data(train_labels, FLAGS.data_fraction)
        validation_images, _ = util.split_data(validation_images, FLAGS.data_fraction)
        validation_labels, _ = util.split_data(validation_labels, FLAGS.data_fraction)

        # set up early stopping
        final_epoch = 0
        final_validation_ce = math.inf
        final_validation_acc = 0
        patience = FLAGS.patience
        wait = 0

        with tf.Session() as session:
            session.run(tf.global_variables_initializer())

            # run training
            batch_size = FLAGS.batch_size
            for epoch in range(FLAGS.max_epoch_num):
                print('Epoch: ' + str(epoch))

                # run gradient steps and report mean loss on train data
                ce_vals = []
                acc_vals = []
                for i in range(train_num_examples // batch_size):
                    batch_xs = train_images[i*batch_size:(i+1)*batch_size, :]
                    batch_ys = train_labels[i*batch_size:(i+1)*batch_size]
                    _, train_ce, train_acc = session.run([train_op, reduce_mean_cross_entropy, reduce_mean_accuracy], {input: batch_xs, label: batch_ys, trainingMode: True})
                    ce_vals.append(train_ce)
                    acc_vals.append(train_acc)
                avg_train_ce = sum(ce_vals) / len(ce_vals)
                avg_train_acc = sum(acc_vals) / len(acc_vals)
                print('TRAIN CROSS ENTROPY: ' + str(avg_train_ce))
                print('TRAIN ACCURACY: ' + str(train_acc))

                # report mean validation loss
                ce_vals = []
                acc_vals = []
                conf_mxs = []
                for i in range(validation_num_examples // batch_size):
                    batch_xs = validation_images[i*batch_size:(i+1)*batch_size, :]
                    batch_ys = validation_labels[i*batch_size:(i+1)*batch_size]
                    validation_ce, validation_acc, conf_matrix = session.run([reduce_mean_cross_entropy, reduce_mean_accuracy, confusion_matrix_op], {input: batch_xs, label: batch_ys})
                    ce_vals.append(validation_ce)
                    acc_vals.append(validation_acc)
                    conf_mxs.append(conf_matrix)
                avg_validation_ce = sum(ce_vals) / len(ce_vals)
                avg_validation_acc = sum(acc_vals) / len(acc_vals)
                print('VALIDATION CROSS ENTROPY: ' + str(avg_validation_ce))
                print('VALIDATION ACCURACY: ' + str(avg_validation_acc))
                print('VALIDATION CONFUSION MATRIX:')
                print(str(sum(conf_mxs)))

                # update final results
                if avg_validation_acc > final_validation_acc or patience == 0:
                    final_epoch = epoch
                    final_validation_ce = avg_validation_ce
                    final_validation_acc = avg_validation_acc
                    wait = 0
                    if FLAGS.output_model:
                        path_prefix = saver.save(session, os.path.join(FLAGS.save_dir, modelFile), global_step=0)
                else:
                    wait += 1
                    if wait == patience:
                        break

                # output tensorboard data
                if FLAGS.output_model and epoch==0:
                    file_writer = tf.summary.FileWriter(FLAGS.save_dir, session.graph)

            print("Results for fold", fold)
            print('Final Epoch: ' + str(final_epoch))
            print('Final VALIDATION CROSS ENTROPY: ' + str(final_validation_ce))
            print('Final VALIDATION ACCURACY: ' + str(final_validation_acc))
            k_fold_accuracy.append(final_validation_acc)

    # report average final accuracy across all k folds
    print('Average accuracy across k folds: '+str(sum(k_fold_accuracy) / len(k_fold_accuracy)))

if __name__ == "__main__":
    tf.app.run()


from cs50 import get_int
from cs50 import get_string
from sys import exit

def main():
    card_num = get_card_num()
    card_len = checksum(card_num)
    if (card_len == -1):
        print("INVALID")
        exit(1)
    print(id_provider(card_num, card_len))
    exit(0)

# Ask the user for the card number
def get_card_num():
    while True:
        num = get_int("Card number: ")
        # Only allow positive integers
        if num >= 1:
            break
    return num

def checksum(number):

    copy = number
    step_1_accum = 0
    step_2_accum = 0
    index = 0

    # Walk through the whole number
    while(copy > 0):

        # Get the last digit
        digit = copy % 10
        # Now, the action depends on the position of every digit
        # If 'index' is odd -> execute step #1
        # If 'index' is even -> execute step #2
        if ((index % 2) == 1):
            # Raise by two the digit and accum the result
            doubled = digit * 2
            # If the digit converts to a double digit number, add both digits
            while(doubled > 9):
                # Get the last digit and accum it
                step_1_accum += doubled % 10
                # Remove the processed digit
                doubled = doubled // 10

            # Accumulate the digit
            step_1_accum += doubled
        else:
            # Accum the digits as they are
            step_2_accum += digit

        # Remove the processed digit
        copy = copy // 10
        # Increment index
        index += 1

    # Finally, step #3
    # Add both accums and verify if last digit is 0
    # If valid return the number of digits the number has, else -1
    result = step_1_accum + step_2_accum
    if ((result % 10) == 0):
        return index
    else:
        return -1

def id_provider(number, number_len):

    # Keep only the two left-most digits
    for i in range(number_len - 2):
        number = number // 10

    provider = "INVALID"

    if ((number // 10) == 4 and (number_len == 13 or number_len == 16)):
        provider = "VISA"

    if (number_len == 15 and (number == 34 or number == 37)):
        provider = "AMEX"

    if (number_len == 16 and (number >= 51 and number <= 55)):
        provider = "MASTERCARD"

    return provider

main()


#O que está dentro da função é o escopo local, fora é o escopo global

escopo_global = "Tchau"  #variável global
numero = 10   #variável global

def teste():
    teste = 'Olá' #variável local
    numero = 10 #variável local
    print(teste)
    print(escopo_global)
    if numero == 10:
        print("Você ganhoU!")

teste()

escopo_global = "Até logo"
numero = 12

teste()

print(numero)

def testando():
    numero = 50
    print(numero)

testando()
teste()


'''
Module for cleaning data related to diseases and their symptoms.
'''
import warnings

import pandas as pd

warnings.simplefilter("ignore")

def clean_data(data_path, cleaned_data_path):
    """
    Cleans the data and saves the cleaned data to a csv file.

    Parameters:
    data_path (str): The path to the csv file containing raw data
    cleaned_data_path (str): The path where the cleaned data will be saved

    Returns:
    None
    """
    # Read the data
    data_frame = pd.read_csv(data_path)

    ##make sure theres no whitespace anywhere
    for i in data_frame.columns[1:]:
        data_frame[i] = data_frame[i].str.strip().str.replace(" ", "")

    ## create an array of symptoms for each disease
    data_frame["Symptoms"] = 0
    records = data_frame.shape[0]
    for i in range(records):
        values = data_frame.iloc[i].values
        values = values.tolist()
        if 0 in values:
            data_frame["Symptoms"][i] = values[1:values.index(0)]
        else:
            data_frame["Symptoms"][i] = values[1:]

    ## gather all the possible symptom values
    columns = data_frame[['Symptom_1', 'Symptom_2', 'Symptom_3', 'Symptom_4',
           'Symptom_5', 'Symptom_6', 'Symptom_7', 'Symptom_8', 'Symptom_9',
           'Symptom_10', 'Symptom_11', 'Symptom_12', 'Symptom_13', 'Symptom_14',
           'Symptom_15', 'Symptom_16', 'Symptom_17']].values.ravel()

    ## remove duplicates and empoty values
    unique_symptoms = pd.unique(columns)
    unique_symptoms = unique_symptoms.tolist()
    unique_symptoms = [i for i in unique_symptoms if str(i) != "nan"]

    ## make the symptoms a new data_frame
    symptoms = pd.DataFrame(columns = unique_symptoms, index = data_frame.index)

    symptoms["Symptoms"] = data_frame["Symptoms"]

    for symptom in unique_symptoms:
        symptoms[symptom] = symptoms.apply(lambda row: 1 if symptom in row.Symptoms else 0, axis=1)

    symptoms["Disease"] = data_frame["Disease"]
    symptoms = symptoms.drop("Symptoms",axis=1)

    return symptoms.to_csv(cleaned_data_path, index=False)


from Modules import Commits, Log

import time
import hashlib
from flask import Flask, render_template, url_for, send_from_directory, request

app = Flask('Portfolio')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

@app.route("/")
def home():
    Log.LogVisit(request)
    return render_template(
            "index.html",
            Events=Commits.GetCommits()
        )

@app.route('/favicon.ico')
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
    return send_from_directory(app.static_folder, request.path[1:])

if __name__ == "__main__":
    app.run(host='0.0.0.0',debug=True)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import argparse
import pickle
import csv
import logging
import numpy as np
import os
import sys

FORMAT = '[%(levelname)s: %(filename)s: %(lineno)4d]: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT, stream=sys.stdout)
logger = logging.getLogger(__name__)

from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import normalize
import matplotlib
matplotlib.use('pdf')
import matplotlib.pyplot as plt
import seaborn as sn
import pandas as pd

import torch
from torch import nn
from experiment_utils.csv_to_dict import csv_to_dict
from experiment_utils.argparse_utils import add_exp_arguments
from experiment_utils import ExperimentBuilder
from utils.video_accuracy import VideoAccuracy
import dataset_configs
import model_configs
import time

import config

vmin = -0.3
vmax = 0.3

def generate_remapped_labels(num_classes, labels_to_keep):
    new_label = 0

    remapped_labels = []

    num_classes_after_removal = len(labels_to_keep)
    assert num_classes_after_removal > 0

    for old_label in range(num_classes):
        if old_label not in labels_to_keep:
            remapped_labels.append(num_classes_after_removal)   # append `other`
        else:
            remapped_labels.append(new_label)
            new_label += 1

    return np.array(remapped_labels)

def remap_class_keys(class_keys, remapped_labels, num_classes_after_removal):
    new_class_keys = [''] * (num_classes_after_removal+1)#{{{
    new_class_keys[-1] = 'other'
    for i, new_label in enumerate(remapped_labels):
        new_class_keys[new_label] = class_keys[i]

    new_class_keys[-1] = 'other'

    return pd.DataFrame(new_class_keys, columns = ['class_key'])['class_key']#}}}

def remap_labels(labels, remapped_labels):
    def convert_func(label):
        return remapped_labels[label]

    return np.vectorize(convert_func)(labels)

def shrink_labels(num_classes, labels_to_keep, class_keys, pred_labels, video_labels, sort_labels, class_frequency_in_train):
    num_classes_after_removal = len(labels_to_keep)

    remapped_labels = generate_remapped_labels(num_classes, labels_to_keep)
    new_class_keys = remap_class_keys(class_keys, remapped_labels, num_classes_after_removal)
    new_pred_labels = remap_labels(pred_labels, remapped_labels)
    new_video_labels = remap_labels(video_labels, remapped_labels)
    new_sort_labels = remap_labels(sort_labels, remapped_labels)
    # remove "other" on sort_labels and put it at the end
    new_sort_labels = new_sort_labels[new_sort_labels < num_classes_after_removal]
    new_sort_labels = np.append(new_sort_labels, num_classes_after_removal)

    new_class_frequency_in_train = np.zeros(num_classes_after_removal+1, dtype=np.int32)
    for i, num in enumerate(class_frequency_in_train):
        new_class_frequency_in_train[remapped_labels[i]] += num

    return new_class_keys, new_pred_labels, new_video_labels, new_sort_labels, new_class_frequency_in_train

def generate_confusion_matrix(args, sort_method, shrink=0):
    """save confusion matrix
    Params:
        shrink (int): set to 0 if you don't want to shrink the matrix. n for the first n, -n for the last n.

    """

    dataset_cfg = dataset_configs.load_cfg(args.dataset)

    exp = ExperimentBuilder(args.experiment_root, args.dataset, args.model, args.experiment_name, args.subfolder_name)
    exp.load_summary()

    def load_predictions(load_epoch, mode, exp):
        real_load_epoch = None
        if load_epoch == -1:
            real_load_epoch = int(exp.summary['epoch'][-1])
        elif load_epoch == -2:
            if mode == 'oneclip':
                field = 'val_acc'
            else:   # multicrop
                field = 'multi_crop_val_vid_acc_top1'
            real_load_epoch = int(exp.get_best_model_stat(field)['epoch'])
        elif load_epoch >= 0:
            real_load_epoch = load_epoch
        else:
            raise ValueError("Wrong load_epoch value: {:d}".format(load_epoch))

        predictions_file_path = os.path.join(exp.predictions_dir, 'epoch_%04d_%sval.pkl' % (real_load_epoch, mode))
        with open(predictions_file_path, 'rb') as f:
            predictions = pickle.load(f)

        video_predictions = predictions['video_predictions']
        video_labels = predictions['video_labels']
        #video_ids = predictions['video_ids']

        return video_predictions, video_labels

    video_predictions, video_labels = load_predictions(args.load_epoch, args.mode, exp)

    ## 2
    exp2 = ExperimentBuilder(args.experiment_root, args.dataset, args.model2, args.experiment_name2, args.subfolder_name2)
    exp2.load_summary()

    video_predictions2, video_labels2 = load_predictions(args.load_epoch2, args.mode2, exp2)

    pred_labels = np.argmax(video_predictions, axis=1)
    pred_labels2 = np.argmax(video_predictions2, axis=1)
    '''
    for i in range(10):
        print(video_predictions[i].max())
        print(video_predictions[i].min())
        print(video_labels[i])
        print()
    '''

    class_indices = range(dataset_cfg.num_classes)
    class_keys = dataset_cfg.class_keys

    def grouped_accuracy(pred_labels, video_labels):
        num_total_val_samples = {}
        num_correct_pred = {}
        for pred, label in zip(pred_labels, video_labels):
            class_key = class_keys[label]
            first_word = class_key.split(' ', 1)[0]

            if pred == label:
                if first_word in num_correct_pred.keys():
                    num_correct_pred[first_word] += 1
                    num_total_val_samples[first_word] += 1
                else:
                    num_correct_pred[first_word] = 1
                    num_total_val_samples[first_word] = 1
            else:
                if first_word in num_correct_pred.keys():
                    num_total_val_samples[first_word] += 1
                else:
                    num_correct_pred[first_word] = 0
                    num_total_val_samples[first_word] = 1

        return num_correct_pred, num_total_val_samples

    num_correct_pred, num_total_val_samples = grouped_accuracy(pred_labels, video_labels)
    num_correct_pred2, num_total_val_samples2 = grouped_accuracy(pred_labels2, video_labels2)

    diff_correct_pred = {}
    diff_accuracy = {}
    for first_word in num_correct_pred.keys():
        diff_correct_pred[first_word] = num_correct_pred2[first_word] - num_correct_pred[first_word]
        diff_accuracy[first_word] = (num_correct_pred2[first_word] - num_correct_pred[first_word]) / num_total_val_samples[first_word]

    index = np.arange(len(diff_correct_pred.keys()))

    #sort_index = np.fromiter(diff_correct_pred.values(), dtype=np.int).argsort()[::-1]
    sort_index = np.fromiter(diff_accuracy.values(), dtype=np.float).argsort()

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.barh(index, np.fromiter(diff_correct_pred.values(), dtype=np.int)[sort_index], height=1, color='b')
    #ax.bar(index, diff_correct_pred.values(), width=1, color='b')
    #plt.ylabel('Class Group', fontsize=12)
    plt.xlabel('Correct sample count difference', fontsize=12)
    plt.yticks(index,  np.array(list(num_correct_pred.keys()))[sort_index], fontsize=6, rotation=0)
    plt.xticks(fontsize=12, rotation=0)

    fig.set_tight_layout(True)

    # Generate visualisation and summary files

    out_dir = os.path.join(args.output_dir, 'grouped_accuracy_comparison', args.dataset, '{:s}_{:s}_{:s}'.format(args.model, args.experiment_name, args.mode), '{:s}_{:s}_{:s}'.format(args.model2, args.experiment_name2, args.mode2), 'sort_%s' % sort_method)
    os.makedirs(out_dir, exist_ok = True)

    plt.savefig('%s/diff.pdf' % (out_dir))
    plt.savefig('%s/diff.png' % (out_dir))

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.barh(index, np.fromiter(diff_accuracy.values(), dtype=np.float)[sort_index], height=1, color='b')
    #plt.ylabel('Class Group', fontsize=12)
    plt.xlabel('Class accuracy difference', fontsize=12)
    plt.yticks(index, np.array(list(num_correct_pred.keys()))[sort_index], fontsize=6, rotation=0)
    plt.xticks(fontsize=12, rotation=0)

    fig.set_tight_layout(True)

    # Generate visualisation and summary files

    plt.savefig('%s/normalised.pdf' % (out_dir))
    plt.savefig('%s/normalised.png' % (out_dir))

    with open('%s/details.csv' % (out_dir), mode='w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=str(','), quotechar=str('"'), quoting=csv.QUOTE_MINIMAL)

        csvwriter.writerow(['class_key_first_word', 'accuracy1 (%)', 'accuracy2 (%)', 'accuracy_diff', 'num_correct_pred1', 'num_correct_pred2', 'num_correct_diff', 'num_samples_in_val'])

        for key in num_correct_pred.keys():

            accuracy1 = float(num_correct_pred[key]) / num_total_val_samples[key] * 100
            accuracy2 = float(num_correct_pred2[key]) / num_total_val_samples2[key] * 100
            accuracy_diff = accuracy2-accuracy1

            csvwriter.writerow([key, accuracy1, accuracy2, accuracy_diff, num_correct_pred[key], num_correct_pred2[key], num_correct_pred2[key]-num_correct_pred[key], num_total_val_samples[key]])

def main():
    parser = argparse.ArgumentParser(
        description='Load predictions, and generating per-class accuracy and confusion matrix')

    add_exp_arguments(parser, dataset_configs.available_datasets, model_configs.available_models, root_default=config.DEFAULT_EXPERIMENT_ROOT, dataset_default='epic_verb', model_default='i3d', name_default='test')
    parser.add_argument("-o", "--output_dir", type=str, default="visualisations",  help="output directory")
    parser.add_argument("-l", "--load_epoch", type=int, default=-2, help="Load from checkpoint. Set to -1 to load from the last checkpoint, and to -2 to load best model in terms of val_acc.")
    parser.add_argument("-L", "--load_epoch2", type=int, default=-2, help="Load from checkpoint. Set to -1 to load from the last checkpoint, and to -2 to load best model in terms of val_acc.")
    parser.add_argument("-m", "--mode", type=str, default="oneclip", choices=["oneclip", "multicrop"],  help="Mode used for run_val.py")
    parser.add_argument("-m2", "--mode2", type=str, default="oneclip", choices=["oneclip", "multicrop"],  help="Mode used for run_val.py")

    parser.add_argument("-M2", "--model2", type=str, default="i3d", choices=model_configs.available_models,  help="Mode used for run_val.py")
    parser.add_argument("-E2", "--experiment_name2", type=str, default="test",  help="")
    parser.add_argument("-S2", "--subfolder_name2", type=str, default="test",  help="")

    parser.add_argument(
        '--sort_method', type=str, default='all', choices=['train_class_frequency', 'val_class_frequency', 'val_per_class_accuracy', 'all'], help='Sorting method')

    args = parser.parse_args()

    generate_confusion_matrix(args, args.sort_method, 0)

if __name__ == '__main__':
    main()


import numpy as np
from Get_global_value import d_time
from f_dyn import f_dyn
from skew_sym import skew_sym
from aw import aw

def f_dyn_rk2(R0, A0, v0, w0, q, qd, F0, T0, Fe, Te, tau):

    C0 = A0.T

    # 1st step
    tmp_vd0, tmp_wd0, tmp_qdd = f_dyn(R0, A0, v0, w0, q, qd, F0, T0, Fe, Te, tau)

    k1_R0 = d_time * v0
    k1_C0 = d_time * skew_sym(w0).T @ C0
    k1_A0 = k1_C0.T
    k1_q = d_time * qd

    k1_v0 = d_time * tmp_vd0
    k1_w0 = d_time * tmp_wd0
    k1_qd = d_time * tmp_qdd

    # 2nd step
    tmp_vd0, tmp_wd0, tmp_qdd = f_dyn(R0 + k1_R0/2, A0 + k1_A0/2, v0 + k1_v0/2, w0 + k1_w0/2, q + k1_q/2, qd + k1_qd/2, F0, T0, Fe, Te, tau)

    k2_R0 = d_time * (v0 + k1_v0 / 2)
    k2_C0 = d_time * (skew_sym(w0 + k1_w0 / 2).T @ C0)
    k2_A0 = k2_C0.T
    k2_q = d_time * (qd + k1_qd / 2)

    k2_v0 = d_time * tmp_vd0
    k2_w0 = d_time * tmp_wd0
    k2_qd = d_time * tmp_qdd

    # 3rd step
    tmp_vd0, tmp_wd0, tmp_qdd = f_dyn(R0 + k2_R0/2, A0 + k2_A0/2, v0 + k2_v0/2, w0 + k2_w0/2, q + k2_q/2, qd + k2_qd/2, F0, T0, Fe, Te, tau)

    k3_R0 = d_time * (v0 + k2_v0 / 2)
    k3_C0 = d_time * (skew_sym(w0 + k2_w0 / 2).T @ C0)
    k3_A0 = k3_C0.T
    k3_q = d_time * (qd + k2_qd / 2)

    k3_v0 = d_time * tmp_vd0
    k3_w0 = d_time * tmp_wd0
    k3_qd = d_time * tmp_qdd

    # 4th step
    tmp_vd0, tmp_wd0, tmp_qdd = f_dyn(R0 + k3_R0, A0 + k3_A0, v0 + k3_v0, w0 + k3_w0, q + k3_q, qd + k3_qd, F0, T0, Fe, Te, tau)
    k4_R0 = d_time * (v0 + k3_v0)
    k4_C0 = d_time * (skew_sym(w0 + k3_w0).T @ C0)
    k4_A0 = k4_C0.T
    k4_q = d_time * (qd + k3_qd)

    k4_v0 = d_time * tmp_vd0
    k4_w0 = d_time * tmp_wd0
    k4_qd = d_time * tmp_qdd

    R0_next = R0 + (k1_R0 + 2 * k2_R0 + 2 * k3_R0 + k4_R0) / 6
    C0_next = C0 + (k1_C0 + 2 * k2_C0 + 2 * k3_C0 + k4_C0) / 6
    A0_next = C0_next.T
    q_next = q + (k1_q + 2 * k2_q + 2 * k3_q + k4_q) / 6

    v0_next = v0 + (k1_v0 + 2 * k2_v0 + 2 * k3_v0 + k4_v0) / 6
    w0_next = w0 + (k1_w0 + 2 * k2_w0 + 2 * k3_w0 + k4_w0) / 6
    qd_next = qd + (k1_qd + 2 * k2_qd + 2 * k3_qd + k4_qd) / 6

    R0 = R0_next
    A0 = A0_next
    q = q_next
    v0 = v0_next
    w0 = w0_next
    qd = qd_next

    return R0, A0, v0, w0, q, qd


from args import parse_config
from main import get_trainer
from utils import *
from sklearn.model_selection import KFold
import os

def cross_validation(**args):
    df_all = pd.read_csv(args.get('train_csv_path'))
    # Extract user, movie, rating vectors from the dataframe.
    users, movies, ratings = extract_user_movie_rating_arrays(df_all)
    cv_folder = args.get('cv_folder')
    base_model_name = args.get('model_name')
    if not os.path.exists(cv_folder):
        os.makedirs(cv_folder)

    kf = KFold(n_splits=args.get('k_fold'), shuffle=True, random_state=args.get('random_seed'))

    args['pred_folder'] = cv_folder
    args['ckpt_folder'] = cv_folder
    for idx, (train_index, test_index) in enumerate(kf.split(df_all)):
        users_train = users[train_index]
        movies_train = movies[train_index]
        ratings_train = ratings[train_index]

        users_test = users[test_index]
        movies_test = movies[test_index]
        ratings_test = ratings[test_index]

        print('Fold {}: Number of training sumples: {}, number of test samples: {}.'.format(idx+1, len(users_train), len(users_test)))

        args['model_name'] = base_model_name + '_fold_' + str(idx+1)
        trainer = get_trainer(**args)
        trainer.train(users_train, movies_train, ratings_train, users_test, movies_test, ratings_test, **args)

if __name__ == '__main__':
    args = parse_config()
    cross_validation(**args)


class BankAccount:
    bankName = 'Dojo National Bank'

    def __init__(self,int_rate = 0,balance=0):
        self.int_rate = int_rate
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self
    def withdrawal(self, amount):
        if amount >= self.balance:
            self.balance -= amount
        else:
            print("Insufficient Funds")
            self.balance -= 5
        return self
    def display_account_info(self):
        print(f"Balance: ${self.balance}\n{self.int_rate}% interest rate")
        return self
    def yield_interest(self):
        if self.balance > 0:
            self.balance += (self.int_rate/100) * self.balance
        else:
            print("This account currently does not have a positive balance. No interest added for this statement period.")
        return self
    @classmethod
    def printAllAccountInfo(cls):
        print(cls.bankName)
        for account in cls.allBankAccs:
            cls.display_account_info(account)

class User:
    def __init__(self,name,email, accounts):
        self.name = name
        self.email = email
        self.accounts = {
            'checking': BankAccount(int_rate=0, balance=0, accountType=''),
            'savings': BankAccount(int_rate=2, balance=0, accountType='')
        }

    def make_deposit(self, accountType, amount):
        self.accounts[accountType].deposit(amount)
        return self

    def make_withdrawal(self, accountType, amount):
        self.accounts[accountType].withdrawal(amount)
        return self

    def display_user_balance(self):
        self.account.display_account_info()

bob = BankAccount("Bob Woodward",1,"checking")
bob = BankAccount("Bob Woodward",2,"savings")
tom = BankAccount("Tom Willard",2,"savings")
tom = BankAccount("Tom Willard",1,"checking")

bob.deposit('savings',300).deposit('checking',300).deposit('checking',300).withdrawal('savings',400).yield_interest().display_account_info()
tom.deposit('checking',400).deposit('savings',400).withdrawal('checking',200).withdrawal('checking',200).withdrawal('checking',200).withdrawal('savings',300).yield_interest().display_account_info()

BankAccount.printAllAccountInfo()