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

import argparse
import os
import re
from collections import defaultdict
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser(description='Script that generates epidemic plots and csv reports.')

parser.add_argument('-l', '--log_file', type=str, required=True, help='Input log file')
parser.add_argument('-d', '--dim', type=str, default="200,200", help='Dimensions of the cell grid (WIDTH,HEIGHT)')
parser.add_argument('-i', '--initial_val', default=0, help='Force initial val for the cells')
parser.add_argument('-c', '--state_changes', action="store_true", help='Log state changes to csv file')
parser.add_argument('-f', '--img_format', default="pdf", help='Image format used to export the images')

args = parser.parse_args()

log_filename = args.log_file
dim = list(map(int, args.dim.split(",")))
initial_val = args.initial_val
#log_filename = "covid_store.log"
#dim = 64, 100
#initial_val = None
log_state_changes_to_file = args.state_changes

patt_out_line = ".*Y / (?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3})(?::0)? / [a-zA-Z0-9]+\((?P<x>[0-9]+),(?P<y>[0-9]+)\)\([0-9]+\) / out / +(?P<state>[0-9.-]+) (?:/|para) [a-zA-Z0-9]+\([0-9]+\)"

ID_SUSCEPTIBLE = 0
ID_INFECTED = 1
ID_SICK = 8
ID_RECOVERED = 16
ID_DEAD = -1
ID_WALL = -10

COLOR_SUSCEPTIBLE = "#3498db"
COLOR_INFECTED = "#e74c3c"
COLOR_SICK = "#f1c40f"
COLOR_RECOVERED = "#2ecc71"
COLOR_DEAD = "#9b59b6"

def time_str_to_ts(time_str):
  patt_time = "([0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{3})"
  match = re.match(patt_time, time_str)
  if not match:
    raise RuntimeError("Error converting simulation time")
  tu = list(map(int, match.groups()))
  return tu[3] + tu[2]*1000 + tu[1]*60000 + tu[0]*3600000

def dict_to_states_row(states_dict):
  row = []
  row.append(states_dict[ID_SUSCEPTIBLE] if ID_SUSCEPTIBLE in states_dict else 0)

  infected_count = 0
  for state in range(ID_INFECTED, ID_SICK):
    if state in states_dict:
      infected_count += states_dict[state]
  row.append(infected_count)

  sick_count = 0
  for state in range(ID_SICK, ID_RECOVERED):
    if state in states_dict:
      sick_count += states_dict[state]
  row.append(sick_count)

  for state in [ID_RECOVERED, ID_DEAD, ID_WALL]:
    row.append(states_dict[state] if state in states_dict else 0)
  return row

state_count = defaultdict(int)
df_rows = []
curr_states = [[initial_val] * dim[1] for _ in range(dim[0])]
if initial_val is not None:
    state_count[initial_val] = dim[0]*dim[1]

if log_state_changes_to_file:
  csv_file = open("state_changing.csv", "w")
  csv_file.write(",".join(("time", "x", "y", "previous_state", "current_state")) + "\n")
curr_time = None

with open(log_filename, "r") as log_file:
  for line in log_file:
    line = line.strip()
    match = re.match(patt_out_line, line)
    if not match:
      if line.startswith("Mensaje Y"):
        print(line)
      continue
    if curr_time is None:
      curr_time = match.group("time")
    elif curr_time != match.group("time"):
      #print("Changed to " + match.group("time"))

      row = [time_str_to_ts(curr_time)] + dict_to_states_row(dict(state_count.items()))
      df_rows.append(row)

      curr_time = match.group("time")

    x = int(match.group("x"))
    y = int(match.group("y"))

    if not curr_states[x][y] is None:
      state_count[curr_states[x][y]] -= 1

    if log_state_changes_to_file:
      csv_file.write((",".join((match.group("time"), match.group("x"), match.group("y"), str(curr_states[x][y]), str(int(float(match.group("state"))))))))
      csv_file.write("\n")
    #print("Time: %s, cell (%s, %s) changing from %d to %s" % (match.group("time"), match.group("x"), match.group("y"), curr_states[x][y], match.group("state")))
    curr_states[x][y] = int(float(match.group("state")))
    state_count[int(float(match.group("state")))] += 1

if log_state_changes_to_file:
  csv_file.close()

# ## Dataframe creation and visualization
columns = ["time", "susceptible", "infected", "sick", "recovered", "dead", "walls"]
df = pd.DataFrame(df_rows, columns=columns)
df = df.set_index("time")

total_cells = sum(df.iloc[0,:])
population = total_cells - df.iloc[0,:]["walls"]
print("Total cells: %d, population: %d" % (total_cells, population))

df_vis = df.copy()
df_vis = df_vis.drop(["walls"], axis=1)
df_vis = df_vis.divide(population)
df_vis.index = df_vis.index.map(lambda x: x/1000)

base_name = os.path.splitext(os.path.basename(log_filename))[0]

col_names = ["infected", "sick", "recovered", "dead"]
colors=[COLOR_INFECTED, COLOR_SICK, COLOR_RECOVERED, COLOR_DEAD]

x = list(df_vis.index)
y = np.vstack([df_vis[col] for col in col_names])

fig, ax = plt.subplots(figsize=(12,7))
ax.stackplot(x, y, labels=col_names, colors=colors)
plt.legend(loc='upper right')
plt.margins(0,0)
plt.title('Epidemic percentages (%s)' % base_name)
#plt.show()
plt.xlabel("Time (s)")
plt.ylabel("Population (%)")
plt.savefig(base_name + "_area" + args.img_format)

fig, ax = plt.subplots(figsize=(12,7))
linewidth = 2

x = list(df_vis.index)
ax.plot(x, df_vis["susceptible"], label="susceptible", color=COLOR_SUSCEPTIBLE, linewidth=linewidth)
ax.plot(x, df_vis["infected"], label="infected", color=COLOR_INFECTED, linewidth=linewidth)
ax.plot(x, df_vis["sick"], label="sick", color=COLOR_SICK, linewidth=linewidth)
ax.plot(x, df_vis["recovered"], label="recovered", color=COLOR_RECOVERED, linewidth=linewidth)
ax.plot(x, df_vis["dead"], label="dead", color=COLOR_DEAD, linewidth=linewidth)
plt.legend(loc='upper right')
plt.margins(0,0)
plt.title('Epidemic percentages (%s)' % base_name)
plt.xlabel("Time (s)")
plt.ylabel("Population (%)")
plt.savefig(base_name + "_lines" + args.img_format)

df_nums = df.drop("walls", axis=1)
df_nums.to_csv(base_name + ".csv")

fig, ax = plt.subplots(figsize=(12,7))
linewidth = 2

x = list(df_vis.index)
ax.plot(x, df_vis["infected"], label="infected", color=COLOR_INFECTED, linewidth=linewidth)
ax.plot(x, df_vis["sick"], label="sick", color=COLOR_SICK, linewidth=linewidth)
ax.plot(x, df_vis["recovered"], label="recovered", color=COLOR_RECOVERED, linewidth=linewidth)
ax.plot(x, df_vis["dead"], label="dead", color=COLOR_DEAD, linewidth=linewidth)
plt.legend(loc='upper right')
plt.margins(0,0)
plt.title('Epidemic percentages (%s)' % base_name)
plt.xlabel("Time (s)")
plt.ylabel("Population (%)")
plt.savefig(base_name + "_lines_nosus" + args.img_format)


from src.infra.webpageprovider import WebPageProvider
from src.infra.localfilewriter import LocalFileWriter
from src.core.challengefantasytransformer import ChallengeFantasyTransformer
from src.core.leaguetransformer import LeagueTransformer
import config as cfg

def extract_show_scores(start_week, end_week):
    l = LocalFileWriter("scores.csv")
    l.open()
    l.write("week,name,identifier,occurrences,total_points\n")

    for week in range(start_week, end_week + 1):
        w = WebPageProvider(cfg.base_url + str(week))
        content = w.get_page()

        if content:
            t = ChallengeFantasyTransformer(content)

            for score in t.parse():
                l.write(f"{week},{score}\n")

    l.close()

def extract_league_scores(start_week, end_week):
    #11459 my team
    l = LocalFileWriter("league_scores.csv")
    l.open()
    l.write("team,week,player\n")

    for team in cfg.team_ids:
        w = WebPageProvider(cfg.team_url + str(team))
        content = w.get_page()
        if content:
            t = LeagueTransformer(content)

        for score in t.parse(start_week, end_week):
            l.write(f"{team},{score}\n")

    l.close()

if __name__ == "__main__":
    extract_show_scores(1, 6)
    #extract_league_scores(1, 6)


import os
import shutil
import glob
import random
import numpy as np
import pandas as pd

path_attr_celeba = 'list_attr_celeba.txt'
feature = 'Blond_Hair'
celeba_subset_src = "../dataset/celeba/align_celeba/img_align_celeba"
folder_pos = '../dataset/celeba_val_test_2/Blond_Hair'
# folder_neg = '../dataset/celeba/celeba_subset/gender/female'
images_list = glob.glob(celeba_subset_src + '/*.jpg')

attr_list = pd.read_csv(path_attr_celeba, delim_whitespace=True)

feature_col = attr_list[feature]

num_images = 0
for image in images_list:
    file_name = os.path.split(image)[1]
    if feature_col[file_name] == 1:
        # move image to positive folder
        if not os.path.exists(folder_pos):
            os.makedirs(folder_pos)
        shutil.copy(image, folder_pos + '/' + file_name)
        num_images += 1
    if num_images == 1000:
        break
    # else:
    #     # move image to negative folder
    #     if not os.path.exists(folder_neg):
    #         os.makedirs(folder_neg)
    #     shutil.copy(image, folder_neg + '/' + file_name)

# print(attr_list[feature]['000010.jpg'])


from simsopt.geo import CurveCWSFourier, SurfaceRZFourier
import numpy as np
import pprint

# CREATE CWS
dofs = [1, 0.1, 0.1] # RBC00, RBC10, ZBS10
quadpoints_phi = np.arange(0, 64, 1)
quadpoints_theta = quadpoints_phi.copy()
surf = SurfaceRZFourier(nfp=1, stellsym=True, mpol=1, ntor=0, quadpoints_phi=quadpoints_phi, quadpoints_theta=quadpoints_theta)
surf.set_dofs(dofs)

curve_cws = CurveCWSFourier(surf.mpol, surf.ntor, surf.x, 64, 1, surf.nfp, surf.stellsym)
curve_cws.set_dofs([1, 0, 0.1, 0, 1, 0, 0.1, 0])

'''
file1 = open("python_cws_dgamma.txt", "w")
for i in range(0, len(quadpoints_phi)):
    value = curve_cws.dgamma_by_dcoeff()[i][0]
    file1.write(f"{value}\n")
file1.close()

file1 = open("python_cws_dgammadash.txt", "w")
for i in range(0, len(quadpoints_phi)):
    value = curve_cws.dgammadash_by_dcoeff()[i]
    file1.write(f"{value}\n")
file1.close()

file1 = open("python_cws_dgammadashdash.txt", "w")
for i in range(0, len(quadpoints_phi)):
    value = curve_cws.dgammadashdash_by_dcoeff()[i]
    file1.write(f"{value}\n")
file1.close()
'''

print(curve_cws.dgammadashdash_by_dcoeff()[0])


from aiogram.types import ReplyKeyboardMarkup,KeyboardButton,InlineKeyboardButton,InlineKeyboardMarkup

til = ReplyKeyboardMarkup(
	keyboard = [
		[
		KeyboardButton(text="🇺🇿 O'zbekcha"),
		]
	],
	resize_keyboard=True
)

shaharlar = ReplyKeyboardMarkup(
	keyboard = [
		[
		KeyboardButton(text="Toshkent"),
		KeyboardButton(text="Yunsobod"),
		],
		[
		KeyboardButton(text="Chilonzor"),
		KeyboardButton(text="Uch.tepa"),
		],
		[
		KeyboardButton(text="Shoxsaroy"),
		KeyboardButton(text="Olmozor "),
		]
	],
	resize_keyboard=True
)

contact1 = ReplyKeyboardMarkup(
	keyboard = [
		[
		KeyboardButton(text="Mening nomerim",request_contact=True)
		],
	],
		resize_keyboard=True
)

lakatsiya1 = ReplyKeyboardMarkup(
	keyboard = [
		[
		KeyboardButton(text="lokatsiya",request_location=True)
		],
	],
		resize_keyboard=True
)

buyurtma1 = InlineKeyboardMarkup(
	inline_keyboard = [
		[
		InlineKeyboardButton(text="Buyurtma",callback_data='buyurtma')
		],
	],
)

menubosilgandagi = InlineKeyboardMarkup(
  inline_keyboard=[
      [
          InlineKeyboardButton("📖 Barcha menyular",url="https://telegra.ph/TASHKENT-EVOS-MENU-02-03"),
          InlineKeyboardButton("🍟🌯🥤 Set", callback_data='set')
      ],
      [
          InlineKeyboardButton("🌯 Lavash", callback_data='lavash'),
          InlineKeyboardButton("Boshqa", callback_data='boshqa')

      ],
      [
        InlineKeyboardButton("🥙 Shaurma", callback_data='shaurma'),
          InlineKeyboardButton("🍲 Donar", callback_data='donar'),

      ],
      [
        InlineKeyboardButton("🍔 Burger", callback_data='burger'),
          InlineKeyboardButton("🌭 Xot-dog", callback_data='xot-dog'),

      ],
      [
        InlineKeyboardButton("🍰 Desertlar", callback_data='desertlar'),
          InlineKeyboardButton("☕ Ichimliklar", callback_data='ichimliklar'),
      ],
      [
         InlineKeyboardButton("🍟 Gazaklar", callback_data='gazaklar')
      ]

  ]
)

menular = ReplyKeyboardMarkup(
  keyboard = [
    [
    KeyboardButton(text="📍 Manzilni o'zgartirish"),
    KeyboardButton(text="🍴 Menu"),
    ],
    [
    KeyboardButton(text="🛍 Buyurtmalarim"),
    KeyboardButton(text="👪 EVOS Oilasi"),
    ],
    [
    KeyboardButton(text="✍ Fikr bildirish"),
    KeyboardButton(text="⚙ Sozlamalar"),
    ],
  ],
  resize_keyboard=True
)


import json
from pymongo import MongoClient
from pymongo.collection import Collection
db_name = 'test'
collection_name = 'testdata'
client = MongoClient('mongodb://db-host.local:27017')

db = client[db_name]

try:
    collection = Collection(db, collection_name, create=True)
except Exception as e:
    print(e)

collection = db[collection_name]

for suffix in range(20000,5000000,10000):
    try:
        with open('testfiles/testdata_'+str(suffix),'r') as  fh:
            data = json.load(fh)
            { item.update({'_id':item['vuln_hash']}) for item in data}
    except:
        continue
    it = 0
    batch = 10000
    while it < len(data):
        res = collection.insert_many(data[it:it+batch])
        it += batch
        print('{}/{} - {}'.format(it, len(data), suffix))

    # for i,item in enumerate(data):
    #     if i%1000 == 0:
    #         print(suffix, i)
    #     try:
    #         # collection_data_obj.insert(json.dumps(item))

#     #     except Exception as e:
#     #         pass
print('done')


# here plot methods and outliers methods

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

# change shape of columns

def choose_col(df,name):
    df_XX =  df[name].dropna()
    X = np.array(df_XX)
    X = X.reshape(len(X),1)

    X_f = X #X_f = X[1:int(len(X)/5)]
    return X_f

def quartil(X):
    data = pd.DataFrame(X)
    q1 = data.quantile(.25)               # Calculate quantiles
    q2 = data.quantile(.5)
    q3 = data.quantile(.75)
    dif = q3 - q1
    k = 1.5

    Total = len(data)

    min, max = q1 - k*dif, q3 + k*dif     # Caculate interval
    min = min[0]
    max = max[0]
    print("Quartilte method, k = ", k)
    print("Min: ", min)
    print("Max: ",max)
    num_out = 0
    num_in = 0
    filtered = []

    for i in X:

        if (i[0].item() > max or  i[0].item() < min ):
            num_out = num_out + 1

        else:
            filtered.append(i[0])
            num_in = num_in + 1

    total = num_in + num_out
    Fraction = num_out/len(X)
    R_frac = num_out/Total

    print("fraction of outliers in a a total of " , total, " is: ", Fraction)
    print("fraction of outliers in a a total of " , Total, " is: ", R_frac)

    return filtered, num_out, Fraction, R_frac

def plot_distr(df,name_col, method):
    X_f = choose_col(df, name_col)
    min_ = min(X_f)[0]
    max_ = max(X_f)[0]
    r, _ , _, _= method(X_f)

    plt.figure(figsize=(12,22))

    #Plot 1
    plt.subplot(4,2,1)
    #Axes.set_ylim(min_, max_)
    plt.plot(X_f)
    plt.ylim(min_, max_)
    plt.title('Line plot original data')
    # plt.xlabel('Week')
    plt.tick_params(labelbottom = False, bottom = False)

    #Plot 2
    plt.subplot(4,2,2)
    plt.hist(X_f, bins = 100)
    plt.title('Distribution of original data')
    #plt.tick_params(left = False, right = False , labelleft = False ,
    #                labelbottom = False, bottom = False)

    # LOF Detection for outliers
    plt.subplot(4,2,3)

    plt.plot(r, color = 'orange')
    plt.ylim(min_, max_)
    plt.title("Line plot with  outliers filtered")
    #plt.tick_params(left = False, right = False , labelleft = False ,
    #                 labelbottom = False, bottom = False)

    plt.subplot(4,2,4)
    #plt.hist(r, bins= 100, color = 'orange', range =[min_, max_])
    plt.hist(r, bins= 100, color = 'orange')
    plt.title("Distribution with outliers filtered")
    #plt.tick_params(left = False, right = False , labelleft = False ,
    #                 labelbottom = False, bottom = False)
    plt.show()


import discord
from discord.ext import commands
from cogs.utils import checks
from datetime import datetime, timedelta
import re
from langdetect import detect

class langdetect:
    """Shows how much french vs english is shown in a channel"""

    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True)
    @checks.is_owner()
    async def langdetect(self, ctx, channel, threshold):
        """Simply give the channel id and how many days old the messages can be"""

        if not threshold.isdigit(): # Checks wether the threshold given is a number
            await self.bot.say("Threshold should be numbers. Aborting.")
            return

        msg = await self.bot.say("Processing please wait...")
        await self.get_messages(ctx.message.server, channel, int(threshold))
        await self.bot.delete_message(msg)

    async def get_messages(self, server, channel, threshold): # Gets messages and detects the language
        after_n_days_ago = datetime.utcnow() - timedelta(days=threshold)
        channel_id = self.bot.get_channel(str(channel))
        frcounter = 0
        encounter = 0
        async for mesg in self.bot.logs_from(channel_id, after=after_n_days_ago, limit=100*100, reverse=True):
            mesg = mesg.clean_content
            cleantext = self.letters(mesg)
            if cleantext == "" or cleantext == None or type(cleantext) != str or cleantext.isspace() == True:
                pass
            else:
                if detect(cleantext) == 'fr':
                    frcounter = frcounter + 1
                if detect(cleantext) == 'en':
                    encounter = encounter + 1
        await self.bot.say("French : {} \nEnglish : {}".format(frcounter, encounter))

    def letters(self, mesg): # Cleans messages to remove non_alphanumeric characters
        valids = []
        for character in mesg:
            if character.isspace():
                valids.append(character)
            if character.isalpha():
                valids.append(character)
        cleantext = ''.join(valids)
        return cleantext

def setup(bot):
    bot.add_cog(langdetect(bot))


from django.conf.urls import url
from . import views

# URL's que pueden ser visitadas, cada una produce un efecto distinto
# para revisar que hace cada una, revisar funcion llamada, ej: views.crear_persona (funcion 'crear_persona' en archivo 'views')

urlpatterns = [
    url(r'^$', views.index),
    url(r'^personas/crear$',views.crear_persona),
    url(r'^autos/crear$',views.crear_auto),
    url(r'^carnet/crear$',views.crear_carnet),

    url(r'^personas/todas$',views.personas_todas),
    url(r'^autos/todos$',views.autos_todos),
    url(r'^carnet/todos$',views.carnet_todos),

    url(r'^personas/eliminar/(?P<pk>[0-9]+)$',views.eliminar_persona),
    url(r'^autos/persona/(?P<pk>[0-9]+)$',views.autos_persona),

]


# coding=utf8

import os
import re
import inspect
import functools

import fabric
import fabric.api
import fabric.colors

FUZZY_VARS_FILE = 'fabric.ini'
icolor = fabric.colors.cyan  # color for interactive

class Vars(dict):

    def __getitem__(self, key):
        if key not in self:
            if os.path.isfile(FUZZY_VARS_FILE):
                from ConfigParser import SafeConfigParser
                config_parser = SafeConfigParser()
                config_parser.read(FUZZY_VARS_FILE)

                self.update(config_parser.items('main'))

            if key not in self:
                value = ensure_prompt(humanize(key))
                self[key] = value
                if os.path.isfile(FUZZY_VARS_FILE):
                    config_parser.set('main', key, value)
                    config_parser.write(open(FUZZY_VARS_FILE, 'w'))

        return dict.__getitem__(self, key)

def humanize(var_name):
    return var_name.replace('_', ' ').capitalize()

vars = Vars()

# --- Format ---

def format_decorator(func):

    inspect_func = func
    if isinstance(func, functools.partial):
        inspect_func = func.func

    func_kwargs_names = {}
    argspec = inspect.getargspec(inspect_func)

    if argspec.defaults:
        func_kwargs_names = argspec.args[-len(argspec.defaults):]

    def inner(template, *args, **kwargs):
        func_kwargs = {}
        for func_kwarg_key in func_kwargs_names:
            if func_kwarg_key in kwargs:
                func_kwargs[func_kwarg_key] = kwargs.pop(func_kwarg_key)

        output = format_template(template, *args, **kwargs)
        return func(output, **func_kwargs)

    return inner

def format_template(template, *args, **format_kwargs):
    format_kwargs = get_missed_vars(template, format_kwargs)
    output = template.format(*args, **format_kwargs)
    return output

def get_missed_vars(template, format_kwargs):
    # take format token from template
    # e.g., 'package_name' from 'Init {package_name}?'
    template_var_names = re.findall('(?!<{){(\w+)}(?!})', template)

    for var_name in template_var_names:
        if var_name not in format_kwargs:
            format_kwargs[var_name] = vars[var_name]

    return format_kwargs

# --- Input/Output ---

@format_decorator
def prompt(message):
    return fabric.api.prompt(icolor('  ' + message))  # spaces inside color to avoid strip

@format_decorator
def ensure_prompt(message):
    if not message.endswith(':'):
        message += ':'

    entered = ''
    while not entered:
        entered = fabric.api.prompt(icolor('  ' + message))  # spaces inside color to avoid strip
    return entered

@format_decorator
def confirm(message='Are you sure?'):
    return fabric.api.prompt(icolor('  ' + message)) == 'yes'

def suited_options(options, entered):
    result = []
    for option in options:
        handy_for_input_option = option.lstrip(' .-').lower()
        if handy_for_input_option.startswith(entered):
            result.append(option)
    return result

def print_color(message, color=fabric.colors.blue):
    print('  ' + color(message))

@format_decorator
def choose(message, options=[]):
    # print options
    options.sort(key=str.lower)
    print_color('\n  '.join(options), icolor)
    print_color('')
    while True:
        entered = fabric.api.prompt(icolor('  {}:'.format(message)))
        if len(entered) >= 2:
            suited = suited_options(options, entered)
            if len(suited) == 1:
                return suited[0]

@format_decorator
def info(message):
    print_color(message, fabric.colors.blue)

@format_decorator
def success(message):
    print_color(message, fabric.colors.green)

@format_decorator
def warning(message):
    print_color(message, fabric.colors.yellow)

@format_decorator
def error(message):
    print_color(message, fabric.colors.red)

def func_repr(func):
    if isinstance(func, functools.partial):
        func = func.func

    if func.__doc__:
        return func.__doc__.strip()
    else:
        return func.__name__

def call_chosen(funcs, message='Execute'):
    func_dict = {func_repr(func): func for func in funcs}
    chosen = choose(message, options=func_dict.keys())
    return func_dict[chosen]()

# -- Deprecated --
# todo ???
def task(task_function):
    task_function.__name__ = task_function.__name__.replace('_', '-')
    return fabric.api.task(task_function)


import random
import collections
class RandomizedCollection:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.nums = []
        self.indexTable = collections.defaultdict(list)

    def insert(self, val: int) -> bool:
        """
        Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
        """
        result = val not in self.indexTable
        self.indexTable[val].append(len(self.nums))
        self.nums.append([val, len(self.indexTable[val]) - 1])
        return result

    def remove(self, val: int) -> bool:
        """
        Removes a value from the collection. Returns true if the collection contained the specified element.
        """
        if val not in self.indexTable:
            return False
        index = self.indexTable[val][-1]
        last = self.nums[-1]
        self.indexTable[last[0]][last[1]] = index
        self.nums[index] = last
        self.nums.pop()
        self.indexTable[val].pop()
        if not self.indexTable[val]:
            del self.indexTable[val]
        return True

    def getRandom(self) -> int:
        """
        Get a random element from the collection.
        """
        return random.choice(self.nums)[0]

# Your RandomizedCollection object will be instantiated and called as such:
# obj = RandomizedCollection()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

0301-0400/0381-Insert Delete GetRandom O(1) - Duplicates allowed/0381-Insert Delete GetRandom O(1) - Duplicates allowed.py


from sqlhandler import sqlhandler
from datamanager import datamanager
import pandas as pd

dm = datamanager()
localserver = sqlhandler('mysqllocal')
localserver.fetchsize = 10
localserver.query = ("SELECT * FROM officelean")

def refactor_func(dm, localserver):
    try:
        results, cnames = localserver.fetchmany()
        df = pd.DataFrame(results)
        df.columns = cnames
        #temp = dm.get_errorlist(results, 21, 23, 17, 18, 19, 20, 22)
        #temp = dm.get_errorlist(rsults, 6, 23)
        #temp = dm.get_stoplist(results, 5, 23)
        #for tmp in temp:
            #print(tmp)
        print(df)
        df.to_csv('C:\\Users\\Pc\\Desktop\\officelean\\officelean.csv', encoding='utf-8')
    except Exception as e:
        print(e)

refactor_func(dm, localserver)


import itertools

DIMACS_INSTANCE_DIR = 'data/Color02'

GRAPHS = ['myciel3.col', 'myciel4.col', 'queen5_5.col', '1-FullIns_3.col', 'queen6_6.col', '2-Insertions_3.col',
          'myciel5.col', 'queen7_7.col', '2-FullIns_3.col', '3-Insertions_3.col', 'queen8_8.col', '1-Insertions_4.col',
          'huck.col', '4-Insertions_3.col', 'jean.col', '3-FullIns_3.col', 'queen9_9.col', 'david.col', 'mug88_25.col',
          'mug88_1.col', '1-FullIns_4.col', 'myciel6.col', 'queen8_12.col', 'mug100_1.col', 'queen10_10.col',
          'mug100_25.col', '4-FullIns_3.col', 'games120.col', 'queen11_11.col', 'DSJC125.9.col', 'DSJC125.1.col',
          'DSJC125.5.col', 'miles750.col', 'miles250.col', 'miles500.col', 'miles1500.col', 'miles1000.col', 'anna.col',
          'queen12_12.col', '2-Insertions_4.col', '5-FullIns_3.col', 'queen13_13.col', 'mulsol.i.3.col',
          'mulsol.i.4.col', 'mulsol.i.5.col', 'mulsol.i.2.col', 'myciel7.col', 'queen14_14.col', 'mulsol.i.1.col',
          '1-Insertions_5.col', 'zeroin.i.3.col', 'zeroin.i.2.col', 'zeroin.i.1.col', '2-FullIns_4.col',
          'queen15_15.col', 'DSJC250.1.col', 'DSJC250.5.col', 'DSJC250.9.col', 'queen16_16.col', '3-Insertions_4.col',
          '1-FullIns_5.col', 'school1_nsh.col', 'school1.col', '3-FullIns_4.col', 'fpsol2.i.3.col', 'le450_5c.col',
          'le450_15d.col', 'le450_15c.col', 'le450_15a.col', 'le450_25b.col', 'le450_25d.col', 'le450_5a.col',
          'le450_25a.col', 'le450_25c.col', 'le450_5d.col', 'le450_5b.col', 'le450_15b.col', 'fpsol2.i.2.col',
          '4-Insertions_4.col', 'fpsol2.i.1.col', 'DSJC500.5.col', 'DSJR500.1.col', 'DSJC500.1.col', 'DSJC500.9.col',
          'DSJR500.1c.col', 'DSJR500.5.col', 'homer.col', '2-Insertions_5.col', '1-Insertions_6.col', 'inithx.i.3.col',
          'inithx.i.2.col', 'ash331GPIA.col', '4-FullIns_4.col', 'will199GPIA.col', '2-FullIns_5.col', 'inithx.i.1.col',
          'latin_square_10.col', 'qg.order30.col', 'wap05a.col', 'wap06a.col', 'DSJC1000.5.col', 'DSJC1000.1.col',
          'DSJC1000.9.col', '5-FullIns_4.col', 'ash608GPIA.col', '3-Insertions_5.col', 'abb313GPIA.col',
          'qg.order40.col', 'wap07a.col', 'wap08a.col', 'ash958GPIA.col', '3-FullIns_5.col', 'wap01a.col', 'wap02a.col',
          'qg.order60.col', '4-FullIns_5.col', 'wap03a.col', 'wap04a.col', 'qg.order100.col']

MODELS = [
    (lambda model: model.add_color_orbitope_constraint(), 'Orbitope'),
    (lambda model: None, 'Default'),
    (lambda model: model.scip.setIntParam("misc/usesymmetry", 0), 'No-Sym'),
    (lambda model: model.add_color_orbitope_constraint(components_activation_handler=True, all_color_pairs=False), 'Act-Consec'),
    (lambda model: model.add_color_orbitope_constraint(components_activation_handler=True, all_color_pairs=True), 'Act-AllPairs'),
]

SELECTED_INSTANCES = [
    t
    for t in itertools.product(
        GRAPHS,
        [5, 6, 8, 10],
        MODELS,
    )
]


from pathlib import Path

import numpy as np
import darr

import birdwatcher as bw
from birdwatcher.utils import derive_filepath

__all__ = ['batch_detect_movement', 'detect_movement', 'apply_settings',
           'create_movementvideo']

default_settings = {'processing':{'color': False, # booleans only
                                  'blur': 0, # use '0' for no blur
                                  'morphologyex': True, # booleans only
                                  'resizebyfactor': 1}} # use '1' for no change in size

def _f(rar):
    rar.archive(overwrite=True)
    darr.delete_raggedarray(rar)

def batch_detect_movement(vfs_list, settings=None, startat=None, nframes=None,
                          roi=None, nroi=None,
                          bgs_type=bw.BackgroundSubtractorMOG2,
                          analysispath='.', ignore_firstnframes=10,
                          overwrite=False, resultvideo=False, nprocesses=6):
    """The reason for having a special batch function, instead of just
    applying functions in a loop, is that compression of coordinate results
    takes a long time and is single-threaded. We therefore do this in
    parallel. Use the `nprocesses` parameter to specify the number of cores
    devoted to this.

    """
    from multiprocessing.pool import ThreadPool

    tobearchived = []
    for i, vfs in enumerate(vfs_list):
        cd, cc, cm = detect_movement(vfs, settings=settings, startat=startat,
                                     nframes=nframes, roi=roi, nroi=nroi,
                                     bgs_type=bgs_type,
                                     analysispath=analysispath,
                                     ignore_firstnframes=ignore_firstnframes,
                                     overwrite=overwrite,
                                     resultvideo=resultvideo)
        tobearchived.append(cd)
        if (len(tobearchived) == nprocesses) or (i == (len(vfs_list) - 1)):
            with ThreadPool(processes=nprocesses) as pool:
                list([i for i in pool.imap_unordered(_f, tobearchived)])
            tobearchived = []

def detect_movement(vfs, settings=None, startat=None, nframes=None, roi=None,
                    nroi=None, bgs_type=bw.BackgroundSubtractorMOG2,
                    analysispath='.', ignore_firstnframes=10,
                    overwrite=False, resultvideo=False):
    """Detects movement based on a background subtraction algorithm.

    High-level function to perform movement detection with default parameters,
    but also the option to modify many parameter settings.

    Parameters
    ----------
    vfs : VideoFileStream
        A Birdwatcher VideoFileStream object.
    settings : {dict, dict}, optional
        Dictionary with two dictionaries. One 'bgs_params' with the parameter
        settings from the BackgroundSubtractor and another 'processing'
        dictionary with settings for applying color, resizebyfactor, blur and
        morphologyex manipulations. If None, the default settings of the
        BackgroundSubtractor are used on grey color frames, including
        morphological transformation to reduce noise.
    startat : str, optional
        If specified, start at this time point in the video file. You can use
        two different time unit formats: sexagesimal
        (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds.
    nframes  : int, optional
            Read a specified number of frames.
    roi : (int, int, int, int), optional
        Region of interest. Only look at this rectangular region. h1, h2, w1,
        w2.
    nroi : (int, int, int, int), optional
        Not region of interest. Exclude this rectangular region. h1, h2, w1,
        w2.
    bgs_type: BackgroundSubtractor, default=bw.BackgroundSubtractorMOG2
        This can be any of the BackgroundSubtractors in Birdwatcher, e.g.
        BackgroundSubtractorMOG2, BackgroundSubtractorKNN,
        BackgroundSubtractorLSBP.
    analysispath : Path or str, optional
        Where to write results to. The default writes to the current working
        directory.
    ignore_firstnframes : int, default=10
        Do not provide coordinates for the first n frames. These often have a
        lot of false positives.
    overwrite : bool, default=False
        Overwrite results or not.
    resultvideo : bool, default=False
        Automatically generate a video with results, yes or no.

    Returns
    -------
    tuple of arrays (coordinates, coordinate count, coordinate mean)
        These are Darr arrays that are disk-based.

    """
    if not isinstance(vfs, bw.VideoFileStream):
        raise TypeError(f"`vfs` parameter not a VideoFileStream object.")

    output_settings = {**bgs_type().get_params(),
                       **default_settings['processing']} # get flat dict
    if settings is not None:
        settings = {**settings['bgs_params'], **settings['processing']}
        output_settings.update(settings)

    movementpath = Path(analysispath) / f'movement_{vfs.filepath.stem}'
    Path(movementpath).mkdir(parents=True, exist_ok=True)

    metadata = {}
    metadata['backgroundsegmentclass'] = str(bgs_type)
    metadata['settings'] = output_settings
    metadata['startat'] = startat
    metadata['nframes'] = nframes
    metadata['roi'] = roi
    metadata['nroi'] = nroi
    metadata['birdwatcherversion'] = bw.__version__

    frames = apply_settings(vfs, output_settings, startat, nframes, roi, nroi,
                            bgs_type)

    cd = frames.save_nonzero(Path(movementpath) / 'coords.darr',
                             metadata = metadata,
                             ignore_firstnframes = ignore_firstnframes,
                             overwrite = overwrite)
    cc = darr.asarray(Path(movementpath) / 'coordscount.darr',
                      cd.get_coordcount(),
                      metadata=metadata, overwrite=True)
    cm = darr.asarray(Path(movementpath) / 'coordsmean.darr',
                      cd.get_coordmean(),
                      metadata=metadata, overwrite=True)

    if resultvideo:
        create_movementvideo(vfs, cd, startat=startat, nframes=nframes,
                             videofilepath=Path(movementpath) /
                             'movementvideo.mp4')
    return cd, cc, cm

def apply_settings(vfs, settings, startat=None, nframes=None, roi=None,
                   nroi=None, bgs_type=bw.BackgroundSubtractorMOG2):
    """Applies movement detection based on various parameter settings.

    The background subtractor should be provided as a parameter.

    Parameters
    ----------
    vfs : VideoFileStream
        A Birdwatcher VideoFileStream object.
    settings : dict
        Dictionary with parameter settings from the BackgroundSubtractor and
        settings for applying color, resizebyfactor, blur and morphologyex
        manipulations.
    startat : str, optional
        If specified, start at this time point in the video file. You can use
        two different time unit formats: sexagesimal
        (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds.
    nframes  : int, optional
            Read a specified number of frames.
    roi : (int, int, int, int), optional
        Region of interest. Only look at this rectangular region. h1, h2, w1,
        w2.
    nroi : (int, int, int, int), optional
        Not region of interest. Exclude this rectangular region. h1, h2, w1,
        w2.
    bgs_type: BackgroundSubtractor, default=bw.BackgroundSubtractorMOG2
        This can be any of the BackgroundSubtractors in Birdwatcher, e.g.
        BackgroundSubtractorMOG2, BackgroundSubtractorKNN,
        BackgroundSubtractorLSBP.

    Yields
    ------
    Frames
        Iterator that generates numpy array frames (height x width x color
        channel).

    """
    frames = vfs.iter_frames(startat=startat, nframes=nframes,
                             color=settings['color'])

    if settings['resizebyfactor'] != 1:
        val = settings['resizebyfactor']
        frames = frames.resizebyfactor(val,val)

        # resizebyfactor roi and nroi
        if roi is not None:
            roi = tuple([int(r*val) for r in roi])
        if nroi is not None:
            nroi = tuple([int(r*val) for r in nroi])

    if settings['blur']:
        val = settings['blur']
        frames = frames.blur((val,val))

    bgs_params = bgs_type().get_params()
    bgs_params.update((k, v) for k, v in settings.items() if k in bgs_params)
    bgs = bgs_type(**bgs_params)

    frames = frames.apply_backgroundsegmenter(bgs, learningRate=-1,
                                              roi=roi, nroi=nroi)
    if settings['morphologyex']:
        frames = frames.morphologyex(morphtype='open', kernelsize=2)

    return frames

def create_movementvideo(vfs, coords, startat=None, nframes=None,
                         videofilepath=None, draw_mean=True,
                         draw_framenumbers=(2, 25), crf=17, scale=None):
    """Create a nice video from the original video with movement detection
    results superimposed.

    Parameters
    ----------
    vfs : VideoFileStream
        A Birdwatcher VideoFileStream object
    coords : CoordinateArrays
        CoordinateArrays object with movement results.
    startat : str, optional
        If specified, start at this time point in the video file. You can use
        two different time unit formats: sexagesimal
        (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds.
    nframes  : int, optional
            Read a specified number of frames.
    videofilepath : Path or str, optional
        Output path. If None, writes to filepath of videofilestream.
    draw_mean : bool, default=True
        Draw the mean of the coordinates per frame, or not.
    draw_framenumbers : tuple, optional
        Draw frame numbers. A tuple of ints indicates where to draw
        them. The default (2, 25) draws numbers in the top left corner.
        To remove the framenumbers use None.
    crf : int, default=17
        Quality factor output video for ffmpeg. The default 17 is high
        quality. Use 23 for good quality.
    scale : tuple, optional
        (width, height). The default (None) does not change width and height.

    Returns
    -------
    VideoFileStream
        Videofilestream object of the output video.

    """
    if not isinstance(vfs, bw.VideoFileStream):
        raise TypeError(f"`vfs` parameter not a VideoFileStream object.")

    if videofilepath is None:
        videofilepath = derive_filepath(vfs.filepath, 'results',
                                        suffix='.mp4')

    videoframes = vfs.iter_frames(startat=startat, nframes=nframes)
    frames = (coords.iter_frames(nchannels=3, value=(0, 0, 255))
              .add_weighted(0.8, videoframes, 0.7))

    if draw_framenumbers is not None:
        frames = frames.draw_framenumbers(org=draw_framenumbers)
    if draw_mean:
        centers = coords.get_coordmean()
        frames = frames.draw_circles(centers=centers, radius=6,
                                     color=(255, 100, 0), thickness=2,
                                     linetype=16, shift=0)
        # centers_lp = np.array(
        #     [np.convolve(centers[:, 0], np.ones(7) / 7, 'same'),
        #      np.convolve(centers[:, 1], np.ones(7) / 7, 'same')]).T
        # frames = frames.draw_circles(centers=centers_lp, radius=6, color=(100, 255, 0), thickness=2, linetype=16, shift=0)
    vfs = frames.tovideo(videofilepath, framerate=vfs.avgframerate, crf=crf,
                         scale=scale)
    return vfs


#!/usr/bin/env python3
"""
Advent of Code 2016
Alex Troesch
"""

def find_distance_to_last_position(commands):
    position = (0, 0)
    direction = (0, 1)

    for (turn, dist) in commands:
        direction = change_direction(direction, turn)
        position = (position[0] + direction[0] * dist, position[1] + direction[1] * dist)

    return taxicab_distance(position)

def find_distance_to_revisited_position(commands):
    position = (0, 0)
    direction = (0, 1)

    visited = {}
    revisited = None
    visited[position] = True

    for (turn, dist) in commands:
        direction = change_direction(direction, turn)
        for _ in range(dist):
            position = (position[0] + direction[0], position[1] + direction[1])
            if position in visited and revisited is None:
                return taxicab_distance(position)
            else:
                visited[position] = True

    return -1

def taxicab_distance(pos):
    return sum(abs(x) for x in pos)

def change_direction(dir, turn):
    if turn == 'R':
        return (dir[1], -dir[0])
    elif turn == 'L':
        return (-dir[1], dir[0])

if __name__ == '__main__':
    file_name = 'input/01.txt'

    with open(file_name) as file:
        input = file.readline().split(',')
        commands = [(cmd.strip()[0], int(cmd.strip()[1:])) for cmd in input]

        print('Part One:', find_distance_to_last_position(commands))
        print('Part Two:', find_distance_to_revisited_position(commands))


# -*- coding: UTF-8 -*-
# 这不是我的原创，不过我从这份源码中学习到了很多，感觉非常受益。
# 我认真读过以后，又加了一些注释、进行了一点修改。
# import csv
import numpy as np
from numpy import matrix
from math import pow
from collections import namedtuple
import math
import random
import os
import json

import copy
import scipy.signal as signal

def rand_arr(a, b, *args):
    np.random.seed(0)
    return np.random.rand(*args) * (b - a) + a

class CNN:
    # 保存神经网络的文件路径，我没有加保存功能
    # NN_FILE_PATH = 'CNN.json'
    def __init__(self, data_matrix, data_labels, training_indices):
        self.C_SIZE = 5  #卷积核/滤波器的长与宽，据说一般要是奇数？28*28的图像，按经验C_SIZE=5
        self.F_NUM = 12  #全连接层的卷积核/滤波器的长与宽，这里就不是奇数了
        self.P_SIZE = 2  #池化窗口的长与宽
        self.MAX_ITER_NUM = 10 #本来是50，很花时间；但是也不要改成1，准确率太低了

        self.data_matrix = data_matrix #卷积层输入图像
        self.data_labels = data_labels

        cLyNum = 20
        pLyNum = 20
        fLyNum = 100
        oLyNum = 10
        self.cLyNum = cLyNum  #卷积层输出、池化层输入图像的个数 = 卷积核数 = feature map数
        self.pLyNum = pLyNum  #池化层输出、全连接层输入图像的个数, = cLyNum
        self.fLyNum = fLyNum  #全连接层输出图像深度，全连接层输出图像为matrix 1*fLyNum
        self.oLyNum = oLyNum  #输出神经元数目，matrix 1*fLyNum -> 1*oLyNum
        self.pSize = self.P_SIZE
        self.yita = 0.01  #学习率

        self.cLyBias = rand_arr(-0.1, 0.1, 1,cLyNum) #matrix 1*cLyNum
        self.fLyBias = rand_arr(-0.1, 0.1, 1,fLyNum) #matrix 1*fLyNum
        self.kernel_c = np.zeros((self.C_SIZE,self.C_SIZE,cLyNum))
        self.kernel_f = np.zeros((self.F_NUM,self.F_NUM,fLyNum))
        for i in range(cLyNum):
        	#各个卷积核也是参数，它的初始化也是随机的，之后也通过反向传播来更新
            self.kernel_c[:,:,i] = rand_arr(-0.1,0.1,self.C_SIZE,self.C_SIZE)
        for i in range(fLyNum):
            #全连接层的卷积核
            self.kernel_f[:,:,i] = rand_arr(-0.1,0.1,self.F_NUM,self.F_NUM)
        self.pooling_a = np.ones((self.pSize,self.pSize))/(self.pSize**2)
        self.weight_f = rand_arr(-0.1,0.1, pLyNum, fLyNum)
        self.weight_output = rand_arr(-0.1,0.1,fLyNum,oLyNum)

        # 训练并保存
        TrainData = namedtuple('TrainData', ['y0', 'label'])
        for i in range(self.MAX_ITER_NUM):
            #注意，用CNN的话不要把图像的灰度值归一处理，效果很差
            self.train([TrainData(self.data_matrix[i], int(self.data_labels[i])) for i in training_indices])
        #self.save()

    def _rand_initialize_weights(self, size_in, size_out):
        return [((x * 0.12) - 0.06) for x in np.random.rand(size_out, size_in)]

    #卷积层
    def convolution(self, data, kernel):
        data_row, data_col = np.shape(data)
        kernel_row, kernel_col = np.shape(kernel)
        n = data_col - kernel_col
        m = data_row - kernel_row
        state = np.zeros((m+1, n+1))
        for i in range(m+1):
            for j in range(n+1):
                temp = np.multiply(data[i:i+kernel_row,j:j+kernel_col], kernel)
                state[i][j] = temp.sum()
        return state
    # 池化层（通过平均池化来压缩，不过一般选用最大池化）
    def pooling(self, data, pooling_a):
        data_r, data_c = np.shape(data)
        p_r, p_c = np.shape(pooling_a)
        r0 = data_r/p_r
        c0 = data_c/p_c
        state = np.zeros((r0,c0))
        for i in range(c0):
            for j in range(r0):
                temp = np.multiply(data[p_r*i:p_r*(i+1),p_c*j:p_c*(j+1)],pooling_a)
                state[i][j] = temp.sum()
        return state
    #全连接层，相当于乘以一个weight_f的共用参数，再进行卷积
    def convolution_f1(self, state_p1, kernel_f1, weight_f1):
        #池化层出来的20个特征矩阵乘以池化层与全连接层的连接权重进行相加
        #wx(这里的偏置项=0),这个结果然后再和全连接层中的神经元的核
        #进行卷积,返回值:
        #1：全连接层卷积前,只和weight_f1相加之后的矩阵
        #2：和全连接层卷积完之后的矩阵

        #cLyNum, fLyNum
        n_p0, n_f = np.shape(weight_f1)#n_p0=20(是Feature Map的个数);n_f是100(全连接层神经元个数)
        #m_p, n_p, cLyNum
        m_p, n_p, pCnt = np.shape(state_p1)#这个矩阵是三维的
        #F_NUM, F_NUM, fLyNum
        m_k_f1, n_k_f1,fCnt = np.shape(kernel_f1)#12*12*100

        state_f1_temp = np.zeros((m_p,n_p,n_f))
        state_f1 = np.zeros((m_p - m_k_f1 + 1,n_p - n_k_f1 + 1,n_f))
        # state_p(m_c*n_c*cLyNum) dot weight_f(cLyNum*fLyNum) = state_f1_temp(m_c*n_c*fLyNum)
        # 本来矩阵乘法中，还要在最外层对state_p1的前两维进行循环；
        for n in range(n_f):
            count = 0
            for m in range(n_p0):
                # 不过这里可以直接在循环内部进行向量乘法、求和
                temp = state_p1[:,:,m] * weight_f1[m][n]
                count = count + temp
            state_f1_temp[:,:,n] = count
            # 再对state_f1_temp做卷积
            state_f1[:,:,n] = self.convolution(state_f1_temp[:,:,n], kernel_f1[:,:,n])
        return state_f1, state_f1_temp
    # softmax 层
    def softmax_layer(self,state_f1):
        # print 'softmax_layer'
        output = np.zeros((1,self.oLyNum))
        t1 = (np.exp(np.dot(state_f1,self.weight_output))).sum()
        for i in range(self.oLyNum):
            t0 = np.exp(np.dot(state_f1,self.weight_output[:,i]))
            output[:,i]=t0/t1
        return output
    #误差反向传播更新权值，我还不是很懂
    def cnn_upweight(self,err_cost, ylab, train_data,state_c1, state_s1, state_f1, state_f1_temp, output):
        #print 'cnn_upweight'
        m_data, n_data = np.shape(train_data)
        # softmax的资料请查看 (TODO)
        label = np.zeros((1,self.oLyNum))
        label[:,ylab] = 1
        delta_layer_output = output - label  #最终输出层的误差（符号是负的，所以更新时是-=而不是+=）

        weight_output_temp = copy.deepcopy(self.weight_output)
        delta_weight_output_temp = np.zeros((self.fLyNum, self.oLyNum))
        #print np.shape(state_f1)
        #更新weight_output
        for n in range(self.oLyNum):
            # state_fo dot weight_output + bias 再激活-> layer_output
            # 故weight_output += yita * (delta_layer_output dot state_fo)
            delta_weight_output_temp[:,n] = delta_layer_output[:,n] * state_f1
        weight_output_temp = weight_output_temp - self.yita * delta_weight_output_temp

        #更新bias_f和kernel_f (推导公式请查看 TODO)
        delta_layer_f1 = np.zeros((1, self.fLyNum))
        delta_bias_f1 = np.zeros((1,self.fLyNum))
        delta_kernel_f1_temp = np.zeros(np.shape(state_f1_temp))
        kernel_f_temp = copy.deepcopy(self.kernel_f)
        for n in range(self.fLyNum):
            # 这层的误差（即delta_layer_f1）=
            # 下一层的误差（即delta_layer_output）
            # dot 这层的参数（即weight_output）
            # 整体 multiply 上层的激活函数的导数代入sum（即tanh^-1(state_f_pre)）
            count = 0
            for m in range(self.oLyNum):
                count = count + delta_layer_output[:,m] * self.weight_output[n,m]
            #本来是multiply，但是这里展开对每一维计算，每一维都是数*数，dot也可以
            delta_layer_f1[:,n] = np.dot(count, (1 - np.tanh(state_f1[:,n])**2))
            delta_bias_f1[:,n] = delta_layer_f1[:,n]
            delta_kernel_f1_temp[:,:,n] = delta_layer_f1[:,n] * state_f1_temp[:,:,n]
        # 1
        self.fLyBias = self.fLyBias - self.yita * delta_bias_f1
        kernel_f_temp = kernel_f_temp - self.yita * delta_kernel_f1_temp

        #更新weight_f1
        delta_layer_f1_temp = np.zeros((self.F_NUM,self.F_NUM,self.fLyNum))
        delta_weight_f1_temp = np.zeros(np.shape(self.weight_f))
        weight_f1_temp = copy.deepcopy(self.weight_f)
        for n in range(self.fLyNum):
            delta_layer_f1_temp[:,:,n] = delta_layer_f1[:,n] * self.kernel_f[:,:,n]
        for n in range(self.pLyNum):
            for m in range(self.fLyNum):
                temp = delta_layer_f1_temp[:,:,m] * state_s1[:,:,n]
                delta_weight_f1_temp[n,m] = temp.sum()
        weight_f1_temp = weight_f1_temp - self.yita * delta_weight_f1_temp

        # 更新bias_c1
        n_delta_c = m_data - self.C_SIZE + 1
        delta_layer_p = np.zeros((self.F_NUM,self.F_NUM,self.pLyNum))
        delta_layer_c = np.zeros((n_delta_c,n_delta_c,self.pLyNum))
        delta_bias_c = np.zeros((1,self.cLyNum))
        for n in range(self.pLyNum):
            count = 0
            for m in range(self.fLyNum):
                count = count + delta_layer_f1_temp[:,:,m] * self.weight_f[n,m]
            delta_layer_p[:,:,n] = count
            #print np.shape(np.kron(delta_layer_p[:,:,n], np.ones((2,2))/4))
            delta_layer_c[:,:,n] = np.kron(delta_layer_p[:,:,n], np.ones((2,2))/4) * (1 - np.tanh(state_c1[:,:,n])**2)
            delta_bias_c[:,n] = delta_layer_c[:,:,n].sum()
        # 2
        self.cLyBias = self.cLyBias - self.yita * delta_bias_c
        #更新 kernel_c1
        delta_kernel_c1_temp = np.zeros(np.shape(self.kernel_c))
        for n in range(self.cLyNum):
            temp = delta_layer_c[:,:,n]
            r1 = map(list,zip(*temp[::1]))#逆时针旋转90度
            r2 = map(list,zip(*r1[::1]))#再逆时针旋转90度
            temp = signal.convolve2d(train_data, r2,'valid')
            temp1 = map(list,zip(*temp[::1]))
            delta_kernel_c1_temp[:,:,n] = map(list,zip(*temp1[::1]))
        self.kernel_c = self.kernel_c - self.yita * delta_kernel_c1_temp
        self.weight_f = weight_f1_temp
        self.kernel_f = kernel_f_temp
        self.weight_output = weight_output_temp

    def train(self, training_data_array):
        for data_total in training_data_array:
            data = data_total.y0
            ylab = data_total.label

            self.predict(data, ylab)  #预测并训练

    def predict(self, test, ylab=None):
        data = test.reshape(28,28)
        d_m, d_n = np.shape(data)  #卷积层输入图像的长与宽
        # 假设输入图像为w1 * h1 * c1，w1表示宽度，h1表示高度，c1表示通道数（图像的深度），输出图像为w2 * h2 * c2
        # 在卷积层中，n表示卷积核的个数，k*k表示卷积核大小，p表示扩充边缘，s表示卷积核步长，那么有：
        # w2 = (w1 + 2 * p - k) / s + 1
        # h2 = (h1 + 2 * p - k) / s + 1
        # c2 = n
        # 这里做步长stride=1的卷积，扩充边缘p=0
        m_c = d_m - self.C_SIZE + 1  #卷积层输出、池化层输入图像的长与宽
        n_c = d_n - self.C_SIZE + 1  #d_m = d_n = 28, C_SIZE = 5, 28-5+1 = 24
        m_p = m_c/self.pSize #m_c = n_c = 24, pSize = 2, 24/2 = 12
        n_p = n_c/self.pSize
        state_c = np.zeros((m_c, n_c,self.cLyNum))  #卷积层输出、池化层输入图像的深度
        state_p = np.zeros((m_p, n_p, self.pLyNum))  #池化层输出图像的深度
        for n in range(self.cLyNum):
            state_c[:,:,n] = self.convolution(data, self.kernel_c[:,:,n])  #卷积
            tmp_bias = np.ones((m_c,n_c)) * self.cLyBias[:,n]  # 图像矩阵的每一位都加上偏置
            state_c[:,:,n] = np.tanh(state_c[:,:,n] + tmp_bias)# 加上偏置项然后过激活函数
            state_p[:,:,n] = self.pooling(state_c[:,:,n],self.pooling_a) #池化层输出压缩过的图像
        #全连接层
        #m_p = n_p = 12, F_NUM = 12, 12-12+1 = 1
        #state_f为1*1*fLyNum，state_f_pre为m_c*n_c*fLyNum
        # 变成1维才能作为普通神经网络的输入，进入softmax层
        state_f, state_f_pre = self.convolution_f1(state_p,self.kernel_f, self.weight_f)
        #进入激活函数
        state_fo = np.zeros((1,self.fLyNum))#全连接层经过激活函数的结果
        for n in range(self.fLyNum):
            state_fo[:,n] = np.tanh(state_f[:,:,n] + self.fLyBias[:,n])
        #进入softmax层
        output = self.softmax_layer(state_fo)
        #计算误差
        y_pre = output.argmax(axis=1)

        # 如果预测完了还要进行训练
        if ylab != None:
            err = -output[:,ylab]
            #print output
            #计算误差
            #print err
            self.cnn_upweight(err,ylab,data,state_c,state_p, state_fo, state_f_pre, output)
            # print self.kernel_c
            # print self.cLyBias
            # print self.weight_f
            # print self.kernel_f
            # print self.fLyBias
            # print self.weight_output
        return y_pre


from datasets import load_dataset
from transformers import  AutoTokenizer
import torch

class PegasusDataset(torch.utils.data.Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels
    def __getitem__(self, idx):
        item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
        item['labels'] = torch.tensor(self.labels['input_ids'][idx])  # torch.tensor(self.labels[idx])
        return item
    def __len__(self):
        return len(self.labels["input_ids"])

def prepare_data(model_name,
                 train_texts, train_labels,
                 val_texts=None, val_labels=None,
                 test_texts=None, test_labels=None):
  """
  Prepare input data for model fine-tuning
  """
  tokenizer = AutoTokenizer.from_pretrained(model_name)

  prepare_val = False if val_texts is None or val_labels is None else True
  prepare_test = False if test_texts is None or test_labels is None else True

  def tokenize_data(texts, labels):
    encodings = tokenizer(texts, truncation=True, padding='longest')
    decodings = tokenizer(labels, truncation=True, padding='longest')
    dataset_tokenized = PegasusDataset(encodings, decodings)
    return dataset_tokenized

  train_dataset = tokenize_data(train_texts, train_labels)
  val_dataset = tokenize_data(val_texts, val_labels) if prepare_val else None
  test_dataset = tokenize_data(test_texts, test_labels) if prepare_test else None

  return train_dataset, val_dataset, test_dataset


# -*- coding: utf-8 -*-
"""
Created on Wed Jun 22

@author: Xiang Li
"""

'''

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet,
return its corresponding column number.

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28
'''

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        base = ord("A") - 1
        colname = [ord(char) - base for char in s]
        power = reversed([26**x for x in range(len(s))])
        return sum([a*b for a,b in zip(colname,power)])

#%% test

test = Solution()
test.titleToNumber("AAA")


"""
Blind 75
https://leetcode.com/problems/flood-fill/

An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].

To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with color.

Return the modified image after performing the flood fill.

Time: O(row * col)
Space: O(row * col)

Hint:
    - Use DFS
"""

from typing import List
import unittest

def dfs(image: List[List[int]], row: int, col: int, new_color: int, start_color: int) -> List[List[int]]:
    # Set boundary for DFS
    if row < 0 or row >= len(image) or col < 0 or col >= len(image[0]) or image[row][col] != start_color:
        return

    # Update row col to new color
    image[row][col] = new_color

    # DFS to up down left right
    dfs(image, row - 1, col, new_color, start_color)
    dfs(image, row + 1, col, new_color, start_color)
    dfs(image, row, col - 1, new_color, start_color)
    dfs(image, row, col + 1, new_color, start_color)

def flood_fill(image: List[List[int]], start_row:int, start_col: int, new_color: int) -> List[List[int]]:
    # Skip if the row col already new color
    if image[start_row][start_col] == new_color:
        return image

    # DFS
    dfs(image, start_row, start_col, new_color, image[start_row][start_col])

    return image

class TestCase(unittest.TestCase):
    def test_case1(self):
        image = [[1,1,1],[1,1,0],[1,0,1]]
        start_row = 1
        start_col = 1
        color = 2

        result = [[2,2,2],[2,2,0],[2,0,1]]

        return self.assertEqual(flood_fill(image, start_row, start_col, color), result)

    def test_case2(self):
        image = [[0,0,0],[0,0,0]]
        start_row = 0
        start_col = 0
        color = 0

        result = [[0, 0, 0], [0, 0, 0]]

        return self.assertEqual(flood_fill(image, start_row, start_col, color), result)

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


############## threat hunting ##############

import pandas as pd
import pprint

from collections import Counter
from malware_analysis import LOG_FILE
from malware_analysis import DISPOSITION
from malware_analysis import read_logs

pp = pprint.PrettyPrinter(indent=4)
dot = '.'

def get_data():
    """
    make lists of each category.
    :param:
    :return: a tuple of lists
    """
    logs = read_logs(LOG_FILE)
    data_unknown = []
    data_clean = []
    data_malicious = []

    for log in logs:
        if log['dp'] == DISPOSITION.UNKNOWN.value:
            data_unknown.append(log)
        elif log['dp'] == DISPOSITION.MALICIOUS.value:
            data_malicious.append(log)
        elif log['dp'] == DISPOSITION.CLEAN.value:
            data_clean.append(log)

    return (data_unknown, data_malicious, data_clean)

def count_extensions(data):
    """
    count occurences of extensions of files per category.
    :param data: list of log entries
    :return: list of tuples of 10 most common extensions
    """
    cnt = Counter()  # record occurences for each file extension

    for log in data:
        if len(log['nm']) > 0:
            extension = log['nm'].split('.')[1]
            cnt[extension] += 1

    return cnt.most_common(10)

def count_parent_folders(data):
    """
    count occurences of parent folders of files per category.
    :param data: list of log entries
    :return: list of tuples of 10 most common parent folders
    """
    cnt = Counter()

    for log in data:
        ph_arr = log['ph'].split('/')
        p_folder = ph_arr[-1]

        if dot in p_folder or len(p_folder) == 0:
            p_folder = ph_arr[-2]
        cnt[p_folder] += 1

    return cnt.most_common(10)

data_u, data_m, data_c = get_data()

df_m = pd.DataFrame(data_m)  # malicious
df_u = pd.DataFrame(data_u)  # unknown
df_c = pd.DataFrame(data_c)  # clean

mean_pt_malicious = df_m['pt'].mean()
mean_pt_clean = df_c['pt'].mean()
mean_pt_unknown = df_u['pt'].mean()

# avearge pt for each category is almost the same - bummer :(
print(f"""mean processing time for:

unknown: {mean_pt_unknown}
clean: {mean_pt_clean}
malicious: {mean_pt_malicious}""")

# some extensions are common b/w these two categories
count_extensions(data_m)
count_extensions(data_u)

# we can see parent folders of these two categories are very similar
count_parent_folders(data_m)
count_parent_folders(data_u)

"""
Final thoughts:
In order ot formalize our hypothesis we need to consider our IOCs:
Each log entry has following attritbutes:

ts:    timestamp - we compare timelines of each category and see which timeline the unknowns resemble the closest.
pt:    processing time - mean pt across all three categories was almost the same.
si:    session ID - this is unique to each log entry might not be as useful.
uu:    connector GUID - same as session ID.
bg:    business GUID - same as session ID.
sha:   sha 256 of the file - unique to each entry.
nm:    file name - I counted the extensions of files per category and was able to identify which .extensions were cuasing the most trouble we could add guards against such extensions in our cloud server.
ph:    path - The paths followed by seen MALICIOUS files and the UNKNOWN ones were suprisingly similar - this could be a strong indicator of compromise.
dp:    disposition (valid values: MALICIOUS (1), CLEAN (2), UNKNOWN (3))

Hypothesis:
Two types of folders seem to be causing most malicious activity i.e. folders with sports and astronomy information.
We can do statistical hypothesis testing to conform our hypothesis.
"""


#!/usr/bin/env python3

import bisect

def main():
    lis = []
    for elem in [int(input()) for _ in range(int(input()))]:
        if len(lis) == 0 or lis[-1] < elem:
            lis.append(elem)
        else:
            lis[bisect.bisect_left(lis, elem)] = elem
    print(len(lis))

if __name__ == "__main__":
    main()


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

'''
Dahu: Picture gallery
Licence: BSD (see LICENCE file)

Author: Damien Riquet <d.riquet@gmail.com>
Description:
    Create a context for web requests
'''

import os
from flask import url_for, current_app, session
from dahu.core import album, permission

def split_path(s):
    """
    This function split properly a path
    For example: 'A/B/C/D' will be splitted as ['A', 'B', 'C', 'D']
    """
    head, tail = os.path.split(s)
    if head == '':
        return tail,
    return split_path(head) + (tail,)

def get_linked_path(path):
    """
    Return the path (and corresponding links) of albums to reach the current album
    It returns a list as follow: [(album, link), ..., (album, link)]
    """
    splitted_path = split_path(path)
    full_path = [('Home', url_for('.show_album', album_path=''))]
    current_path = ''

    for p in splitted_path:
        if p:
            current_path = os.path.join(current_path, p)
            full_path.append((p, url_for('.show_album', album_path=current_path)))

    return full_path

def generate_album_context(current_album):
    """ Generate data context (pictures, links, and so on) for the current album """
    context = {}
    p_config = permission.get_config(current_app.config['CACHE_PATH'])

    # Related data about the album
    context['name'] = current_album
    context['pictures'] = album.get_album_pictures(current_app.config['ALBUMS_PATH'], current_album)

    # Generate inner albums related data
    context['inner_albums'] = []
    for a in album.get_album_dirs(current_app.config['ALBUMS_PATH'], current_album):
        if permission.is_public_album(p_config, os.path.join(current_album,a)) or 'username' in session:
            context['inner_albums'].append({
                'name' : a,
                'link' : url_for('.show_album', album_path=os.path.join(current_album, a)),
                'path' : os.path.join(current_album, a),
                'nb_pictures' : album.get_album_number_pictures(current_app.config['ALBUMS_PATH'], os.path.join(current_album, a)),
                'total_nb_pictures' : album.get_album_total_number_pictures(current_app.config['ALBUMS_PATH'],  os.path.join(current_album, a)),
            })

    # Generate the path of the current album
    context['linked_path'] = get_linked_path(current_album)

    return context


#!/usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan

def analyze(data):
    print(len(data.ranges))
    angle = data.angle_min - data.angle_max
    print(angle)
    print(angle/-data.angle_increment)

def listener():
    rospy.init_node('listener', anonymous=True)

    msg = LaserScan()
    msg = rospy.wait_for_message('b_scan', LaserScan)
    analyze(msg)

if __name__ == '__main__':
    listener()


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

# ## The following code does an analysis on stock price of Microsoft and makes a buying and seling decision based on 15ma and 200 #ma rule
# ### 1. Stock price of 5 years from 2015 to 2020 is fetched via Alpha vantage API
# ### 2. The buying decision is taken when the 15 ma signal crosses 200 ma signal from above
# ### 3. The selling action is taken when 15 ma signal crosses the 200 ma signal from below
# ### 4. Finding the final wealth of the trader, for both strategies, to an accuracy of two (2) decimal points
# ### 5. Plotting the outcome of the trader

# In[32]:

from alpha_vantage.timeseries import TimeSeries
import pandas as pd
import numpy as np
from alpha_vantage.techindicators import TechIndicators
import matplotlib.pyplot as plt

# In[33]:

api_key = '' # provide user api key
stock = 'MS'
start_dt = np.datetime64('2015-01-01')
end_dt = np.datetime64('2020-12-31')
long_MA = 200 #days
short_MA = 15 #days
initial_wealth = 1000

# ## Make use of the alpha vantage module to download the prices of stock from start_dt to end_dt.

# In[34]:

def slice_series(dt_frm, start_date, end_date):
#Return sliced time series based on the start ate and end date provided
    data_slice1 = dt_frm[dt_frm.index > start_date]
    data_slice2 = data_slice1[data_slice1.index <end_dt]
    return data_slice2

# In[46]:

ts = TimeSeries(key=api_key, output_format='pandas')
data, meta_data = ts.get_daily('MS', outputsize='full')
dt_data = slice_series(data,start_dt,end_dt)
print(f'The stock data of {stock} from {start_dt} to {end_dt} is as follows.')
dt_data

# ## Assumptions made:
#
# ### 1. The trading strategy is assumed to be without **human intervention**
#
# ### 2. The buying decision is taken when the 15 ma signal crosses 200 ma signal from above(sensing that the stock is undervalued now). If the price continues to fall, no discretion is made and the stock is hold.
#
# ### 3. The selling action is taken when 15 ma signal crosses the 200 ma signal from below(sensing that the stock is overvalued now)
#
# ### 4. The buy and sell will be done automatically even if the transaction ends in loss. No other strategies is considered
#

# In[36]:

ti = TechIndicators(key=api_key, output_format='pandas')
#downloading 15 ma
data_ti, meta_data_ti = ti.get_sma(symbol='MS', interval='daily',
                                    time_period=short_MA, series_type='close')
#downloading 200 ma
data_t2, meta_data_t2 = ti.get_sma(symbol='MS', interval='daily',
                                    time_period=long_MA, series_type='close')

# In[49]:

#slicing the data with the desired time period
dt_short_sma = slice_series(data_ti,start_dt,end_dt)
dt_long_sma = slice_series(data_t2,start_dt,end_dt)

#renaming the colums to plot
df1 = dt_short_sma
df1 = df1.rename(columns={'SMA': 'short ma'})
df2 = dt_long_sma
df2 = df2.rename(columns={'SMA': 'long ma'})

# ploting the trading signals
total_df = pd.concat([df2,df1], axis=1)
fig = plt.figure(figsize= [15,4])
total_df.plot()
plt.title('Trading Signal')
plt.show()

# In[44]:

#Adding closing price to the moving averages
total_df = pd.concat([dt_data['4. close'], df1,df2], axis=1)
total_df = total_df.rename(columns={'4. close': 'Closing price'})

#Adding signal to the series
len_total = len(total_df.index)
df_signal = total_df.assign(signal = np.zeros(len_total))
df_signal.loc[df_signal['short ma'] < df_signal['long ma'] , 'signal'] = 1

#creating buy signal
df_signal = df_signal.assign(buysignal = np.zeros(len_total))
df_signal = df_signal.assign(sellsignal = np.zeros(len_total))
for i in range(1,len_total) :
    df_signal.iloc[i,4] = df_signal.iloc[i, 3]-df_signal.iloc[i-1, 3]
df_signal.loc[df_signal['buysignal'] == -1, 'buysignal'] = 0

#creating sell signal
for i in range(len_total) :
    df_signal.iloc[i,5] = df_signal.iloc[i, 3]-df_signal.iloc[i-1, 3]
df_signal.loc[df_signal['sellsignal'] == 1, 'sellsignal'] = 0
df_signal.loc[df_signal['sellsignal'] == -1, 'sellsignal'] = 1

#finding buying and selling dates
sell_dates = df_signal[df_signal['sellsignal']==1].index
buy_dates = df_signal[df_signal['buysignal']==1].index
buy_sell_detail = pd.DataFrame()
buy_sell_detail = buy_sell_detail.assign(Buy_Date = buy_dates)
buy_sell_detail = buy_sell_detail.assign(Buy_Price = df_signal.loc[buy_dates]['Closing price'].tolist())
buy_sell_detail = buy_sell_detail.assign(Sell_Date = sell_dates)
buy_sell_detail = buy_sell_detail.assign(Sell_Price = df_signal.loc[sell_dates]['Closing price'].tolist())
print('The buy and sell dates and corresponding prices will be as shown below')
print(buy_sell_detail)

#
# ## Finding the final wealth of the trader, for both strategies, to an accuracy of two (2) decimal points

# In[39]:

#Creating BUY LOW and SELL HIGH dates
#initial wealth is $1000 till the first buy and sell happens.
df_signal = df_signal.assign(wealth_ma = np.zeros(len_total))
wealth_ma = initial_wealth
num_buy_sell = len(buy_sell_detail)

#intialising intial_wealth as accumulated wealth till first signal is observed
first_buy = buy_sell_detail['Buy_Date'][0]
df_signal.loc[df_signal.index < first_buy , 'wealth_ma'] = wealth_ma

#Calculating the wealth
for i in range(num_buy_sell):
    if(i>0):
        last_sell = buy_sell_detail['Sell_Date'][i-1]
        df_signal.loc[df_signal.index > last_sell , 'wealth_ma'] = wealth_ma
    buy_p = buy_sell_detail['Buy_Price'][i]
    units_p = wealth_ma/buy_p
    start_i=buy_sell_detail['Buy_Date'][i]
    end_i = buy_sell_detail['Sell_Date'][i]
    wealth_x = wealth_ma
    for x in df_signal[start_i:end_i].index:
        wealth_x = df_signal[df_signal.index == x]['Closing price'] * units_p
        df_signal.loc[df_signal.index == x , 'wealth_ma'] = wealth_x[0]
    wealth_ma = wealth_x[0]

#prefilling the last wealth accumulated till the end of period
last_sell = buy_sell_detail['Sell_Date'][num_buy_sell-1]
df_signal.loc[df_signal.index > last_sell , 'wealth_ma'] = wealth_ma

# In[40]:

#Creatiny BUY and HOLD strategy
#buy and hold
price_buy = total_df['Closing price'][0]
units = initial_wealth/price_buy
len_period = len(df_signal)
df_signal = df_signal.assign(wealth_hold = np.zeros(len_total))
df_signal['wealth_hold'] = units*total_df['Closing price']
final_wealth = df_signal['wealth_hold'][len_period-1]
print(f'The wealth accumulated by investing ${initial_wealth} and holding is ${final_wealth:.2f}.')
print(f'The wealth accumulated by investing ${initial_wealth} and using the moving avaerge buy/sell strategy is ${wealth_ma:.2f}.')

# ##  Plotting the outcome of the trader

# In[42]:

#Plotting the result
plt.clf()
df_signal[['wealth_ma','wealth_hold']].plot()
plt.show()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author   : Rancho Cooper
# @date     : 2016-12-23 13:41
# @email    : ranchocooper@gmail.com

t = int(input())

for _ in range(t):
    input()
    nums = list(map(int, input().split()))
    n, i = len(nums), 0
    while i < n - 1 and nums[i] >= nums[i + 1]:
        i += 1
    while i < n - 1 and nums[i] <= nums[i + 1]:
        i += 1
    print("Yes" if i == n - 1 else "No")


from django.db import models
from .user import User
from .teams import Team
from .document import Document
from django.core.validators import MaxValueValidator, MinValueValidator

class UserAccess(models.Model):
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    for_user = models.ForeignKey(User, on_delete=models.CASCADE, default=None)
    permission_level = models.IntegerField(
        validators=[
            MaxValueValidator(4),
            MinValueValidator(1)
        ]
    )
    class Meta:
        unique_together =  ('for_user', 'document')

class TeamAccess(models.Model):
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    for_team = models.ForeignKey(Team, on_delete=models.CASCADE)
    permission_level = models.IntegerField(
        validators=[
            MaxValueValidator(4),
            MinValueValidator(1)
        ]
    )


import os
from typing import Tuple, Union

import pathlib

import torch
import torchvision
import yacs.config

from torch.utils.data import Dataset

from pytorch_image_classification import create_transform

# DATASETS_ROOT = "/root/autodl-tmp"
DATASETS_ROOT = "/media/joshuawen/Joshua_SSD3/Datasets/RGB/classification"
CUSTOM_DATASETS = ['RS19-WHU', 'RSSCN7', 'Google-China', 'EuroSAT', 'AID',
                   'UCMerced_LandUse', 'RSD46-WHU', 'PatternNet', 'RSI-CB128', 'RSI-CB256']

class SubsetDataset(Dataset):
    def __init__(self, subset_dataset, transform=None):
        self.subset_dataset = subset_dataset
        self.transform = transform

    def __getitem__(self, index):
        x, y = self.subset_dataset[index]
        if self.transform:
            x = self.transform(x)
        return x, y

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

def create_dataset(config: yacs.config.CfgNode,
                   is_train: bool) -> Union[Tuple[Dataset, Dataset], Dataset]:
    if config.dataset.name in [
            'CIFAR10',
            'CIFAR100',
            'MNIST',
            'FashionMNIST',
            'KMNIST',
    ]:
        module = getattr(torchvision.datasets, config.dataset.name)
        if is_train:
            if config.train.use_test_as_val:
                train_transform = create_transform(config, is_train=True)
                val_transform = create_transform(config, is_train=False)
                train_dataset = module(config.dataset.dataset_dir,
                                       train=is_train,
                                       transform=train_transform,
                                       download=True)
                test_dataset = module(config.dataset.dataset_dir,
                                      train=False,
                                      transform=val_transform,
                                      download=True)
                return train_dataset, test_dataset
            else:
                dataset = module(config.dataset.dataset_dir,
                                 train=is_train,
                                 transform=None,
                                 download=True)
                val_ratio = config.train.val_ratio
                assert val_ratio < 1
                val_num = int(len(dataset) * val_ratio)
                train_num = len(dataset) - val_num
                lengths = [train_num, val_num]
                train_subset, val_subset = torch.utils.data.dataset.random_split(
                    dataset, lengths)

                train_transform = create_transform(config, is_train=True)
                val_transform = create_transform(config, is_train=False)
                train_dataset = SubsetDataset(train_subset, train_transform)
                val_dataset = SubsetDataset(val_subset, val_transform)
                return train_dataset, val_dataset
        else:
            transform = create_transform(config, is_train=False)
            dataset = module(config.dataset.dataset_dir,
                             train=is_train,
                             transform=transform,
                             download=True)
            return dataset
    elif config.dataset.name == 'ImageNet':
        dataset_dir = pathlib.Path(config.dataset.dataset_dir).expanduser()
        train_transform = create_transform(config, is_train=True)
        val_transform = create_transform(config, is_train=False)
        train_dataset = torchvision.datasets.ImageFolder(
            dataset_dir / 'train', transform=train_transform)
        val_dataset = torchvision.datasets.ImageFolder(dataset_dir / 'val',
                                                       transform=val_transform)
        return train_dataset, val_dataset
    elif config.dataset.name in CUSTOM_DATASETS:
        train_transform = create_transform(config, is_train=True)
        val_transform = create_transform(config, is_train=False)
        train_dataset = torchvision.datasets.ImageFolder(
            os.path.join(DATASETS_ROOT, config.dataset.name, 'train'), transform=train_transform)
        val_dataset = torchvision.datasets.ImageFolder(
            os.path.join(DATASETS_ROOT, config.dataset.name, 'val'), transform=val_transform)
        return train_dataset, val_dataset
    else:
        raise ValueError()


from flask import abort
import json
from utils import read_json

FILE_PATH = 'tasks.json'

def get_tasks(completed: bool = None, title: str = None):
    file = read_json(FILE_PATH)
    if completed != None and title != None:
        dataframe = file[(file["title"]==title) & (file["completed"] == completed)]
    elif title != None or completed != None:
        if completed != None:
            dataframe = file[file["completed"]==completed]
        else:
            dataframe = file[file["title"]==title]
    else:
        dataframe = file
    return json.loads(dataframe.to_json(orient = 'records')), dataframe.shape[0]

def get_tasks_by_id(id: int):
    file = read_json(FILE_PATH)
    dataframe = file[file["id"]==id]
    try:
        data = json.loads(dataframe.to_json(orient = 'records'))[0]
        return data
    except IndexError:
        return abort(404)

def get_tasks_by_user_id(user_id: int, completed = None, title = None):
    file = read_json(FILE_PATH)
    if completed != None and title != None:
        dataframe = file[(file["title"]==title) & (file["completed"] == completed ) & (file["user_id"] == user_id)]
    elif title != None or completed != None:
        if completed != None:
            dataframe = file[(file["completed"]==completed) & (file["user_id"] == user_id)]
        else:
            dataframe = file[(file["title"]==title) & (file["user_id"] == user_id)]
    else:
        dataframe = file[file["user_id"]==user_id]

    return json.loads(dataframe.to_json(orient = 'records')), dataframe.shape[0]


#!/usr/bin/env python

import os
import sys
import json
import pylab
import numpy as np
import matplotlib.ticker
from dateutil import parser
from scipy import interpolate
from matplotlib.dates import date2num
from datetime import timedelta, datetime
from pysurvey.plot import setup, dateticks, minmax, hcolorbar

FILENAME = os.path.expanduser('~/.temperature.neon.log')
SEP = ' : '
YR = [30,75]
TNORM = 38

FILENAME = os.path.expanduser('~/.temperature.xenon.log')
SEP = ' | '
YR = [5,60]
TNORM = 25

def read_temps(filename=FILENAME):
    out = []
    with open(filename, 'r') as f:
        lines = f.readlines()
        for line in lines:
            items = line.split(SEP)
            out.append(dict(date=parser.parse(items[1]),
                            temperature=float(items[2])/1000.0))
    return out

def setupplot(secondax=False, **kwargs):
    ytickv = np.linspace(YR[0],YR[1],6)
    yticknames = map('{:0.0f}'.format, ytickv)
    tmp = dict(
        ylabel='Temperature [c]',
        yr=minmax(ytickv), ytickv=ytickv,
        yticknames=yticknames,
    )
    tmp.update(kwargs)
    ax = setup(**tmp)

    if secondax:
        subplt = kwargs.get('subplt',None)
        f = lambda x: '{:0.0f}'.format(1.8*x + 32.0)
        yticknames = map(f, ytickv)
        ax2 = ax.twinx()
        ax2.set_ylabel(r"Temperature [F]")
        ax2.set_ylim(minmax(ytickv))
        ax2.yaxis.set_major_locator(matplotlib.ticker.FixedLocator(ytickv))
        ax2.yaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(yticknames))
        pylab.sca(ax)

        # setup(ax=ax.twinx(),
        #       subplt=subplt,
        #       ylabel='Temperature [F]',
        #       yr=minmax(ytickv), ytickv=ytickv, yticknames=yticknames)

    return ax

def get_continuum(dates, x, y, delta=2):
    out = []
    t = timedelta(hours=delta)
    for d in dates:
        ii = np.where( (date2num(x) >  date2num(d-t) ) &
                       (date2num(x) <= date2num(d+t) ) )[0]
        if len(ii) <= 20:
            out.append(-1)
        else:
            out.append( np.mean(y[ii]) )

    t = date2num(dates)
    n = np.min(t)
    tmp = np.array(out)
    ii = np.where(tmp > 0)
    f = interpolate.UnivariateSpline(date2num(dates[ii])-n,tmp[ii], s=3)
    out = f(t-n)
    return out

def plot_weather(mindate):
    weatherfile = os.path.expanduser('~/data/weather.json')
    wf = json.load(open(weatherfile,'r'))
    keys = sorted(wf.keys())
    x = np.array(map(np.float, keys))
    y = np.array(map(np.float, [wf[k]['tempm'] for k in keys]))+50
    ii = np.where(x > mindate)
    pylab.plot(x[ii],y[ii])

def plot_temp():
    data = read_temps()
    dates, values = map(np.array, zip(*[(d['date'], d['temperature'])
                                        for d in data]))
    tmp = (date2num(dates) % 1.0)*24.0
    ii = np.where((tmp > 0) & (tmp < 8))[0]
    continuum = get_continuum(dates, dates[ii], values[ii])

    setup(figsize=(12,6))

    setupplot(subplt=(1,2,1), autoticks=True, xlabel='Date',)
    pylab.plot(dates, values)
    pylab.plot(dates[ii], values[ii], '.r')
    pylab.plot(dates, continuum, '.k')
    plot_weather(np.min(date2num(dates)))
    # pylab.plot(dates, values-continuum+38, '.r')
    dateticks('%Y.%m.%d')

    setupplot(subplt=(2,2,2), autoticks=False, xlabel='Hour of Day')
    pylab.plot(tmp, values, '.')
    setupplot(subplt=(2,2,2), ylabel='', secondax=True)

    setupplot(subplt=(2,2,4), autoticks=False, xlabel='Hour of Day')
    sc = pylab.scatter(tmp, values-continuum+TNORM,
                       c=date2num(dates)-np.min(date2num(dates)), s=15,
                       marker='.', edgecolor='none',
                       label='Days since Start')

    setupplot(subplt=(2,2,4), ylabel='', secondax=True)
    hcolorbar(sc, axes=[0.75, 0.42, 0.1, 0.01])

    pylab.tight_layout()
    pylab.show()

if __name__ == '__main__':
    from pysurvey import util
    util.setup_stop()
    plot_temp()


#Trapping Rain Water
class Solution:
    def trap(self, height: List[int]) -> int:
        if not height:
            return 0
        rightMax = [0] * len(height)
        leftMax = [0] * len(height)
        leftMax[0] = height[0]
        l = height[0]
        r = height[len(height)-1]
        ans = 0
        rightMax[len(height)-1] = height[len(height)-1]
        for i in range(1,len(height)):
            if l < height[i]:
                l = height[i]
            leftMax[i] = l

        for i in range(len(height)-2,-1,-1):
            if r < height[i]:
                r = height[i]
            rightMax[i] = r

        for i in range(0,len(height)):
            minHeight = min(leftMax[i],rightMax[i])
            if height[i] < minHeight:
                ans += (minHeight - height[i])
        return ans


import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("DOOM")
canvas = tk.Canvas(window, width=800, height=600)
canvas.pack()

# Draw the background
canvas.create_rectangle(0, 0, 800, 600, fill="black")

# Draw the DOOM logo
canvas.create_text(400, 200, text="DOOM", fill="red", font=("Helvetica", 80, "bold"))

# Draw the character
canvas.create_rectangle(280, 250, 520, 450, fill="gray")

# Draw the character's head
canvas.create_oval(330, 160, 470, 300, fill="gray")

# Draw the character's eyes
canvas.create_oval(360, 190, 390, 220, fill="white")
canvas.create_oval(410, 190, 440, 220, fill="white")

# Draw the character's mouth
canvas.create_arc(370, 250, 430, 290, start=190, extent=160, fill="black", width=2)

# Draw the weapon
canvas.create_line(500, 310, 560, 270, fill="gray", width=5)
canvas.create_line(560, 270, 620, 320, fill="gray", width=5)
canvas.create_line(620, 320, 570, 360, fill="gray", width=5)

# Run the main loop
window.mainloop()