from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, TypeVar

from attrs import define as _attrs_define
from attrs import field as _attrs_field

if TYPE_CHECKING:
    from ..models.extension_source_file_data import ExtensionSourceFileData

T = TypeVar("T", bound="OpenSourceFileEvent")

@_attrs_define
class OpenSourceFileEvent:
    """
    Attributes:
        source_file (Optional[ExtensionSourceFileData]):
    """

    source_file: Optional["ExtensionSourceFileData"]
    additional_properties: Dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        source_file = self.source_file.to_dict() if self.source_file else None

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "sourceFile": source_file,
            }
        )

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        from ..models.extension_source_file_data import ExtensionSourceFileData

        d = src_dict.copy()
        _source_file = d.pop("sourceFile")
        source_file: Optional[ExtensionSourceFileData]
        if _source_file is None:
            source_file = None
        else:
            source_file = ExtensionSourceFileData.from_dict(_source_file)

        open_source_file_event = cls(
            source_file=source_file,
        )

        open_source_file_event.additional_properties = d
        return open_source_file_event

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


import argparse
import datasets
from transformers import BertTokenizer
from bs4 import BeautifulSoup

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset', type=str, required=True)
    parser.add_argument('--tokenizer', type=str, required=True)
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    dataset = datasets.load_from_disk(args.dataset)
    tokenizer = BertTokenizer.from_pretrained(args.tokenizer)
    print('===================================================')
    print(dataset)
    print('train INFO')
    print(dataset['train'].info)
    print('test INFO')
    print(dataset['test'].info)

    print(f"len = {len(dataset['train'])}")

    print('===================================================')
    for part in ['train', 'test']:
        print('PART', part)
        for i, x in enumerate(dataset[part]):
            # print(x['labels'], sum(1 for c in x['input_ids'] if int(c) > int(0)))
            # print(x['excess_entropy'], x['text'])
            # print(f'{len(x["text"])}, {x["tse"]}')
            print(x)
            if i >= 50:
                break


# Uses python3
import sys

comp_speed = 'fast'

input = sys.stdin.read()
input_list = input.split('\n')
n = int(input_list[0])
a = [int(x) for x in input_list[1].split()]

assert(len(a) == n)

if comp_speed == 'slow':
    result = 0
    for i in range(0, n):
        for j in range(i+1, n):
            if a[i]*a[j] > result:
                result = a[i]*a[j]
    print(result)

if comp_speed == 'fast':
    max_index1 = -1;

    for i in range(0, n):
        if ((max_index1 == -1) or (a[i] > a[max_index1])):
            max_index1 = i;
    max_index2 = -1;
    for j in range(0, n):
        if ((j != max_index1) and ((max_index2 == -1) or (a[j] > a[max_index2]))):
            max_index2 = j;
    result = a[max_index1] * a[max_index2]
    print(result)


"""Load lens artifacts from the hub or locally storage."""
import os
from pathlib import Path
from typing import Optional

from huggingface_hub import hf_hub_download

def load_lens_artifacts(
    resource_id: str,
    repo_id: Optional[str] = None,
    repo_type: Optional[str] = None,
    revision: Optional[str] = None,
    config_file: str = "config.json",
    ckpt_file: str = "params.pt",
    subfolder: str = "lens",
    cache_dir: Optional[str] = None,
) -> tuple[Path, Path]:
    """First checks for lens resource locally then tries to download it from the hub.

    Args:
        resource_id: The id of the lens resource.
        repo_id: The repository to download the lens from. Defaults to
            'AlignmentResearch/tuned-lens'. However, this default can be overridden by
            setting the TUNED_LENS_REPO_ID environment variable.
        repo_type: The type of repository to download the lens from. Defaults to
            'space'. However, this default can be overridden by setting the
            TUNED_LENS_REPO_TYPE environment variable.
        config_file: The name of the config file in the folder contain the lens.
        ckpt_file: The name of the checkpoint file in the folder contain the lens.
        revision: The revision of the lens to download.
        subfolder: The subfolder of the repository to download the lens from.
        cache_dir: The directory to cache the lens in.

    Returns:
        * The path to the config.json file
        * The path to the params.pt file

    Raises:
        ValueError: if the lens resource could not be found.
    """
    if repo_id is None:
        if os.environ.get("TUNED_LENS_REPO_ID"):
            repo_id = os.environ["TUNED_LENS_REPO_ID"]
        else:
            repo_id = "AlignmentResearch/tuned-lens"

    if repo_type is None:
        if os.environ.get("TUNED_LENS_REPO_TYPE"):
            repo_type = os.environ["TUNED_LENS_REPO_TYPE"]
        else:
            repo_type = "space"

    # Fist check if the resource id is a path to a folder that exists
    local_path = Path(resource_id)
    if (local_path / config_file).exists() and (local_path / ckpt_file).exists():
        return local_path / config_file, local_path / ckpt_file

    subfolder = "/".join((subfolder, resource_id))
    params_path = hf_hub_download(
        filename=ckpt_file,
        repo_id=repo_id,
        repo_type=repo_type,
        revision=revision,
        subfolder=subfolder,
        cache_dir=cache_dir,
    )

    config_path = hf_hub_download(
        filename=config_file,
        repo_id=repo_id,
        repo_type=repo_type,
        revision=revision,
        subfolder=subfolder,
        cache_dir=cache_dir,
    )

    if config_path is not None and params_path is not None:
        return Path(config_path), Path(params_path)

    raise ValueError("Could not find lens resource locally or on the hf hub.")


import numpy as np
import math

neglambAndBeta = np.array([[2.167,-0.083,0],[-0.085,2.167,-0.035],[0,-0.0835,2.167]],dtype=float)
firstbound = np.array([[6.2625],[6.2625],[14.6125]])
secondbound = np.array([[0],[0],[8.35]],dtype=float)
thirdbound = np.array([[4.175],[4.175],[12.525]],dtype=float)
finallist = np.linalg.solve(neglambAndBeta,firstbound)
print(finallist)
secondgo = finallist+secondbound
print(secondgo)
finallist = np.linalg.solve(neglambAndBeta,secondgo)
print(finallist)

booksolutions1 = np.array([[3.01597],[3.2708],[6.8692]],dtype=float)
booksolutions2 = np.array([[0.1274],[0.2900],[4.1291]],dtype=float)
booksolutions3 = np.array([[2.0181],[2.2477],[6.0256]],dtype=float)
# print(np.dot(neglambAndBeta,booksolutions1))
# print(np.dot(neglambAndBeta,booksolutions2))
# print(np.dot(neglambAndBeta,booksolutions3))

newbound1 = np.dot(neglambAndBeta,booksolutions1)/0.0835
newbound2 = np.dot(neglambAndBeta,booksolutions2)/0.0835
newbound3 = np.dot(neglambAndBeta,booksolutions3)/0.0835
# print(newbound1)
print(newbound2)
# print(newbound3)
# temp = np.reshape(neglambAndBeta,(-1,1))


cube = lambda x:x*x*x # complete the lambda function

def fibonacci(n):
    # return a list of fibonacci numbers

    if n>1:
        l_fb=[0,1]
        for i in range(n-2):

                l_fb.append(l_fb[i]+l_fb[i+1])
    elif n==0:
            l_fb=[]
    elif n==1:
            l_fb=[0]

    return l_fb


# This is just cheating lol

import numpy as np
import cv2
from PIL import Image

import torch
import torch.nn as nn
import torchvision.transforms as transforms

from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from torch.utils.data.sampler import Sampler
from torch.autograd import Variable

import glob # Why even bother doing shit yourself

class TiDataSet(Dataset):
    DATA_DIR = '../images/4/' # here we assume that in that folder there are downsampled pngs of size (28x28) of the ti dataset

    def __init__(self, transform=None, data_dir=None):
        data_dir = self.DATA_DIR if data_dir is None else data_dir

        names = glob.glob(data_dir + "*.png") # name expansion holy shit

        #dummy image loader
        N = len(names)
        images = np.zeros((N,28,28,1),   dtype=np.uint8) # how do you python?
        labels = np.zeros((N),           dtype=np.int64) # somewhere if make this 32 bit shit goes bananas

        for n in range(N):
            images[n] = cv2.imread(names[n], 0).reshape((28,28,1)) #Yes we have 1 channel, better make it explicit or .ToTensor will freak the heck out
            # images[n] = Image.open(names[n]).convert('L')
            # images[n].reshape((28,28,1))
            labels[n] = int(names[n].split('/')[-1][0:2])-1 # poof
            #print(labels[n])

        self.transform = transform
        self.images    = images  #torch.from_numpy(images)
        self.labels    = labels  #torch.from_numpy(labels)

        print(len(self.images), len(self.labels))

    def __getitem__(self, index):
        # print ('\tcalling Dataset:__getitem__ @ idx=%d'%index)
        img   = self.images[index]
        label = self.labels[index]

        if self.transform is not None:
            img = self.transform(img)

        return img, label

    def __len__(self):
        # print ('\tcalling Dataset:__len__')
        return len(self.images)

dataset = TiDataSet()
print("Hi!")

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

# Hyper Parameters
num_epochs = 10
batch_size = 1
learning_rate = 0.100

train_dataset = TiDataSet(transform=transforms.ToTensor()) # .ToTensor lol this is just to ez
test_dataset  = TiDataSet(transform=transforms.ToTensor())

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset    = train_dataset,
                                           batch_size = batch_size,
                                           shuffle    = False)

test_loader  = torch.utils.data.DataLoader(dataset    = test_dataset,
                                           batch_size = batch_size,
                                           shuffle    = False)

# CNN Model (2 conv layer)
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.fc = nn.Linear(7*7*32, 10)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out

cnn = CNN()

print("Network Structure:")
print(cnn)
print(train_loader)

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)

# Train the Model
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = Variable(images)
        labels = Variable(labels)

        # Forward + Backward + Optimize
        optimizer.zero_grad()
        outputs = cnn(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        if (i+1) % 100 == 0:
            print ('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
                   %(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))

# Test the Model
cnn.eval()  # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0
total = 0
for images, labels in test_loader:
    images = Variable(images)
    outputs = cnn(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum()

print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))

# Save the Trained Model
torch.save(cnn.state_dict(), 'cnn.pkl')


#This program will convert one datatype to the other

num1=99.23
num2=23
num3=150
string1="100"

#Converting num1 into an integer
num1_int=int(num1)

#converting num2 into a float
num2_float=float(num2)

#converting num3 into a string
num3_str=str(num3)

#converting string1 into an integer
string1_int=int(string1)

print (num1_int)
print (num2_float)
print (num3_str)
print (string1_int)


# Tc and Sc: O(n)
class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        dic = {}
        n = len(numbers)

        for i in range(n):
            diff = target - numbers[i]

            if diff in dic:
                return [dic[diff], i+1]

            dic[numbers[i]] = i + 1

NeetCode/Two Pointer/167. Two Sum II - Input Array Is Sorted/sol.py


#导入库
import json
import numpy as np
import json
import cv2
import matplotlib.pyplot as plt

path = 'img/6.json' #json文件路径
with open(path,'r') as f:
    data = json.load(f)
#print(data)
#print(data['bbox'])
# print(data['bbox'][0][0])
#data中每个元素都是嵌套的字典
img = cv2.imread('img/6.jpg') #读入图像
copyimg=img.copy()#拷贝一份，不拷贝容易出错
list = []
list = list + data['bbox']
for i in range(len(list)):
    pts = np.array([[list[i][0],list[i][1]],[list[i][2],list[i][3]],[list[i][4],list[i][5]],[list[i][6],list[i][7]]], np.int32)
    pts = pts.reshape((-1,1,2))
    cv2.polylines(copyimg,[pts],True,(0,0,255))
# 图片保存
#参数1是路径，要包括保存的名字和格式，参数2是来源图片
cv2.imwrite('img/q.jpg',copyimg)


import codecs
import csv
import os
import re
import sys
import urllib.request
import logging
from typing import Type, Tuple

from baseParser import BaseParser
from monstatparser import MonStatParser
from property import Property
from property import PropertyParse
from skills import SkillParser
from utils import isdebugging, getFileInsensitive
from urllib.error import URLError

logger = logging.getLogger(__name__)

getSkillId = (lambda par, skills: [skill for skill in skills if skill.id == par])
getSkillSkill = (lambda par, skills: [skill for skill in skills if skill.skill.lower() == par.lower()])
getMonster = (lambda x, monsters: [mon for mon in monsters if mon.id == x.par])
class Parser:
	skills: [SkillParser] = []
	definedProperties: [PropertyParse] = []
	monsters: [MonStatParser] = []
	version: str = ""

	def __init__(self, modName: str, version: str):
		self.modName = modName
		self.version = version
		self.skills = self.read(SkillParser)
		self.definedProperties = self.read(PropertyParse)
		self.monsters = self.read(MonStatParser)

	def read(self, cls: Type[BaseParser], dl=False) -> [BaseParser]:
		return BaseParserCreator.read(cls, self, self.modName, self.version, dl)

	def findInCommon(self, property: Property) -> str:
		classes = {}
		skillBeg = int(property.min)
		skillEnd = int(property.max)
		for skillI in range(skillBeg, skillEnd):
			sk = self.getSk(str(skillI))[0]
			if sk.char not in classes and len(classes) == 0:
				classes[sk.char] = True
			elif sk.char in classes and len(classes) == 1:
				pass
			else:
				# TODO
				return "Random Level {} skill ({} possibilities)".format(property.par, skillEnd-skillBeg)
		cls = list(classes.keys())[0]
		return "Random Level {} {} Skill".format(property.par, self.charAbbrvToFull(cls))

	def charAbbrvToFull(self, abbv: str) -> str:
		if abbv == "pal":
			return "Paladin"
		elif abbv == "ass":
			return "Assassin"
		elif abbv == "sor":
			return "Sorceress"
		elif abbv == "ama":
			return "Amazon"
		elif abbv == "dru":
			return "Druid"
		elif abbv == "bar":
			return "Barbarian"
		elif abbv == "nec":
			return "Necromancer"
		else:
			logger.error("Unknown char abbv %s" % abbv)
			sys.exit()

	def getSk(self, skillId) -> SkillParser:
		skill = getSkillId(skillId, self.skills)
		if not skill:
			skill = getSkillSkill(skillId, self.skills)
		if not skill:
			logger.error("Unable to get skill for property {}".format(skillId))
			sys.exit()
		return skill

	def parseSkillOnEvent(self, property) -> str:
		global skills
		if property.prop == "skill-rand":
			return self.findInCommon(property)

		skill = self.getSk(property.par)

		if property.prop == "skill":
			if(len(skill)):
				skill = skill[0]
				#TODO
				return "+{} to {} ({} Only)".format(generateRange(property), skill.skill, self.charAbbrvToFull(skill.char))
			else:
				return "+{} to {} (Class Only)".format(generateRange(property), property.par)
		elif property.prop == "att-skill":
			return "{}% Chance To Cast Level {} {} On Attack".format(property.min, property.max, property.par)
		elif property.prop == "oskill":
			if len(skill):
				skill = skill[0]
			return "+{} to {}".format(generateRange(property), skill.skill)
			#return "Level {} {} ({} Charges)".format(int(int(property.min)/3), skill.skill, property.max)
		elif property.prop == "kill-skill":
			when = "Kill An Enemy"
		elif property.prop == "levelup-skill":
			when = "Level-Up"
		elif property.prop == "death-skill":
			when = "Die"
		else:
			logger.error("Unknown skill property {}".format(property.prop))
			sys.exit()
		return "{}% Chance To Cast Level {} {} When you {}".format(property.min, property.max, property.par, when)

	def parseReanimate(self, property) -> str:
		if property.prop == "reanimate":
			monster = getMonster(property, self.monsters)[0]
			return "{} Reanimate As: {}".format(generateRange(property, "%"), monster)
		else:
			logger.error("Unknown reanimate {}".format(property.prop))
			sys.exit()

	def parseCharges(self, property) -> str:
		skills = self.getSk(property.par)
		if(len(skills) != 1):
			logger.error("Got %s skills" % len(skills))
			sys.exit()
		else:
			return "Level %s %s (%s Charges)" % (property.max, skills[0].skill, property.min)

	def validateProperty(self, property: Property) -> str:
		x = property
		matched = 0
		if x.prop.startswith("*"):
			if isdebugging():
				logger.info("Got star prop " + x.prop)
			x.prop = x.prop[1:]
		for prop in self.definedProperties:
			if prop.code == x.prop.lower():
				matched += 1
		if matched != 1:
			logger.error("Property {} is not valid".format(property.prop))
			sys.exit()

	def parse(self, properties: [Property]):
		poisonDmgProps: [Tuple[Property, int]] = []
		coldDmgProps: [Tuple[Property, int]] = []
		for k, property in enumerate(properties):
			parsed = None
			minMax = re.compile("[A-Za-z]+\-(min|max)")
			dmg = re.compile("dmg\-[a-z]+")
			pierce = re.compile('pierce\-[A-Za-z]+')
			res = re.compile('res\-[A-Za-z]+')
			resLen = re.compile('res\-[A-Za-z]+\-len')
			abs = re.compile('abs\-[A-Za-z]+')
			red = re.compile('red\-[A-Za-z]+')
			extra = re.compile('extra\-[A-Za-z]+')
			star = re.compile('\*.+')
			dot = re.compile("(pois|cold)-len")
			self.validateProperty(property)
			if property.prop is None:
				logger.error("cant")
			elif property.prop == "dmg":
				parsed = "{} Damage".format(generateRange(property))
			elif property.prop == "dmg%":
				parsed = "{} Enhanced Damage".format(generateRange(property, "%"))
			elif property.prop == "dmg%/lvl":
				parsed = "{} Enhanced Damage (Based on Character Level)".format(generateRange(property, "%"))
			elif property.prop == "stamdrain":
				parsed = "%s Slower stamina drain" % generateRange(property, "%")
			elif property.prop == "stam":
				parsed = "+%s to Maximum Stamina" % generateRange(property)
			elif property.prop == "stam/lvl":
				property.mult(1/8)
				parsed = "+%s to Maximum Stamina (Based on Character Level)" % generateRange(property)
			elif property.prop == "regen-stam":
				parsed ="Heal Stamina Plus %s" % generateRange(property, "%")
			elif property.prop == "regen-stam/lvl":
				property.mult(1/8)
				parsed = "Heal Stamina Plus %s (Based on Character Level)" % generateRange(property)
			elif re.match(dot, property.prop):
				property.mult(1/25)
				if property.prop.startswith("cold"):
					coldDmgProps.append((property, k))
				elif property.prop.startswith("pois"):
					poisonDmgProps.append((property, k))
				parsed = "This should not be seen"
			elif re.match(star, property.prop):

				logger.error("Got star property: {}".format(property.prop))
			elif re.match(red, property.prop):
				ret = "{}Damage Reduced By {}"
				if property.prop == "red-mag":
					type = "Magic "
				else:
					type = ""
				parsed = ret.format(type, generateRange(property))
			elif re.match(minMax, property.prop):
				type=""
				if(property.prop.startswith("cold")):
					coldDmgProps.append((property, k))
				elif(property.prop.startswith("pois")):
					poisonDmgProps.append((property, k))
				elif(property.prop.endswith("max")):
					type = "Maximum"
				elif(property.prop.endswith("min")):
					type = "Minimum"
				parsed = "+{} To {} Damage".format(generateRange(property), type)
			elif re.match(abs, property.prop):
				type = tagToString(property.prop)
				parsed = "{} Absorb +{}".format(type, generateRange(property, "%"))
			elif re.match(resLen, property.prop):
				if property.prop == "res-pois-len":
					parsed = "Poison Length Reduced by {}".format(generateRange(property, "%"))
				else:
					logger.error("Unknown reduce length {}".format(property.prop))
					sys.exit()
			elif re.match(res, property.prop):
				if property.prop == "res-all":
					parsed = "All Resistances +{}".format(generateRange(property))
				else:
					type = ""
					if property.prop == "res-all-max":
						type = "Maximum Poison/Fire/Cold/Lightning"
					elif "max" in property.prop:
						type = "Maximum "
					else:
						type += tagToString(property.prop)
					parsed = "{} Resistance +{}".format(type, generateRange(property, "%"))
					if "/lvl" in property.prop:
						parsed += " (Based on Character Level)"
			elif re.match(pierce, property.prop):
				type = tagToString(property.prop)
				parsed = "-{} To Enemy {} Resistance".format(generateRange(property, "%"), type)
			elif re.match(dmg, property.prop):
				if(property.prop == "dmg-undead"):
					parsed = "+{} Damage To Undead".format(generateRange(property, "%"))
				elif(property.prop == "dmg-und/lvl"):
					parsed = "+{} Damage To Undead (Based on Character Level)".format(generateRange(property, "%"))
				elif(property.prop == "dmg-demon"):
					parsed = "+{} Damage To Demons".format(generateRange(property, "%"))
				elif(property.prop == "dmg-dem/lvl"):
					parsed = "+{} Damage To Demons (Based On Character Level)".format(generateRange(property, "%"))
				elif property.prop == "dmg-to-mana":
					parsed = "{} Damage Taken Goes to Mana".format(generateRange(property, "%"))
				else:
					if (property.prop == "dmg-ac"):
						parsed = "{} To Monster Defense Per Hit".format(generateRange(property))
					elif property.prop == "dmg-elem":
						parsed = "+{} Fire/Cold/Lightning Damage".format(generateRange(property))
					else:
						ret = "Adds " + generateRange(property) + " "
						if (property.prop == "dmg-ltng"):
							ret += "Lightning"
						elif (property.prop == "dmg-cold"):
							ret += "Cold"
						elif (property.prop == "dmg-fire"):
							ret += "Fire"
						elif (property.prop == "dmg-pois"):
							ret += "Poison"
						elif (property.prop == "dmg-mag"):
							ret += "Magic"
						elif (property.prop == "dmg-norm"):
							pass
						else:
							logger.error("Unknown add damage: {}".format(property.prop))
							sys.exit()
						parsed = ret  + " Damage"
			elif re.match(extra, property.prop):
				if property.prop == "extra-pois":
					type = "Poison"
				elif property.prop == "extra-cold":
					type = "Cold"
				elif property.prop == "extra-fire":
					type = "Fire"
				elif property.prop == "extra-ltng":
					type = "Lightning"
				parsed = "+{} To {} Skill Damage".format(generateRange(property), type)

			elif property.prop in ["skill", "kill-skill", "levelup-skill", "death-skill", "att-skill", "oskill", "skill-rand"]:
				parsed = self.parseSkillOnEvent(property)
			elif property.prop == "randclassskill":
				# TODO
				parsed = "+3 To All Character Skills (Varies)"
			elif property.prop == "reanimate":
				self.parseReanimate(property)
			elif property.prop == "fireskill":
				parsed = "+{} To Fire Skills".format(generateRange(property))
			elif property.prop == "skilltab":
				# TODO
				parsed = "+{} To Skill Tab {}".format(generateRange(property), property.par)
			elif property.prop in ["sor", 'nec', 'bar', 'pal', 'ass', 'dru', 'ama']:
				parsed = "+%s To All %s Skills" % (generateRange(property), self.charAbbrvToFull(property.prop))
			elif property.prop == "charged":
				parsed = self.parseCharges(property)
			elif property.prop == "res-all":
				parsed = "All Resistances +{}".format(generateRange(property))
			elif property.prop == "aura":
				parsed = "Level {} {} Aura When Equipped".format(generateRange(property), property.par)
			elif property.prop in ["swing1","swing2","swing3"]:
				parsed = "{} Increased Attack Speed".format(generateRange(property, "%"))
			elif property.prop == "all-stats":
				parsed = "+{} To All Attributes".format(generateRange(property))
			elif property.prop.lower() == "light":
				parsed = "+{} To Light Radius".format(generateRange(property))
			elif property.prop == "stupidity":
				parsed = "Hit Blinds Target"
			elif property.prop == "allskills":
				parsed = "+{} To All Skills".format(generateRange(property))
			elif property.prop == "":
				parsed = ""
			elif property.prop == "balance1":
				parsed = "+{} Faster Hit Recovery".format(generateRange(property, "%"))
			elif property.prop == "balance2":
				parsed = "+{} Faster Hit Recovery".format(generateRange(property, "%"))
			elif property.prop == "balance3":
				parsed = "+{} Faster Hit Recovery".format(generateRange(property, "%"))
			elif property.prop == "hit-skill":
				parsed = "{}% Chance To Cast Level {} {} On Striking".format(min, max, property.par)
			elif property.prop == "gethit-skill":
				parsed = "{}% Chance To Cast Level {} {} When Struck".format(min, max, property.par)
			elif property.prop == "ethereal":
				parsed = "Ethereal"
			elif property.prop == "indestruct":
				parsed = "Indestructible"
			elif property.prop == "dur":
				parsed = "+%s Increased Durability" % generateRange(property)
			elif property.prop == "rep-dur":
				parsed = "Repairs Durability {} in 4 Seconds".format(property.par)
			elif property.prop == "knock":
				parsed = "Knockback"
			elif property.prop == "mana/lvl":
				val = int(property.par) / 8
				parsed = "+{}-{} To Mana (Based On Character Level)".format(val, val * 111)
			elif property.prop == "regen-mana":
				parsed = "Regenerate Mana {}".format(generateRange(property, "%"))
			elif property.prop == "mana%":
				parsed = "Increase Maximum Mana {}".format(generateRange(property, "%"))
			elif property.prop in ["cast1", "cast2", "cast3"]:
				parsed = "{} Faster Cast Rate".format(generateRange(property, "%"))
			elif property.prop == "hp" or property.prop == "mana":
				if property.prop == "hp":
					type = "Life"
				else:
					type = "Mana"
				parsed = "+{} To {}".format(generateRange(property), type)
			elif property.prop == "hp/lvl":
				parsed = "+{} To Life (Based on Character Level)".format(generateRange(property))
			elif property.prop == "explosivearrow":
				parsed = "FIres Explosive Arrows or Bolts ({})".format(generateRange(property))
			elif property.prop == "cheap":
				parsed = "Reduces All Vendor Prices {}".format(generateRange(property, "%"))
			elif property.prop == "mag%":
				parsed = "{} Better Chance of Getting Magic Items".format(generateRange(property, "%"))
			elif property.prop == "mag%/lvl":
				parsed = "+{} Better Chance of Getting Magical Items (Based On Character Level)".format(
					generateRange(property))
			elif property.prop == "red-dmg%":
				parsed = "Damage Reduced By {}".format(generateRange(property, "%"))
			elif property.prop == "str":
				parsed = "+{} to Strength".format(generateRange(property))
			elif property.prop == "str/lvl":
				parsed = "+{} To Strength (Based On Character Level)".format(property)
			elif property.prop == "dex":
				parsed = "+{} to Dexterity".format(generateRange(property))
			elif property.prop == "dex/lvl":
				parsed = "+{} To Dexterity (Based On Character Level)".format(property)
			elif property.prop == "enr":
				parsed = "+{} to Energy".format(generateRange(property))
			elif property.prop == "enr/lvl":
				parsed = "+{} To Energy (Based On Character Level)".format(property)
			elif property.prop == "vit":
				parsed = "+{} to Vitality".format(generateRange(property))
			elif property.prop == "vit/lvl":
				parsed = "vit/lvl"
			elif property.prop == "slow":
				parsed = "Slows Target By {}".format(generateRange(property, "%"))
			elif property.prop == "ac":
				parsed = "+{} Defense".format(generateRange(property))
			elif property.prop == "ac/lvl":
				if(property.min == "" or property.max == ""):
					property.min = property.par
					property.max = property.par
				property.min=str(int(int(property.min)/8))
				property.max=str(int(int(property.max)/8))
				parsed = "+{} To Defense (Based On Character Level)".format(generateRange(property))
			elif property.prop == "ac%":
				parsed = "+{} Enhanced Defense".format(generateRange(property, "%"))
			elif property.prop == "ac-miss":
				parsed = "+{} Defense Vs. Missle".format(generateRange(property))
			elif property.prop == "ignore-ac":
				parsed = "Ignores Target's Defense"
			elif property.prop == "reduce-ac":
				parsed = "-{} Target Defense".format(generateRange(property, "%"))
			elif property.prop == "hp%":
				parsed = "Increase Maximum Life {}".format(generateRange(property, "%"))
			elif property.prop == "att%":
				parsed = "{} Bonus To Attack Rating".format(generateRange(property, "%"))
			elif property.prop == "att":
				parsed = "+{} To Attack Rating".format(generateRange(property))
			elif property.prop == "att-undead":
				parsed = "+{} To Attack Rating Against Undead".format(generateRange(property))
			elif property.prop == "att-und/lvl":
				parsed = "+{} To Attack Rating Against Undead (Based on Character Level)".format(
					generateRange(property))
			elif property.prop == "att-demon":
				parsed = "+{} Damage to Demons".format(generateRange(property, "%"))
			elif property.prop == "att-dem/lvl":
				parsed = "+{} Damage to Demons (Based on Character Level)".format(generateRange(property, "%"))
			elif property.prop == "demon-heal":
				parsed = "+{} Life After Each Demon Kill".format(generateRange(property))
			elif property.prop == "noheal":
				parsed = "Prevent Monster Heal"
			elif property.prop == "heal-kill":
				parsed = "+{} Life After Each Kill".format(generateRange(property))
			elif property.prop == "ease":
				parsed = "Requirements {}".format(generateRange(property, "%"))
			elif property.prop == "nofreeze":
				parsed = "Cannot be Frozen"
			elif property.prop == "freeze":
				parsed = "Freezes Target +{}".format(generateRange(property))
			elif property.prop == "half-freeze":
				parsed = "Half Freeze Duration"
			elif property.prop == "block1":
				parsed = "+{} Faster Block Rate".format(generateRange(property, "%"))
			elif property.prop == "block2":
				parsed = "+{} Faster Block Rate".format(generateRange(property, "%"))
			elif property.prop == "block3":
				parsed = "+{} Faster Block Rate".format(generateRange(property, "%"))
			elif property.prop == "block":
				parsed = "{} Increased Chance of Blocking".format(generateRange(property, "%"))
			elif property.prop == "howl":
				parsed = "Hit Causes Monster to Flee {}".format(generateRange(property, "%"))
			elif property.prop == "gold%":
				parsed = "{} Extra Gold From Monsters".format(generateRange(property, "%"))
			elif property.prop == "gold%/lvl":
				parsed = "{} Extra Gold From Monsters (Based on Character Level)".format(generateRange(property, "%"))
			elif property.prop == "manasteal":
				parsed = "{} Mana Stolen Per Hit".format(generateRange(property, "%"))
			elif property.prop == "lifesteal":
				parsed = "{} Life Stolen Per Hit".format(generateRange(property, "%"))
			elif property.prop == "deadly":
				parsed = "{} Deadly Strike".format(generateRange(property, "%"))
			elif property.prop == "deadly/lvl":
				parsed = "+{} Deadly Strike (Based on Character Level)".format(generateRange(property))
			elif property.prop == "regen":
				parsed = "Replenish Life {}".format(generateRange(property))
			elif property.prop == "regen-mana":
				parsed = "Regenerate Mana {}".format(generateRange(property, "%"))
			elif property.prop == "mana-kill":
				parsed = "+{} to Mana After Each Kill".format(generateRange(property))
			elif property.prop == "openwounds":
				parsed = "{} Chance of Open Wounds".format(generateRange(property, "%"))
			elif property.prop == "crush":
				parsed = "{} Chance of Crushing Blow".format(generateRange(property, "%"))
			elif property.prop == "light-thorns":
				parsed = "Attacker Takes Lightning Damage of %s" % generateRange(property)
			elif property.prop == "thorns":
				parsed = "Attacker Takes Damage of {}".format(generateRange(property))
			elif property.prop == "thorns/lvl":
				property.mult(1/8)
				parsed = "Attacker Takes Damage of %s (Based on Character Level)" % generateRange(property)
			elif property.prop == "rip":
				parsed = "Slain Monsters Rest in Peace"
			elif property.prop == "move2":
				parsed = "{} Faster Run Walk".format(generateRange(property, "%"))
			elif property.prop == "move3":
				parsed = "{} Faster Run Walk".format(generateRange(property, "%"))
			elif property.prop == "att/lvl":
				property.mult(.5)
				parsed = "{} to Attack Rating (Based on Chracter Level)".format(generateRange(property))
			elif property.prop == "dmg/lvl":
				property.mult(1/8)
				parsed = "{} to Maximum Damage (Based on Character Level)".format(generateRange(property))
			elif property.prop == "addxp":
				parsed = "{} to Experience Gained".format(generateRange(property, "%"))
			elif property.prop == "rep-quant":
				y = 100/int(property.par)
				z = y/int(property.par)
				property.mult(z)
				parsed = "Replenishes Quantity (1 in {} sec)".format(generateRange(property))
			elif property.prop  == "state":
				# TODO
				parsed = "Monster Set {} TODO".format(property.par.split("-")[-1])
			elif property.prop == "pierce":
				# TODO
				parsed = "Attacks Pierce target TODO"
			elif property.prop == "stack":
				parsed = "Increased stack size {}".format(generateRange(property))
			elif property.prop == "splash":
				# TODO
				if property.par == "proc_SplashDamage":
					parsed = "Attacks Spalsh TODO"
			elif property.prop == "magicarrow":
				parsed = "Fires Level {} Magic Arrows/Bolts".format(generateRange(property))
			elif property.prop == "ac-hth":
				parsed = "+{} Defense vs. Melee".format(generateRange(property))
			elif property.prop == "sock":
				parsed = "Socketed (%s)" % generateRange(property)
			elif property.prop == "bloody":
				# TODO not visible
				parsed = "Extra Blood {}".format(generateRange(property))
			else:
				logger.error("Unable to parse " + str(property))
				sys.exit()
			if parsed == "":
				logger.error("Property {} did not get parsed!".format(property.prop))
				sys.exit()
			property.parsed = parsed
			properties[k] = property

		byType = (lambda typ, props: [x for x in props if typ in x[0].prop])
		validProp = re.compile("(pois|cold)-(len|min|max)")
		def mergeDmgProps(props: [Property], typ: str):
			if len(props) == 3 and len([x for x in props if re.match(validProp, x[0].prop)]) == 3:
				lenProp = byType("len", props)[0]
				minProp = byType("min", props)[0]
				maxProp = byType("max", props)[0]
				properties[lenProp[1]].parsed = "Adds {}-{} {} Damage, Duration: {} seconds".format(
					generateRange(minProp[0]), generateRange(maxProp[0]), typ, generateRange(lenProp[0]))
				properties.remove(minProp[0])
				properties.remove(maxProp[0])
			elif len(props) != 0:
				logger.error("Mismatched number of %s properties %s" % (typ, len(props)))

		mergeDmgProps(poisonDmgProps, "Poison")
		mergeDmgProps(coldDmgProps, "Cold")

		return properties

def tagToString(prop) -> str:
	if "ltng" in prop:
		return "Lightning"
	elif "cold" in prop:
		return "Cold"
	elif "fire" in prop:
		return "Fire"
	elif "pois" in prop:
		return "Poison"
	elif "mag" in prop:
		return "Magic"
	else:
		logger.error("Unknown prop " + prop)
		sys.exit()

def generateRange(prop, append=""):
	if prop.min == "" and prop.max == "" and prop.par != "":
		return "{}{}".format(prop.par, append)
	else:
		if prop.min == prop.max:
			return prop.min + append
		return "{}-{}{}".format(prop.min, prop.max, append)

class BaseParserCreator:
	@staticmethod
	def read(cls: Type[BaseParser], parser: Parser, modName: str, version: str, dl=False) -> [BaseParser]:
		items = []
		dir = os.path.join(os.getcwd(), "versions", modName, version)
		path = os.path.join(dir, getFileInsensitive(dir, cls.getName()+".txt"))
		try:
			with codecs.open(path, 'r', encoding='utf-8', errors='ignore') as file:
				reader = csv.DictReader(file, delimiter='\t')
				for line in reader:
					item = cls(dict(line))
					item.parse(parser)
					if item.verify():
						item.setMod(modName, version)
						items.append(item)
						if dl:
							BaseParserCreator.Download(item)
					else:
						try:
							logger.debug("bad [%s] (%s) %s" % (cls.getName(), item.name, item))
						except:
							logger.debug("bad [%s] %s" % (cls.getName(), str(item)))
		except FileNotFoundError:
			logger.error("Unable to find %s" % path)
			sys.exit()

		return items

	@staticmethod
	def Download(cls: Type[BaseParser]):
		import socket
		dfto = socket.getdefaulttimeout()
		socket.setdefaulttimeout(1)
		name = cls.fileName()
		url = "http://classic.battle.net/images/battle/diablo2exp/images/{}".format(name) + "/{}.gif"
		gemName = cls.Name().replace(" ", "")
		nurl = url.format(gemName)
		try:
			try:
				os.mkdir(os.path.join('Images', name))
			except:
				pass
			imgName = os.path.join("Images", name, cls.FileName())
			urllib.request.urlretrieve(nurl, imgName)
			cls.img = imgName
		except URLError as e:
			logger.error("timedout getting %s" % nurl)
		except Exception:
			logger.error("Unable to parse {} url {}".format(gemName, nurl))
		socket.setdefaulttimeout(dfto)


import numpy as np
import sys, os
from matplotlib.pylab import *
from mpl_toolkits.axes_grid1 import host_subplot
import matplotlib.animation as animation

#####################################################
#Objective of this is to learn how to tune my model & params
#####################################################

#Generate Sin Cos wave for 2 dimensional data
#Augment data at least 1000 samples - 1)slight change in freq, 2)phase shift, 3)magnitude
t = np.linspace(0.0, np.pi*2.0, 100)
n = 1000

X = []
for i in xrange(n):
    if i<1:
        x1 = np.cos(t) + np.random.normal(-0.3, 0.3, np.shape(t) ) #random.normal(mu, sig, )
        x2 = np.sin(t) + np.random.normal(-0.3, 0.3, np.shape(t) )
    elif i<100:
        x1 = np.cos(t) + np.random.normal(-0.2, 0.2, np.shape(t) )
        x2 = np.sin(t) + np.random.normal(-0.2, 0.2, np.shape(t) )
    elif i<200:
        x1 = np.cos(t) + np.random.normal(-0.1, 0.1, np.shape(t) )
        x2 = np.sin(t) + np.random.normal(-0.1, 0.1, np.shape(t) )
    elif i<300:
        x1 = np.cos(1.1*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
        x2 = np.sin(1.1*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
    elif i<400:
        x1 = np.cos(0.8*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
        x2 = np.sin(0.8*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
    elif i<500:
        x1 = np.cos(1.1*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
        x2 = np.sin(1.1*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
    elif i<600:
        x1 = 1.2*np.cos(t) + np.random.normal(-0.2, 0.2, np.shape(t) )
        x2 = 1.2*np.sin(t) + np.random.normal(-0.2, 0.2, np.shape(t) )
    elif i<700:
        x1 = 1.1*np.cos(0.9*t) + np.random.normal(-0.1, 0.1, np.shape(t) )
        x2 = 1.1*np.sin(0.9*t) + np.random.normal(-0.1, 0.1, np.shape(t) )
    elif i<800:
        x1 = 0.8*np.cos(0.9*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
        x2 = 0.8*np.sin(0.9*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
    elif i<900:
        x1 = np.cos(0.9*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
        x2 = np.sin(0.9*t) + np.random.normal(-0.3, 0.3, np.shape(t) )
    elif i<1000:
        x1 = 1.1*np.cos(0.9*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
        x2 = 1.1*np.sin(0.9*t) + np.random.normal(-0.2, 0.2, np.shape(t) )
    X.append( np.vstack([ x1.reshape(1,len(t)), x2.reshape(1,len(t)) ]) )
X = np.swapaxes(X, 0,1)

# #Plot
# font = {'size'   : 9}
# matplotlib.rc('font', **font)
# f0 = figure(num = 0, figsize = (12, 8))#, dpi = 100)
# f0.suptitle("ARtag & Audio combined Prediction", fontsize=12)
# ax01 = subplot2grid((4, 2), (0, 0))
# ax02 = subplot2grid((4, 2), (1, 0))
# ax03 = subplot2grid((4, 2), (2, 0))
# ax04 = subplot2grid((4, 2), (3, 0))
# ax05 = subplot2grid((4, 2), (0, 1))
# ax06 = subplot2grid((4, 2), (1, 1))
# ax07 = subplot2grid((4, 2), (2, 1))
# ax08 = subplot2grid((4, 2), (3, 1))
# #X[0]=cos, X[1]=sin
# ax01.grid(True)
# ax01.plot(t, X[0][101])
# ax02.grid(True)
# ax02.plot(t, X[0][901])
# ax05.grid(True)
# ax05.plot(t, X[1][0])
# ax06.grid(True)
# ax06.plot(t, X[1][1])
# plt.show()

#Generate Data like I do - Window method
# print 'a'
# print X.shape
# window_size = 5

# dX, dY = [], []
# for i in range(n - window_size + 1):
#     dX.append(X[i : i+window_size])
#     dY.append(X[i+window_size : i+window_size+1][0])

# #window over entire dataset - mfcc
# mfccs = np.rollaxis(mfccs, 1, 0)
# aX, aY = self.construct_dataset(mfccs)
# audio_dataX.append(aX)
# audio_dataY.append(aY)
# #window over entire dataset - xyz
# iX, iY = self.construct_dataset(relative_position_intp)
# image_dataX.append(iX)
# image_dataY.append(iY)

# #Below here should be outside the loop
# audio_dataX = np.array(audio_dataX)
# audio_dataY = np.array(audio_dataY)
# image_dataX = np.array(image_dataX)
# image_dataY = np.array(image_dataY)

# #concatenate for number of experiment samples
# audio_dataX2 = audio_dataX[0]
# audio_dataY2 = audio_dataY[0]
# image_dataX2 = image_dataX[0]
# image_dataY2 = image_dataY[0]
# for i in range(1, audio_dataX.shape[0]):
#     audio_dataX2 = np.concatenate((audio_dataX2, audio_dataX[i]), axis=0)
#     audio_dataY2 = np.concatenate((audio_dataY2, audio_dataY[i]), axis=0)
#     image_dataX2 = np.concatenate((image_dataX2, image_dataX[i]), axis=0)
#     image_dataY2 = np.concatenate((image_dataY2, image_dataY[i]), axis=0)
# audio_dataX = audio_dataX2
# audio_dataY = audio_dataY2
# image_dataX = image_dataX2
# image_dataY = image_dataY2

#Use LSTM to train/predict based on predict, try as much tuning as possible

#Plot final result

#Optional
#See what I get using SVM


"""
Name: Thomas Scola
hw4.py

Problem: Use graphics to solve the homework

Certification of Authenticity:
I certify that this assignment is entirely my own work.
"""
from math import sqrt, pi
from graphics import Point, GraphWin, Circle, Text, Rectangle

def squares():
    # Creates a graphical window
    width = 400
    height = 400
    win = GraphWin("square", width, height)

    # number of times user can move square
    num_clicks = 5

    # create a space to instruct user
    inst_pt = Point(width / 2, height - 10)
    instructions = Text(inst_pt, "Click to move square")
    instructions.draw(win)

    # builds a square
    shape = Rectangle(Point(100, 100), Point(150, 150))
    shape.setOutline("red")
    shape.setFill("red")
    shape.draw(win)

    # allows the user to click multiple times to move the circle
    for i in range(num_clicks):
        click = win.getMouse()
        center = shape.getCenter()  # center of circle

        # move amount is distance from center of circle to the
        # point where the user clicked
        change_x = click.getX() - center.getX()
        change_y = click.getY() - center.getY()
        new_shape = shape.clone()
        shape.move(change_x, change_y)
        new_shape.draw(win)
    instructions.undraw()
    closing_message = Text(inst_pt, "Click to close")
    closing_message.draw(win)
    win.getMouse()
    win.close()

def rectangle():
    win = GraphWin("Rectangle", 500, 500)
    win.setCoords(0, 0, 10, 10)

    message = Text(Point(5,1), "Click twice to draw a new rectangle")
    message.draw(win)

    point_one = win.getMouse()
    point_one.draw(win)
    point_two = win.getMouse()
    point_two.draw(win)
    pone_x = point_one.getX()
    ptwo_x = point_two.getX()
    pone_y = point_one.getY()
    ptwo_y = point_two.getY()

    rectangle_made = Rectangle(Point(pone_x, pone_y), Point(ptwo_x, ptwo_y))
    rectangle_made.setFill("green")
    rectangle_made.draw(win)

    perimeter_textbox = Text(Point(5, 3), "Perimeter: ")
    area_textbox = Text(Point(5, 2.5), "Area: ")
    perimeter_textbox.draw(win)
    area_textbox.draw(win)

    length = abs(point_two.getY() - point_one.getY())
    width = abs(point_two.getX() - point_one.getY())
    area = length * width
    perimeter = 2 * area

    perimeter_value = Text(Point(6.5, 3), perimeter.__round__(3))
    area_value = Text(Point(6, 2.5), area.__round__(3))
    perimeter_value.draw(win)
    area_value.draw(win)

    message.setText("Click to close")
    win.getMouse()
    win.close()

def circle():
    win = GraphWin("Double Click circle", 600, 600)

    center = win.getMouse()
    point = win.getMouse()

    dX = point.getX() - center.getX()
    dY = point.getY() - center.getY()

    radius = sqrt(dX **2 + dY **2)
    circle = Circle(center, radius)

    circle.setFill("light blue")
    circle.draw(win)

    radius_text_box = Text(Point(300, 550), "Radius: ")
    radius_text_box.draw(win)

    radius_value = Text(Point(350, 550), radius.__round__(3))
    radius_value.draw(win)

    win.getMouse()
    win.close()

def pi2():
    summing_terms = eval(input("Enter the number of terms to sum: "))
    pi_approx = 0
    denom = 1
    sign_of_term = 1

    for i in range(summing_terms):
        pi_approx = pi_approx + (sign_of_term * (4 / denom))
        denom = denom + 2
        sign_of_term = sign_of_term * -1

    accuracy = abs(pi - pi_approx)

    print("The pi approximation ", pi_approx)
    print("The accuracy is ", accuracy)

if __name__ == '__main__':
    pass


import clr
from collections import OrderedDict
from datetime import datetime
import os
import re
import sys
from typing import Dict, Optional, Tuple

from connect import *
from connect.connect_cpython import PyScriptObject  # For type hints

clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import MessageBox

# Directory to save the TXT file in
# This directory does not have to exist
OUTPUT_DIR = os.path.join('T:', os.sep, 'Physics', 'Scripts', 'Output Files', 'Generate Shifts Comments')

def fmt_num(num: float, nearest_x: float) -> str:
    """Gets a nicely formatted string version of the number

    Rounds the number to the nearest `nearest_x`
    Removes any trailing zeroes. If nothing remians after the decimal point, removes it also

    Arguments
    ---------
    num: The number to format
    nearest_x: The interval to round to

    Returns
    -------
    The string that is the formatted number

    Example
    -------
    fmt_num(1.61, 0.5) -> '1.5'
    """
    num = round(num / nearest_x) * nearest_x
    # Don't count the "0." at the beginning
    num_places = len(str(nearest_x)) - 2
    num = format(num, '.{}f'.format(num_places))
    # Remove trailing zeroes. If there was nothing but zeroes after the decinal point, remove it, too
    num = num.rstrip('0').rstrip('.')
    return num

def setup_beam_ssd(setup_beam: PyScriptObject, regex: str) -> Optional[float]:
    """Gets the SSD of the setup beam if any of the following are true:
    - Its name or description contains the given regular expression (case insensitive)
    - The regular expression is "AP" (case insensitive) and the beam's gantry angle is zero
    - The regular expression is "PA" (case insensitive) and the beam's gantry angle is 180

    The matched substring must not be surrounded by alphabetic characters

    Arguments
    ---------
    setup_beam: The setup beam whose SSD to return
    regex: The regular expression to match a substring in the setup beam's name or description

    Returns
    -------
    The setup beam's SSD, if the setup beam matches, or None otherwise or if the SSD is infinite
    """
    full_regex = r'(^|[^a-zA-Z])' + regex + r'($|[^a-zA-Z])'  # Matching substring must not be immediately preceded or followed by an alphabetic character
    if (re.search(full_regex, setup_beam.Name, re.IGNORECASE) is not None or re.search(full_regex, setup_beam.Description, re.IGNORECASE) is not None) or (regex == 'AP' and setup_beam.GantryAngle == 0) or (regex == 'PA' and setup_beam.GantryAngle == 180):
        setup_beam_ssd = setup_beam.GetSSD()
        if abs(setup_beam_ssd) != float('inf'):  # Infinite SSD
             return setup_beam_ssd

def cb_setup_beams(beam_set: PyScriptObject, pos: str) -> Tuple[Dict[PyScriptObject, float], Dict[PyScriptObject, float]]:
    """Gets the cone-beam (CB) setup beams for a beam set

    A setup beam is considered CB if its name or description contains "CB" or "CBCT", or "AP" (for supine patients) or "PA" (for prone patients)
    Only CB setup beams with SSD are returned

    Arguments
    ---------
    beam_set: The beam set whose CB setup beams to return

    Returns
    -------
    A 2-tuple of dictionaries with setup beams as the keys and SSDs as the values.
    The first element is "CB" setup beams, the second "AP"/"PA" setup beams.
    """
    cbs, aps = OrderedDict(), OrderedDict()

    # Check each setup beam to see if it is CB
    for setup_beam in beam_set.PatientSetup.SetupBeams:
        # Is it "CB"?
        ssd = setup_beam_ssd(setup_beam, r'CB(CT)?')
        if ssd is None:
            # Not "CB", so is it "AP"/"PA"?
            ssd = setup_beam_ssd(setup_beam, pos)
            if ssd is not None:
                aps[setup_beam] = ssd
        else:
            cbs[setup_beam] = ssd
    return cbs, aps

def output_filepath(beam_set: PyScriptObject) -> str:
    """Creates an output filename for the shifts TXT file for the beam set

    TXT file is in `OUTPUT_DIR`
    Filename is in the format "<beam set ID> Shifts Comments YYYY-MM-DD HH:MM:SS.txt"
    The colon in the beam set ID is replaced with a hyphen for Windows filename compatibility

    Arguments
    ---------
    beam_set: The beam set to return an output filepath for

    Returns
    -------
    The absolute filepath of the TXT file to create

    Example
    -------
    output_filepath(some_beam_set) -> 'My\Output\Dir\plan name-beam set name 2022-05-04 16_15_30.txt'
    """
    dt = datetime.now().strftime('%Y-%m-%d %H_%M_%S')
    bs_id = '-'.join(beam_set.BeamSetIdentifier().split(':'))
    filename = bs_id + ' Shifts Comments ' + dt + '.txt'
    filename = re.sub(r'[<>:"/\\\|\?\*]', '_', filename)
    return os.path.join(OUTPUT_DIR, filename)

def generate_shifts_comments(include_msq_shifts: Optional[bool] = False) -> None:
    """Creates and opens a TXT file listing patient shifts (couch shifts, MOSAIQ Site Setup shifts) and CB/AP/PA setup SSD if applicable.
    The MOSAIQ Setup Shifts are the same as the couch setup shifts but using the opposite directional term (e.g., "Posterior" if the couch shift is "Anterior").

    If there is no shift, writes "No setup shifts" instead of the couch shift.
    If there is a single CB setup beam, uses it. Otherwise, if there is a single AP (for supine) / PA (for prone) setup beam, uses it. Otherwise, alerts the user and exits the script.

    Assumes that CB setup beam name or description contains "CB" or "CBCT" (case insensitive) and that other AP/PA setup beam names or descriptions contain "AP" or "PA" (case insensitive).

    Arguments
    include_msq_shifts: True to include the MOSAIQ Setup Shifts, False otherwise.
                        Defaults to False.
    """
    # Get current beam set
    try:
        beam_set = get_current('BeamSet')
    except:
        MessageBox.Show('There is no beam set loaded. Click OK to abort the script.', 'No Beam Set Loaded')
        sys.exit()

    # Ensure beam set has beam(s)
    if beam_set.Beams.Count == 0:
        MessageBox('There are no beams in the current beam set.\nClick OK to abort the script.', 'No Beams')
        sys.exit()

    # Localization geometry
    loc_geom = beam_set.GetStructureSet().LocalizationPoiGeometry
    if loc_geom.Point is None or loc_geom.Point.x == float('inf'):
        MessageBox.Show('There is no localization geometry on the planning exam.\nClick OK to abort the script.', 'No Localization Geometry')
        sys.exit()
    loc_geom = loc_geom.Point

    # Beam set isocenter
    isos = [beam.Isocenter.Position for beam in beam_set.Beams]
    # Ensure all beam isos have same coordinates
    if len(set((iso.x, iso.y, iso.z) for iso in isos)) > 1:
        MessageBox.Show('Beams in beam set have different isocenter coordinates.\nClick OK to abort the script.', 'Beam Isos Differ')
        sys.exit()
    iso = isos[0]  # All isos have same position, so doesn't matter which one we use

    # Calculate shifts = localization geometry minus isocenter
    x, y, z = (loc_geom[dim] - iso[dim]
               for dim in 'xyz')

    # ----------------------------- Shift directions ----------------------------- #

    # "Left" or "Right"
    if x < 0:
        couch_r_l = 'Right'
        msq_r_l = 'Left'
    else:
        couch_r_l = 'Left'
        msq_r_l = 'Right'

    # "Superior" or "Inferior"
    if z < 0:
        couch_i_s = 'Inferior'
        msq_i_s = 'Superior'
    else:
        couch_i_s = 'Superior'
        msq_i_s = 'Inferior'

    # "Posterior" or "Anterior"
    if y < 0:
        couch_p_a = 'Anterior'
        msq_p_a = 'Posterior'
    else:
        couch_p_a = 'Posterior'
        msq_p_a = 'Anterior'

    # Round and format shifts
    x, y, z = (fmt_num(abs(shift), 0.1) for shift in (x, y, z))

    # Add couch shifts to comments
    comments = '1. Align to initial CT marks\n'
    if x == y == z == '0':
        comments += 'No setup shifts'
    else:
        comments += f'2. Shift couch so PATIENT is moved: {couch_r_l} {x} cm (patient\'s right/left), {couch_i_s} {z} cm, {couch_p_a} {y} cm'

    # CB setup SSD
    pos = 'AP' if 'Supine' in beam_set.PatientPosition else 'PA'
    cbs, aps = cb_setup_beams(beam_set, pos)  # All CB or AP/PA (depending on pt position) setup beams
    ssd = None
    if len(cbs) > 1:  # Multiple CB setup beams
        if len(aps) > 1:  # Multiple AP/PA setup beams
            MessageBox.Show('There are multiple CB and multiple ' + pos + ' setup beams, and the script doesn\'t know which one to use. Click OK to abort the script.', 'Multiple CB/' + pos + ' Setup Beams')
            sys.exit()
        elif not aps:  # No AP/PA setup beams
            MessageBox.Show('There are multiple CB setup beams and no other ' + pos + ' setup beams, and the script doesn\'t know which one to use. Click OK to abort the script.', 'Multiple CB Setup Beams')
            sys.exit()
        else:  # Exactly 1 AP/PA setup beam
            MessageBox.Show('There are multiple CB and one other ' + pos + ' setup beam, and the script doesn\'t know which one to use. Click OK to abort the script.', 'Multiple CB/' + pos + ' Setup Beams')
            sys.exit()
    elif cbs:  # Exactly 1 CB setup beam, so use it
        ssd = list(cbs.values())[0]
    elif aps:  # At least one AP/PA setup beam
        if len(aps) > 1:  # Multiple AP/PA setup beams
            MessageBox.Show('There are multiple ' + pos + ' setup beams and no other CB setup beams, and the script doesn\'t know which one to use. Click OK to abort the script.', 'Multiple ' + pos + ' Setup Beams')
            sys.exit()
        else:  # Exactly 1 AP/PA setup beam, so use it
            ssd = list(aps.values())[0]
    else:  # No CB or other AP/PA setup beams
        MessageBox.Show('There are no CB or other ' + pos + ' setup beams. Click OK to abort the script.', 'No CB/' + pos + ' Setup Beams')
        sys.exit()
    comments += '\n' + pos + ' setup SSD: ' + fmt_num(ssd, 0.25)

    # Add MOSAIQ site setup shifts to comments
    if include_msq_shifts:
        comments += '\n\nMOSAIQ Site Setup shifts:\n' \
                 + f'{msq_r_l} {x} cm, {msq_i_s} {z} cm, {msq_p_a} {y} cm'

    # Write comments to file
    if not os.path.isdir(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)
    filepath = output_filepath(beam_set)
    with open(filepath, 'w') as f:
        f.write(comments)

    # Open comments file
    os.system('START /B notepad.exe "' + filepath + '"')


from __future__ import print_function
import numpy as np
import h5py
import hdf5plugin
import Daisy

class SaveHDF5(Daisy.Base.DaisyAlg):

    def __init__(self, name):
        super().__init__(name)
        self.cfg        = {}

    def initialize(self, outputfile_name = None, default_init = None):
        self.data   = self.get("DataStore").data()
        self.LogInfo("initialized")

    def config(self, cfgdata=None):
        if cfgdata is None:
            self.LogError('Please provide configuration file!')
        Daisy.CfgParser(self, cfgdata, self.cfg)
        self.foname = self.cfg['outputfile_name']
        return True

    def execute(self, input_dataobj, output_path=''):
        idata  = self.data[input_dataobj]
        dtype  = type(idata)
        #print('type of ouput data: '+str(dtype))
        with h5py.File(self.foname, 'w') as hfile:
            hfile.create_dataset(output_path, data=idata)
            self.LogInfo('Save data '+input_dataobj+' as '+output_path+' to '+self.foname)
       # if type(output_path) is str:
       #     items = output_path.split('/')
       #     for item in items:
       #         if len(item) == 0:
       #             continue
       #         if item in handle.keys():
       #             handle = handle[item]
       #         else:
       #             handle = handle.create_group(item)
       #         print('enter path: '+ item)
       #
       # if dtype is dict:
       #     for key in idata.keys():
       #         if type(key) is str:
       #             self.LogInfo(key+" is stored in file "+self.foname)
       #             self.LogInfo(str(idata[key]))
       #             handle.create_dataset(key, data=idata[key])
       #         else:
       #             self.LogError("The keys of input data should be class <'str'> ")
       #             return True
       # else:
       #     print('================='+output_path)
       #     handle.create_dataset(output_path, data=idata)
       #

        return True

    def finalize(self):
        #self.hdf5.close()
        self.LogInfo("finalized, close HDF5 File: "+self.foname)
        return True


import csv
import io
import json
import os
import pickle
import platform
import subprocess

import emot
# import en_core_web_sm
import gensim
import matplotlib.pyplot as plt
import numpy as np
from gensim.models import KeyedVectors
from numba import jit
from pyspark import SparkContext, SparkConf
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from tqdm import tqdm
from whoosh.analysis import StemmingAnalyzer, StandardAnalyzer
from krovetzstemmer import Stemmer

# choose correct variable values according to what pc I am using
# if platform.node() == 'acquario':
#     OS_PYTHON = r'/home/alberto/anaconda3/envs/tensorflow/bin/python3'
#     TREC_EVAL_PATH = 'trec_eval.8.1/trec_eval'
# elif platform.node() == 'DESKTOP-LKU0VTG':
#     OS_PYTHON = r'C:\Users\apirp\anaconda\envs\tf\python.exe'
#     TREC_EVAL_PATH = r'trec_eval-master\trec_eval.exe'
# elif platform.node() == 'hopper':
#     OS_PYTHON = '/home/ims/anaconda3/envs/tensorflow_env/bin/python3'
#     TREC_EVAL_PATH = 'trec_eval.8.1/trec_eval'
# elif platform.node() == 'alberto-Alienware-15-R4':
#     OS_PYTHON = r'/home/alberto/anaconda3/envs/tf/bin/python3'
#     TREC_EVAL_PATH = 'trec_eval.8.1/trec_eval'
# else:
TREC_EVAL_PATH = 'trec_eval.8.1/trec_eval'
OS_PYTHON = r'/home/alberto/anaconda3/envs/tf/bin/python3'

def create_summary_chart(losses, maps, suff=''):
    plt.switch_backend('agg')
    fig, (ax1, ax2) = plt.subplots(2, 1)
    fig.subplots_adjust(hspace=0.5)

    ax1.plot(losses, c='b', label='loss')
    ax1.legend()
    ax2.plot(maps, c='r', label='map_score')
    ax2.legend()
    plt.savefig('analyses/maps_and_losses_' + suff + '.png')
    plt.close()
    return

def find_first_prime_number_after_k(k):
    if k < 10000000019:
        return 10000000019
    print('finding valid prime number...')
    candidate = k
    while True:
        candidate += 1
        is_prime = True
        print('evaluating candidate=%s' % candidate)
        for i in tqdm(range(2, candidate)):
            if (i % i) == 0:
                is_prime = False
                break
        if is_prime:
            return candidate

def get_hash(x, a, b, p, m):
    """
    a must be in [1, (p-1)]
    b must be in [1, (p-1)]
    p is a prime number that is greater than max possible value of x
    h(x, a, b) = ((ax + b) mod p)mod m
    m is a max possible value you want for hash code + 1
    """
    if a >= p or b >= p:
        print('error in input params of hash function')
        return None
    return ((a * x + b) % p) % m

def read_files_from_folder(input_folder):
    files = []
    fnames = []
    for filename in os.listdir(input_folder):
        if filename.startswith('.') or os.path.isdir(os.path.join(input_folder, filename)):
            continue
        with open(os.path.join(input_folder, filename), 'r', encoding='latin-1') as f:  # Reading file
            files.append(f.read())
            fnames.append(filename)
    return files, fnames

def hash_docs(word_dict, encoded_docs, repr_size, n_hash_functions):
    hashed_docs = []
    dict_size = len(word_dict)
    p = find_first_prime_number_after_k(dict_size)
    print('hashing docs...')
    for d in tqdm(encoded_docs):
        hashed_d = np.zeros(repr_size)
        for w in d:
            for b in range(n_hash_functions):
                i = get_hash(w, 2, b, p, repr_size)
                hashed_d[i] = hashed_d[i] + 1
        hashed_docs.append(hashed_d)
    return np.array(hashed_docs)

def read_lines_data_file(fpath, separator='\t', encoding='latin-1'):
    docs_text = []
    labels_text = []

    for data in read_tsv_file(fpath, delimiter=separator, encoding=encoding):
        if len(data[1].strip()) == 0:
            continue
        labels_text.append(data[1].strip())
        docs_text.append(data[2].strip())
    return docs_text, labels_text

def read_tsv_file(path, delimiter='\t', headers=False, encoding='latin-1'):
    rows = []
    if headers:
        cnt = 0
    else:
        cnt = 1
    with io.open(path, 'r', encoding=encoding, errors='ignore') as f:
        for line in csv.reader((x.replace('\0', '') for x in f), delimiter=delimiter):
            if cnt > 0:
                rows.append(line)
            else:
                cnt += 1
    return rows

def save_json(model, output_path):
    with open(output_path, 'w') as outfile:
        json.dump(model, outfile)

def load_json(path):
    with open(path, 'r') as json_file:
        return json.load(json_file)

def save_model(model, output_path):
    with open(output_path, 'wb') as handle:
        pickle.dump(model, handle)
        handle.close()

def load_model(path):
    model = pickle.load(open(path, 'rb'))
    return model

def run_trec_eval(trec_eval_path=TREC_EVAL_PATH,
                  qrels_file='data/cran/processed_corpus/cranfield.qrel',
                  run_to_eval='/Users/albertopurpura/PycharmProjects/ml4ir_git/results/re_ranking_output_lmnn.txt'):
    print('using the qrels file: %s' % qrels_file)
    if platform.node() == 'acquario' or platform.node() == 'hopper' or platform.node() == 'alberto-Alienware-15-R4':
        command = os.path.join(os.getcwd(), trec_eval_path) + ' ' \
                  + os.path.join(os.getcwd(), qrels_file) + ' ' \
                  + os.path.join(os.getcwd(), run_to_eval) + ' | grep "^map" '
    else:
        command = os.path.join(os.getcwd(), trec_eval_path) + ' -m map ' \
                  + os.path.join(os.getcwd(), qrels_file) + ' ' \
                  + os.path.join(os.getcwd(), run_to_eval)
    print(command)
    (map_line, err) = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).communicate()
    map_line = map_line.decode("utf-8")
    map_value = map_line.split('\t')[2]
    return float(map_value)

def chunk(l, size):
    chnks = []
    for i in range(0, len(l), size):
        chnks.append(l[i: min(i + size, len(l))])
    return chnks

def get_embeddings_matrix(word_index, embs_path='models/GoogleNews-vectors-negative300.bin', emb_size=300):
    vocab_size = len(word_index.keys())
    model = KeyedVectors.load_word2vec_format(embs_path, binary=True)
    embeddings = np.zeros((vocab_size + 1, emb_size))

    for k, v in word_index.items():
        if k in model.wv.vocab:
            embeddings[v] = model[k]
        else:
            embeddings[v] = np.zeros(emb_size)  # unk words are mapped to all zero vector

    return np.array(embeddings, dtype=np.float32), vocab_size, emb_size

def split_doc_in_frames(collection, max_sentence_size=20, max_sentences_per_doc=None):
    fixed_sizes_collections = []
    for i in tqdm(range(len(collection))):
        d = collection[i]
        # a document is a list of sentences
        max_sentence_size_in_words = max_sentence_size
        frames = chunk(d, max_sentence_size_in_words)
        if max_sentences_per_doc is not None:
            frames = frames[:max_sentences_per_doc]
        fixed_sizes_collections.append(frames)
    return fixed_sizes_collections

@jit
def encode_sequence(sequence, word_dict):
    encoded_sequence = []
    for w in sequence:
        word = w.text
        if word in word_dict.keys():
            encoded_sequence.append([word_dict[word]])
    return encoded_sequence

def load_indri_stopwords():
    fpath = './data/indri_stoplist_eng.txt'
    sws = []
    for line in open(fpath, 'r'):
        sws.append(line.strip())
    return sws

def contains_digits(token):
    for c in token:
        if c.isdigit():
            return True
    return False

def encode_docs_split_in_sentences(docs_split_in_sentences, word_dict):
    encoded_docs = []
    for doc in tqdm(docs_split_in_sentences):
        encoded_doc = []
        for sentence in doc:
            encoded_sentence = []
            for w in sentence:
                if w in word_dict.keys():
                    encoded_sentence.append(word_dict[w])
            if len(encoded_sentence) > 0:
                encoded_doc.append(encoded_sentence)
        encoded_docs.append(encoded_doc)
    return encoded_docs

# def split_in_sents_then_encode(docs, dictionary):
#     nlp = en_core_web_sm.load()
#     # split documents in sentences
#     docs_split_in_sentences = []
#     for d in tqdm(docs):
#         d = d.replace('\n', ' ')
#         d = d.replace('  ', ' ')
#         d = d.strip()
#         doc = nlp(d)
#         docs_split_in_sentences.append([s.text for s in list(doc.sents)])
#
#     # encoded documents
#     encoded_docs = []
#     for doc in docs_split_in_sentences:
#         encoded_doc = []
#         for sent in doc:
#             encoded_sent = []
#             tokens = nlp(sent)
#             for t in tokens:
#                 token = t.text
#                 if token in dictionary.keys():
#                     encoded_sent.append(dictionary[token])
#             if len(encoded_sent) > 0:
#                 encoded_doc.append(encoded_sent)
#         encoded_docs.append(encoded_doc)
#
#     return encoded_docs

def stem_tokens(tokens):
    for i in range(len(tokens)):
        tokens[i] = kstemmer.stem(tokens[i])
    tokens = [t for t in tokens if len(t) > 2]
    return tokens

def extract_emojis_emoticons(text):
    extracted = []
    vals = emot.emoticons(text)
    if len(vals) > 1:
        extracted.extend(vals['value'])

    vals = emot.emoji(text)
    if len(vals) > 1:
        extracted.extend(vals['value'])
    return extracted

def encode_spark(docs, dictionary, stopwords=None):
    os.environ['PYSPARK_PYTHON'] = OS_PYTHON
    conf = SparkConf().setMaster('local[*]').set('spark.driver.memory', '10g').set('spark.executor.memory', '10g')
    sc = SparkContext(conf=conf)
    encoded_docs = sc.parallelize(docs).map(lambda d: encode_doc(d, dictionary, stopwords)).collect()
    sc.stop()
    return encoded_docs

def rerank_docs_spark(q, docs_to_rerank, we):
    os.environ['PYSPARK_PYTHON'] = OS_PYTHON
    conf = SparkConf().setMaster('local[*]').set('spark.driver.memory', '10g').set('spark.executor.memory', '10g')
    sc = SparkContext(conf=conf)
    sims = sc.parallelize(docs_to_rerank).map(lambda d: compute_q_d_sim(q, d, we)).collect()
    sc.stop()
    return sims

def compute_q_d_sim(q, d, we):
    exact_match_sim = np.sum([1 for w in d if w in q]) / len(d)
    sim = np.sum(cosine_similarity([q], [we[word] for word in d])) / len(d)
    return sim + exact_match_sim

def encode_doc(d, dictionary, stopwords):
    if stopwords is not None:
        stemmed = tokenize_preprocess_document_standard(d, stopwords)
    else:
        stemmed = tokenize_preprocess_document_standard(d)
    new_d = []
    for t in stemmed:
        if dictionary.get(t) is not None:
            new_d.append(dictionary[t])
    return new_d

def encode(docs, dictionary):
    encoded_docs = []
    for d in tqdm(docs):
        tokenized = tokenize(d)
        new_d = []
        for t in tokenized:
            if dictionary.get(t) is not None:
                new_d.append(dictionary[t])
        encoded_docs.append(new_d)
    return encoded_docs

def encode_collection(docs, max_n_words=None, min_df=1, max_df=1.0):
    print('computing terms frequencies...')
    vectorizer = TfidfVectorizer(tokenizer=tokenize, max_df=max_df, min_df=min_df, max_features=max_n_words)
    vectorizer.fit(docs)
    features_list = vectorizer.get_feature_names()
    dictionary = {}
    for w in features_list:
        dictionary[w] = len(dictionary)
    print('dictionary size=' + str(len(dictionary.keys())))
    print('turning text to sequences...')
    encoded_docs = []
    for d in tqdm(docs):
        tokenized = tokenize(d)
        new_d = []
        for t in tokenized:
            if dictionary.get(t) is not None:
                new_d.append(dictionary[t])
        encoded_docs.append(new_d)
    return np.array(encoded_docs), dictionary

def compute_sim(d, q, sim_scores_index):
    if len(d) == 0:
        doc_score = 0
    else:
        neural_sim_score = [sim_scores_index[t] for t in d]
        sim_score = np.mean(neural_sim_score)
        exact_match_score = np.sum([1 for w in q if w in d]) / len(q)  # add exact match bonus term
        doc_score = sim_score + exact_match_score
    return doc_score

def compute_doc_similarity_spark(documents, sim_scores_index, q):
    os.environ['PYSPARK_PYTHON'] = OS_PYTHON
    conf = SparkConf().setMaster('local[*]').set('spark.driver.memory', '10g').set('spark.executor.memory', '10g')
    sc = SparkContext(conf=conf)
    sims = sc.parallelize(documents).map(lambda doc: compute_sim(doc, q, sim_scores_index)).collect()
    sc.stop()
    return sims

def load_w2v_model(word_dictionary):
    model = gensim.models.KeyedVectors.load_word2vec_format('./models/GoogleNews-vectors-negative300.bin', binary=True)
    emb_matrix = []
    word_dict_new = {}
    for k, v in word_dictionary.items():
        if k in model.wv.vocab:
            emb_matrix.append(model.wv[k])
            word_dict_new[k] = len(word_dict_new)
    emb_matrix.append(np.zeros(300))  # for padding values
    return np.array(emb_matrix), word_dict_new

def tokenize(text, stemming=True, stoplist=None):
    text = text.lower()
    text = text.strip()
    if stemming:
        analyzer = StemmingAnalyzer(stoplist=stoplist, minsize=2, stemfn=kstemmer.stem)
    else:
        analyzer = StandardAnalyzer(stoplist=stoplist, minsize=2)

    tokens = [token.text for token in analyzer(text)]
    tokens = [word for word in tokens if not contains_digits(word)]
    return tokens

def tokenize_preprocess_document_standard(doc, stoplist=None):
    # doc = doc.lower()
    # doc = doc.strip()
    # tokenized_doc = doc.split(' ')
    # if stoplist is not None:
    #     tokenized_doc = [w for w in tokenized_doc if w not in sw]
    # tokenized_doc = [word for word in tokenized_doc if not contains_digits(word)]
    # tokenized_doc = stem_tokens(tokenized_doc)
    return tokenize(doc, stoplist)

def train_w2v_model(documents):
    emb_size = 300
    print('tokenizing and stemming collection')
    tokenized_stemmed_docs = []
    for d in tqdm(documents):
        tokenized_stemmed_docs.append(tokenize_preprocess_document_standard(d))

    print('training word embeddings')
    model = gensim.models.Word2Vec(tokenized_stemmed_docs, negative=10, size=emb_size, window=10, min_count=10,
                                   workers=12, sample=1e-4, sg=0)
    model.train(tokenized_stemmed_docs, total_examples=model.corpus_count, epochs=10)
    emb_matrix = []
    word_dict = {}
    for k in model.wv.vocab:
        word_dict[k] = len(word_dict)
        emb_matrix.append(model.wv[k])
    emb_matrix.append(np.zeros(emb_size))  # for padding values
    return np.array(emb_matrix), word_dict

def train_w2v_gensim(documents, word_dictionary):
    print('Training word embeddings')
    emb_size = 300
    docs = [tokenize(d, stemming=True) for d in documents]
    model = gensim.models.Word2Vec(docs, negative=10, size=emb_size, window=10, min_count=10, workers=12, sample=1e-4,
                                   sg=0)
    model.train(docs, total_examples=model.corpus_count, epochs=10)
    emb_matrix = []
    word_dict_new = {}
    for k, v in word_dictionary.items():
        if k in model.wv.vocab:
            emb_matrix.append(model.wv[k])
            word_dict_new[k] = len(word_dict_new)
    emb_matrix.append(np.zeros(emb_size))  # for padding values
    return np.array(emb_matrix), word_dict_new

def split_doc_in_sequences(d, max_seq_len):
    sequences = []
    s = []
    for w in d:
        s.append(w)
        if len(s) == max_seq_len:
            sequences.append(s)
            s = []
    sequences.append(s)
    return sequences

def pad_documents(docs_batch, max_seq_len, padding_value):
    docs_split_in_seqs = []
    for d in docs_batch:
        docs_split_in_seqs.append(split_doc_in_sequences(d, max_seq_len))
    true_seqs_lengths = []
    for d in docs_split_in_seqs:
        true_seqs_lengths.append([len(s) for s in d])

    # pad each sequence
    for d in docs_split_in_seqs:
        new_d = []
        for s in d:
            while len(s) < max_seq_len:
                s.append(padding_value)
            new_d.append(s)
    # add n
    max_n_sequences_in_doc = max([len(d) for d in docs_split_in_seqs])
    for i in range(len(docs_split_in_seqs)):
        d = docs_split_in_seqs[i]
        sequences_lengths = true_seqs_lengths[i]
        while len(d) < max_n_sequences_in_doc:
            d.append([padding_value] * max_seq_len)
            sequences_lengths.append(0)
        docs_split_in_seqs[i] = d
        true_seqs_lengths[i] = sequences_lengths

    return docs_split_in_seqs, true_seqs_lengths

def pad_sequences(sequences, padding_value, ml=-1):
    if ml == -1:
        ml = max([len(s) for s in sequences])
    new_list = np.ones(shape=(len(sequences), ml)) * padding_value
    for i in range(new_list.shape[0]):
        for j in range(len(sequences[i])):
            new_list[i, j] = sequences[i][j]
    return new_list


import pygame
import random
import os
import time
import os

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

pygame.init()

# screen settings
screen_width = 870
screen_height = 970
screen = pygame.display.set_mode((screen_width, screen_height))

# imgaes setting
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, "images")

background = pygame.image.load(os.path.join(image_path, "background.png"))

# character
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_mask = pygame.mask.from_surface(character)
character_x_pos = 20
character_y_pos = 220
to_x = 0
to_y = 0

# Tiles and colors
tile_images =[
    pygame.image.load(os.path.join(image_path, "tile1.png")),
    pygame.image.load(os.path.join(image_path, "tile2.png")),
    pygame.image.load(os.path.join(image_path, "tile3.png")),
    pygame.image.load(os.path.join(image_path, "tile4.png")),
    pygame.image.load(os.path.join(image_path, "tile5.png")),
    pygame.image.load(os.path.join(image_path, "tile6.png")),
    pygame.image.load(os.path.join(image_path, "tile7.png"))
]

colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Purple"]

hidetile = pygame.image.load(os.path.join(image_path, "hidetile.png"))

tiles = []

pygame.display.set_caption("Mark Game")

# Font
game_font = pygame.font.Font(None, 40)

# Tracking variables
memorize = True
find = False
answer = False
correct = False
level = 1
memorize_time = 15
find_time = 10
check_time = 5
start_ticks = pygame.time.get_ticks()
elapsed_time = 0
action = ""
timer = ""
target_color = -1
difficulty = 0
color_number = 3
difficulty_changed = False
win = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if (not answer) and event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                character_x_pos -= 110
            elif event.key == pygame.K_RIGHT:
                character_x_pos += 110
            elif event.key == pygame.K_UP:
                character_y_pos -= 110
            elif event.key == pygame.K_DOWN:
                character_y_pos += 110

        if (not answer) and event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0

    if character_x_pos < 20:
        character_x_pos = 20
    elif character_x_pos > 790:
        character_x_pos = 790

    if character_y_pos < 220:
        character_y_pos = 220
    elif character_y_pos > 880:
        character_y_pos = 880

    # Change Difficulty
    if level % 3 == 0 and not difficulty_changed:
        difficulty += 1
        memorize_time = 15
        if color_number < 7:
            color_number += 1
        difficulty_changed = True

    if len(tiles) == 0:
        for i in range(0, 56):
            col = i % 8
            row = i // 8
            tiles.append({
                "img": tile_images[random.randrange(0, color_number)],
                "x_pos": col * 110,
                "y_pos": 200 + (row * 110)
            })

    # draw screen

    # Background
    screen.blit(background, (0, 0))

    # timer
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    if memorize:
        if (memorize_time - elapsed_time) <= 0 :
            memorize = False
            find = True
            target_color = random.randrange(0, color_number)
            start_ticks = pygame.time.get_ticks()
    elif find:
        if (find_time - elapsed_time) <= 0:
            find = False
            answer = True
            start_ticks = pygame.time.get_ticks()
    elif answer:
        if (check_time - elapsed_time) <= 0:
            answer = False
            memorize = True
            correct = False
            tiles.clear()
            level += 1
            memorize_time -= 5
            start_ticks = pygame.time.get_ticks()
            difficulty_changed = False

    # check answer
    if answer and (not correct):
        for t in tiles:
            tile_mask = pygame.mask.from_surface(t["img"])
            offset = (int(character_x_pos - t["x_pos"]), int(character_y_pos - t["y_pos"]))
            result = tile_mask.overlap(character_mask, offset)
            if result and t["img"] == tile_images[target_color]:
                correct= True
                break

    if memorize:
        action = game_font.render("Memorize the Colors and their Positions!",\
             True, (255, 255, 255))
        timer = game_font.render("Time Left: {}".format(int(memorize_time - elapsed_time)),\
             True, (255, 255, 255))
    elif find:
        action = game_font.render("Find the correct Color! The color is {}"\
             .format(colors[target_color]), True, (255, 255, 255))
        timer = game_font.render("Time Left: {}".format(int(find_time - elapsed_time))\
             , True, (255,255,255))
    elif answer and correct and level == 17:
        action = game_font.render("You are Correct!", True, (0, 255, 0))
        timer = game_font.render("You have finished all the levels! Congrats!!", True, (0, 255, 0))
        running = False
        win = True
    elif answer and correct:
        action = game_font.render("You are Correct!", True, (0, 255, 0))
        timer = game_font.render("Next round begins in: {}".format(int(check_time - elapsed_time)),\
             True, (255, 255, 255))
    elif answer and (not correct):
        action = game_font.render("Your answer is Wrong", True, (255, 0, 0))
        running = False

    level_display = game_font.render("Level {}".format(level), True, (255, 255, 255))
    difficulty_display = game_font.render("Difficulty : {}".format(difficulty), True, (255, 255, 255))
    screen.blit(level_display, (380, 10))
    screen.blit(difficulty_display, (670, 120))
    screen.blit(action, (20, 60))
    screen.blit(timer, (20, 90))

    # Tiles
    if memorize:
        for idx, val in enumerate(tiles):
            tile_img = val["img"]
            tile_x = val["x_pos"]
            tile_y = val["y_pos"]
            screen.blit(tile_img, (tile_x, tile_y))
    elif answer:
        for idx, val in enumerate(tiles):
            tile_img = val["img"]
            tile_x = val["x_pos"]
            tile_y = val["y_pos"]
            if tile_images[target_color] == tile_img:
                screen.blit(tile_img, (tile_x, tile_y))
            else:
                screen.blit(hidetile, (tile_x, tile_y))
    else:
        for i in range(0, 56):
            col = i % 8
            row = i // 8
            screen.blit(hidetile, ((col * 110), 200 + (row * 110)))

    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update()

game_font = pygame.font.Font(None, 100)
if win:
    msg = game_font.render("You Won!!", True, (0, 0, 0))
else:
    msg = game_font.render("Game Over", True, (0, 0, 0))

msg_rect = msg.get_rect(center=(int(screen_width/2), int(screen_height/2)))
screen.blit(msg, msg_rect)
pygame.display.update()
pygame.time.delay(4000)

pygame.quit()


from __future__ import unicode_literals
import vmraid
from vmraid.translate import get_lang_dict

def execute():
	vmraid.reload_doc('core', 'doctype', 'language')

	from vmraid.core.doctype.language.language import sync_languages
	sync_languages()

	# move language from old style to new style for old accounts
	# i.e. from "english" to "en"

	lang_dict = get_lang_dict()
	language = vmraid.db.get_value('System Settings', None, 'language')
	if language:
		vmraid.db.set_value('System Settings', None, 'language', lang_dict.get('language') or 'en')

	for user in vmraid.get_all('User', fields=['name', 'language']):
		if user.language:
			vmraid.db.set_value('User', user.name, 'language',
				lang_dict.get('language') or 'en', update_modified=False)


# coding:utf-8
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""
# 这题有点绕，看答案有人说可以用morris算法
# 这里用的是用一个prev记录上一次访问过的点，然后看栈顶的node,如果有左儿子，就push进栈
# 这样一路push可以找到最左边的那个node,只要满足当前这个点prev是自己，且上个访问过的节点不是自己的
# 左儿子，那就可以append到result里，如果是的话说明是现在访问的node是已经访问过的
# 就看有没有右儿子
# 有的话继续，没有的话这个也append到result，完成post traversal

class Solution:
    """
    @param root: A Tree
    @return: Postorder in ArrayList which contains node values.
    """
    def postorderTraversal(self, root):
        # write your code here
        if not root:
            return []

        stack = []
        postorder = []
        prev = None # previously traversed node
        curr = root
        stack.append(root)
        while stack:
            curr = stack[-1]
            if prev is None or prev.left == curr or prev.right == curr:
                if curr.left is not None:
                    stack.append(curr.left)
                elif curr.right is not None:
                    stack.append(curr.right)
            else:
                if curr.left == prev:
                    if curr.right is not None:
                        stack.append(curr.right)
                else:
                    postorder.append(curr.val)
                    stack.pop()
            prev = curr

        return postorder

lintcode algo ladder/68. Binary Tree Postorder Traversal.py


from django.urls import path
from .views import index, detail, result

app_name = "boss"

urlpatterns = [
    path('', index, name="index"),
    path('<int:pk>/', detail, name='detail'),
    path('<int:pk>/result/', result, name='result'),

]

# urlpatterns = [
#     path('index/', index, name="index"),
#     path('<int:pk>/', detail, name='detail'),
#     path('<int:pk>/result/', result, name='result'),

# ]


from aiohttp import web
import socketio
from faceRecog import FacRecog
import time
import redis
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
r = redis.Redis(host='localhost', port=6379, db=0)

async def index(request):
    with open('index.html') as f:
        return web.Response(text=f.read(), content_type='text/html')

@sio.on('renderFrame')
async def print_message(sid, message):
    index = 0
    imageSX4 = ""
    print(message)
    while(True):
        imageSX4 = (r.get(index)).decode('utf-8')
        print("Connected via Socket ID: ", sid)
        if(r.get(str(index))) == None:
            time.sleep(2)
            continue
        await sio.emit('response', imageSX4)
        print(message)
        r.delete(str(index))
        index+=1
        time.sleep(1.5)

app.router.add_get('/', index)
if __name__ == '__main__':
    web.run_app(app)


""" Search's indexes. """
from haystack import indexes

from .models import Solution

class SolutionIndex(indexes.SearchIndex, indexes.Indexable):

    """ Index by solutions. """

    text = indexes.CharField(document=True, use_template=True, null=True)
    title = indexes.CharField(model_attr='title', null=True)
    time_updated = indexes.DateTimeField(model_attr='time_updated')
    project = indexes.CharField(model_attr='project__title')

    def get_model(self):
        """ Point to model.

        :return Task:

        """
        return Solution

    def get_updated_field(self):
        """ Point to updated field.

        :return str:

        """
        return 'time_updated'


from typing import List

class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        if not grid:
            return 0
        m, n = len(grid), len(grid[0])
        answer = 0

        def dfs(i, j):
            grid[i][j] = '2'
            for di, dj in (0, 1), (0, -1), (1, 0), (-1, 0):
                ii, jj = i + di, j + dj
                if 0 <= ii < m and 0 <= jj < n and grid[ii][jj] == '1':
                    dfs(ii, jj)
        for i in range(m):
            for j in range(n):
                if grid[i][j] == '1':
                    dfs(i, j)
                    answer += 1
        return answer

if __name__ == '__main__':

    # Inputs and Expected Outputs
    grid_1 = [
        ["1", "1", "1", "1", "0"],
        ["1", "1", "0", "1", "0"],
        ["1", "1", "0", "0", "0"],
        ["0", "0", "0", "0", "0"]
    ]
    expected_output_1 = 1

    grid_2 = [
        ["1", "1", "0", "0", "0"],
        ["1", "1", "0", "0", "0"],
        ["0", "0", "1", "0", "0"],
        ["0", "0", "0", "1", "1"]
    ]
    expected_output_2 = 3

    # Run Tests
    solution_1 = Solution()
    solution_2 = Solution()
    test_1 = solution_1.numIslands(grid_1)
    test_2 = solution_2.numIslands(grid_2)

    # Print Results
    print(f"\nTest 1 Result: {test_1} \nExpected Result: {expected_output_1}")
    print(f"\nTest 2 Result: {test_2} \nExpected Result: {expected_output_2}")