class Node:
	'''
		Node represents a node in the Binary Search Tree
		functions:
			addNode(self,value) - adds a node to the BST with data = value
			printTree(self, order) - prints the BST,
				order = "in" by default, prints BST in inorder form
				order = "pre" prints BST in preorder form
				order = "post" prints BST in postorder form
				order = "revin" prints the BST in reverse inorder format i.e Right->Root->Left
	'''
	def __init__(self, value):
		'''
			__init__(self,value) - creates an instance of Node with the given value
		'''
		self.left = None
		self.right = None
		self.value = value

	def addNode(self, value):
		'''
			addNode(self, value) - adds a Node with the given value
				If the tree has no nodes, the node is created as root
				If the value of new node > root node value, recursively call the function to add it as a right child
				If the value of new node < root node value, recursively call the function to add it as a left child
		'''
		if self.value :
			if value < self.value:
				if self.left is None:
					self.left = Node(value)
				else:
					self.left.addNode(value)
			elif value > self.value:
				if self.right is None:
					self.right = Node(value)
				else:
					self.right.addNode(value)
			else:
				self.value = value
	def printTree(self,order = "in"):
		'''
			printTree(self,order) - prints the BST
				order = "in"(default value) prints the BST in inorder format
				order = "pre" prints the BST in preorder format
				order = "post" prints the BST in postorder format
				order = "revin" prints the BST in reverse inorder format i.e Right->Root->Left
		'''
		if order == "in":
			if self.left is not None:
				self.left.printTree(order)
			print(self.value, end = " ")
			if self.right is not None:
				self.right.printTree(order)
		elif order == "pre":
			print(self.value, end=" ")
			if self.left is not None:
				self.left.printTree(order)
			if self.right is not None:
				self.right.printTree(order)
		elif order == "post":
			if self.left is not None:
				self.left.printTree(order)
			if self.right is not None:
				self.right.printTree(order)
			print(self.value, end =" ")
		elif order == "revin":
			if self.right is not None:
				self.right.printTree(order)
			print(self.value, end=" ")
			if self.left is not None:
				self.left.printTree(order)

	def getTreeRepresentation(self, order="in"):
		'''
			getTreeRepresentation(self,order) - gets the tree representation of BST
				order = "in"(default value) gets the tree representation of BST in inorder format
				order = "pre" gets the tree representation of BST in preorder format
				order = "post" gets the tree representation of BST in postorder format
				order = "revin" gets the tree representation of BST in reverse inorder format i.e Right->Root->Left
		'''
		treeRepr = ""
		if order == "in":
			if self.left is not None:
				treeRepr += self.left.getTreeRepresentation(order)
			treeRepr += str(self.value) + " "
			if self.right is not None:
				treeRepr += self.right.getTreeRepresentation(order)
		elif order == "pre":
			treeRepr += str(self.value) + " "
			if self.left is not None:
				treeRepr += self.left.getTreeRepresentation(order)
			if self.right is not None:
				treeRepr += self.right.getTreeRepresentation(order)
		elif order == "post":
			if self.left is not None:
				treeRepr += self.left.getTreeRepresentation(order)
			if self.right is not None:
				treeRepr += self.right.getTreeRepresentation(order)
			treeRepr += str(self.value) + " "
		elif order =="revin":
			if self.right is not None:
				treeRepr += self.right.getTreeRepresentation(order)
			treeRepr+= str(self.value) + " "
			if self.left is not None:
				treeRepr += self.left.getTreeRepresentation(order)
		return treeRepr

	def __contains__(self, value):
		'''
			__contains__(self, value) - returns True if the value is present in the BST else returns False
		'''
		if value > self.value and self.right is not None:
			return self.right.__contains__(value)
		elif value < self.value and self.left is not None:
			return self.left.__contains__(value)
		return self.value == value

class PS06:
	def __init__(self):
		self.increasingTree = None
		self.decreasingTree = None
		self.max_element = None
		self.N = 0
		self.incrCount = 0
		self.decrCount = 0

	def readInput(self, filename):
		'''
			def readInput(self, filename):
				This function reads a file with name given by the filename and returns the initial BSequence, number of new elements and new elements
		'''
		inputFile = open(filename)
		lines=inputFile.readlines()
		lines=[line.replace("\n","").strip() for line in lines]
		inputFile.close()
		try:
			self.N=int(lines[0])
			S=[int(i) for i in  lines[1].split(" ")]
			if self.N != len(S):
				raise Exception('Given size of elements in B-sequence do not match with actual size')
			Q=int(lines[2])
			Q_elements =[int(line.replace("\n","").strip()) for line in lines[3:]]
			if Q != len(Q_elements):
				raise Exception('given size of new elements do not match with the actual size')
			return S,Q,Q_elements
		except ValueError:
			raise Exception('Input values are not acceptable!')

	def initialize(self,input_b_sequence):
		'''
			def initialize(self, input_b_sequence):
				This function builds an increasingTree and a decreasingTree out of the given input B-Sequence
		'''
		self.max_element = max(input_b_sequence)
		max_element_index = input_b_sequence.index(self.max_element)
		for i in input_b_sequence[:max_element_index]:
			if self.increasingTree is None:
				self.increasingTree = Node(i)
			else:
				self.increasingTree.addNode(i)
			self.incrCount+=1
		for i in input_b_sequence[max_element_index+1:]:
			if self.decreasingTree is None:
				self.decreasingTree = Node(i)
			else:
				self.decreasingTree.addNode(i)
			self.decrCount+=1

	def addElementsToSequenceAndOutputResultToFile(self, elements_to_add, filename):
		'''
			def addElementsToSequenceAndOutputResultToFile(self, elements_to_add, filename):
				This function adds elements to the extisting B-Sequence, and writes the output to a file
		'''
		outputFile = open(filename, 'w')
		for i in range(len(elements_to_add)):
			new_element = elements_to_add[i]
			if new_element == self.max_element:
				pass
			elif new_element > self.max_element:
				self.increasingTree.addNode(self.max_element)
				self.incrCount+=1
				self.max_element = new_element
			elif self.increasingTree is not None and new_element in self.increasingTree:
				if self.decreasingTree is None:
					self.decreasingTree = Node(new_element)
					self.decrCount += 1
				elif new_element in self.decreasingTree:
					pass
				else:
					self.decreasingTree.addNode(new_element)
					self.decrCount+=1
			else:
				self.increasingTree.addNode(new_element)
				self.incrCount += 1
			outputFile.write(str(self.incrCount+1+self.decrCount)+"\n")
		new_node=Node(self.max_element)
		new_node.left = self.decreasingTree
		self.decreasingTree = new_node
		if self.increasingTree is not None:
			outputFile.write(self.increasingTree.getTreeRepresentation().strip()+ ' ')
		if self.decreasingTree is not None:
			outputFile.write(self.decreasingTree.getTreeRepresentation('revin').strip()+ ' ')
		outputFile.write('\n')
		outputFile.close()

def main():
	problem = PS06()
	inputFilename = 'inputPS6.txt'
	outputFilename = 'outputPS6.txt'
	try:
		S,Q,Q_elements = problem.readInput(inputFilename)
		problem.initialize(S)
		problem.addElementsToSequenceAndOutputResultToFile(Q_elements, outputFilename)
	except Exception as error:
		print('Error occurred:\n\t'+repr(error))

if __name__ == '__main__':
	main()


import sys

from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(
  packages = [], excludes = [],
  include_files = ['icon','toc'],
)

name = 'example'

if sys.platform == 'win32':
  name = name + '.exe'

base = None
if sys.platform == "win32":
    base = "Win32GUI"

executables = [
  Executable('main.py', base = base, targetName = name,
             compress = True,
            )
]

setup(name='Example',
      version = '1.0',
      description = 'An example program',
      options = dict(build_exe = buildOptions),
      executables = executables)


#!/usr/bin/env python3

import numpy as np
import h5py
import argparse
import matplotlib
import matplotlib.pyplot as plt
import csv
import glob
import os

def get_dataset_keys(f):
    keys = []
    f.visit(lambda key : keys.append(key) if isinstance(f[key], h5py.Dataset) else None)
    return keys

def plot(time, data, key, figure_name):
    os.system("mkdir -p {}min_max_mean/".format(figure_name))
    if "min" in key or "mean" in key or "max" in key:
        fig, (ax1) = plt.subplots(nrows=1, sharex=True)
        plt.subplots_adjust(hspace=0.1)
        ax1.plot(time, data[key], c="k")  # , s=0.1, alpha=0.3)
        ax1.set_title("Key: {}".format(key))
        # ax1.set_xlabel(r'$r$')
        # ax1.set_ylabel(r'$\rho$')
        # ax1.set_xlim(0, r_max)
        # ax1.set_ylim(0, 4.0)
        fig.tight_layout()
        plt.savefig("{}min_max_mean/min_max_mean_{}.png".format(figure_name, key))
    else:
        fig, (ax1) = plt.subplots(nrows=1, sharex=True)
        plt.subplots_adjust(hspace=0.1)
        # linestyle="dotted",
        # linestyle="dashed",
        # linestyle="dashdot"
        ax1.plot(time, data["{}_min".format(key)], c="k", linestyle="dotted", label="min")  # , s=0.1, alpha=0.3)
        ax1.plot(time, data["{}_max".format(key)], c="k", linestyle="dashed", label="max")
        ax1.plot(time, data["{}_mean".format(key)], c="k", linestyle="dashdot", label="mean")
        ax1.set_title("Key: {}".format(key))
        ax1.legend(loc="best")
        # ax1.set_xlabel(r'$r$')
        # ax1.set_ylabel(r'$\rho$')
        # ax1.set_xlim(0, r_max)
        # ax1.set_ylim(0, 4.0)
        fig.tight_layout()
        plt.savefig("{}min_max_mean/min_max_mean_{}.png".format(figure_name, key))

if __name__ == '__main__':

    plot_all = False

    parser = argparse.ArgumentParser(description="min/max/mean for all entries")
    parser.add_argument("--input", "-i", metavar="str", type=str, help="input file",
                        nargs="?", default="../output")
    parser.add_argument("--output", "-o", metavar="str", type=str, help="output directory",
                        nargs="?", default="-")
    parser.add_argument("--key", "-k", metavar="str", type=str, help="output directory",
                        nargs="?", default="-")
    parser.add_argument('--all', "-a", action='store_true')
    args = parser.parse_args()

    if args.all:
        plot_all = True

    header = []
    detailed_header = []
    data = {}

    with open(args.input, newline='\n') as csvfile:
        reader = csv.reader(csvfile, delimiter=';', quotechar='|')
        for i_row, row in enumerate(reader):
            if i_row == 0:
                for elem in row:
                    header.append(elem)
            elif i_row == 1:
                for elem in row:
                    detailed_header.append(elem)
                    data[elem] = []
            else:
                for i_elem, elem in enumerate(row):
                    data[detailed_header[i_elem]].append(float(elem))

    # print("data[{}] = {}".format(args.key, data[args.key]))
    if args.key in header or args.key in detailed_header:
        plot(data["time_max"], data, args.key, args.output)

    if plot_all:
        for _header in header:
            print("plotting {} ...".format(_header))
            plot(data["time_max"], data, _header, args.output)


'''LETTERCASE PERCENTAGE RATIO

Your goal is to find the percentage ratio of lowercase and uppercase letters in
line below.

INPUT SAMPLE:
Your program should accept as its first argument a path to a filename. Each line
of input contains a string with uppercase and lowercase letters E.g.:
thisTHIS
AAbbCCDDEE
N
UkJ

OUTPUT SAMPLE:
For each line from input, print the percentage ratio of uppercase and lowercase
letters rounded to the second digit after the dot. E.g.:
lowercase: 50.00 uppercase: 50.00
lowercase: 20.00 uppercase: 80.00
lowercase: 0.00 uppercase: 100.00
lowercase: 33.33 uppercase: 66.67
'''
def f(s):
    s = s.strip()
    lower = sum(char.islower() for char in s)
    return "lowercase: {:0.2f} uppercase: {:1.2f}".format(
        round(100*lower/len(s),2), round(100*(len(s)-lower)/len(s),2))

Easy/c147 Lettercase Percentage Ratio.py


from django_filters import rest_framework as filters
from mondoir.utilities.api.filters import (
    UserDataModelFilterSet,
    MultipleValueFilter
)

class EducationFilterSet(UserDataModelFilterSet):
    institution_name = filters.CharFilter(
        field_name='institution_name',
        lookup_expr='icontains',
        help_text={'description': 'Institution name', "example": "k.n toosi"},
    )
    degree = MultipleValueFilter(
        field_name='degree',
        help_text={'description': 'Degree', "example": "POST->[1,2] queryparams->1,2,3"},
    )
    graduated_date = filters.DateFromToRangeFilter(
        field_name='graduated_date',
        help_text={
            'description': 'Graduated date',
            "example": "graduated_date_after: '2022-12-28', graduated_date_before: '2023-12-28'",
        },
    )


from datetime import datetime
from tqdm.auto import tqdm
import torch
import torch.nn as nn
from torch.nn.utils import clip_grad_norm_
from sklearn import metrics
import numpy as np

from model import VLPForTokenClassification, model_config_factory
from dataset import dataset_factory
from training.utils import get_tokenizer, get_scheduler, get_model_summary

import warnings

warnings.filterwarnings('ignore')

POS_TAGS = [
    "XX", "``", "$", "''", "*", ",", "-LRB-", "-RRB-", ".", ":", "ADD", "AFX",
    "CC", "CD", "DT", "EX", "FW", "HYPH", "IN", "JJ", "JJR", "JJS", "LS", "MD",
    "NFP", "NN", "NNP", "NNPS", "NNS", "PDT", "POS", "PRP", "PRP$", "RB",
    "RBR", "RBS", "RP", "SYM", "TO", "UH", "VB", "VBD", "VBG", "VBN", "VBP",
    "VBZ", "VERB", "WDT", "WP", "WP$", "WRB"
]
NER_TAGS = [
    "O", "B-PERSON", "I-PERSON", "B-NORP", "I-NORP", "B-FAC", "I-FAC", "B-ORG",
    "I-ORG", "B-GPE", "I-GPE", "B-LOC", "I-LOC", "B-PRODUCT", "I-PRODUCT",
    "B-DATE", "I-DATE", "B-TIME", "I-TIME", "B-PERCENT", "I-PERCENT",
    "B-MONEY", "I-MONEY", "B-QUANTITY", "I-QUANTITY", "B-ORDINAL", "I-ORDINAL",
    "B-CARDINAL", "I-CARDINAL", "B-EVENT", "I-EVENT", "B-WORK_OF_ART",
    "I-WORK_OF_ART", "B-LAW", "I-LAW", "B-LANGUAGE", "I-LANGUAGE"
]

# POS_TAGS = [
#     'XX', '"', "''", '#', '$', '(', ')', ',', '.', ':', '``', 'CC', 'CD', 'DT',
#     'EX', 'FW', 'IN', 'JJ', 'JJR', 'JJS', 'LS', 'MD', 'NN', 'NNP', 'NNPS',
#     'NNS', 'NN|SYM', 'PDT', 'POS', 'PRP', 'PRP$', 'RB', 'RBR', 'RBS', 'RP',
#     'SYM', 'TO', 'UH', 'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ', 'WDT', 'WP',
#     'WP$', 'WRB'
# ]

# NER_TAGS = [
#     'O', 'B-PER', 'I-PER', 'B-ORG', 'I-ORG', 'B-LOC', 'I-LOC', 'B-MISC',
#     'I-MISC'
# ]

TRAIN_DEBUG_STEP = 1000
TRAIN_EVAL_STEP = 3000

# TRAIN_DEBUG_STEP = 500
# TRAIN_EVAL_STEP = 1000

def run_token_cls_training(args):
    current_date_str = datetime.now().strftime('%m%d%Y_%H%M%S')
    tokenizer, vocab_size = get_tokenizer(args)

    config = model_config_factory(args.model_name)
    model = VLPForTokenClassification(**config,
                                      vocab_size=vocab_size,
                                      num_ner_tags=len(NER_TAGS),
                                      num_pos_tags=len(POS_TAGS),
                                      dropout=0.0)

    if args.pretrained_model_path:
        pretrained = torch.load(args.pretrained_model_path)["model_state"]
        model.load_state_dict(pretrained, strict=False)

    model.train()

    train_loader, test_loader = dataset_factory(args, config, tokenizer)
    optimizer, lr_scheduler = get_scheduler(model, args, config,
                                            len(train_loader))

    get_model_summary(model, args.image_size, args.max_text_len,
                      args.num_channels)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    best_eval_loss = np.inf
    count = 0
    for epoch in range(args.num_epochs):
        training_losses = []
        tr_ner_losses = []
        tr_pos_losses = []
        tr_txt_losses = []

        f1_micro_scores_ner, f1_macro_scores_ner = [], []
        f1_micro_scores_pos, f1_macro_scores_pos = [], []
        progress_bar = tqdm(train_loader,
                            desc='Training',
                            position=0,
                            leave=True)
        for step, (images, tokens, tgt, tgt_y, tgt_mask, ner_tags,
                   pos_tags) in enumerate(progress_bar):
            images, tgt = images.to(device), tgt.to(device)
            tgt_y, tgt_mask = tgt_y.to(device), tgt_mask.to(device)
            ner_tags, pos_tags = ner_tags.to(device), pos_tags.to(device)

            tgt_mask = tgt_mask.squeeze(1)

            logits_txt, logits_ner, logits_pos = model(images,
                                                       tgt,
                                                       tgt_mask=tgt_mask)

            loss_fct = nn.CrossEntropyLoss(
                label_smoothing=args.label_smoothing)
            loss_txt = loss_fct(logits_txt.view(-1, vocab_size),
                                tgt_y.view(-1))
            loss_ner = loss_fct(logits_ner.view(-1, len(NER_TAGS)),
                                ner_tags.view(-1))
            loss_pos = loss_fct(logits_pos.view(-1, len(POS_TAGS)),
                                pos_tags.view(-1))
            loss = loss_txt + loss_ner + loss_pos

            loss.backward()
            if args.use_clip_grad:
                clip_grad_norm_(model.parameters(), 1.0)
            optimizer.step()
            lr_scheduler.step()
            optimizer.zero_grad()

            training_losses.append(loss.item())
            tr_txt_losses.append(loss_txt.item())
            tr_ner_losses.append(loss_ner.item())
            tr_pos_losses.append(loss_pos.item())
            count += 1

            preds_ner = logits_ner.argmax(dim=-1)
            preds_pos = logits_pos.argmax(dim=-1)

            target_ner, preds_ner = ner_tags.cpu().detach().numpy(
            ), preds_ner.cpu().detach().numpy()
            target_pos, preds_pos = pos_tags.cpu().detach().numpy(
            ), preds_pos.cpu().detach().numpy()

            f1_score_micro_ner, f1_score_macro_ner = get_metrics(
                target_ner.flatten(), preds_ner.flatten(), display=False)
            f1_micro_scores_ner.append(f1_score_micro_ner)
            f1_macro_scores_ner.append(f1_score_macro_ner)

            f1_score_micro_pos, f1_score_macro_pos = get_metrics(
                target_pos.flatten(), preds_pos.flatten(), display=False)
            f1_micro_scores_pos.append(f1_score_micro_pos)
            f1_macro_scores_pos.append(f1_score_macro_pos)

            if step % TRAIN_DEBUG_STEP == 0:
                print("train target_ner", [NER_TAGS[t] for t in target_ner[0]])
                print("train preds_ner", [NER_TAGS[t] for t in preds_ner[0]])
                print("!" * 100)
                print("train target_pos", [POS_TAGS[t] for t in target_pos[0]])
                print("train preds_pos", [POS_TAGS[t] for t in preds_pos[0]])
                print("#" * 50)
                idxs = torch.argmax(logits_txt, dim=-1)
                for i, idx in enumerate(idxs):
                    if i == 1:
                        break
                    print('#' * 100)
                    print(
                        tokenizer.decode(tokens[i].tolist()).replace(
                            ' [PAD] ', '').replace('[PAD]', ''))
                    print('!' * 50)
                    print(
                        tokenizer.decode(idx.tolist()).replace(
                            ' [PAD] ', '').replace('[PAD]', ''))
                    print('#' * 100)

            logs = {
                "epoch": epoch + 1,
                # "loss": f"{np.mean(training_losses[-2000:]):.3f}",
                "loss_ner": f"{np.mean(tr_ner_losses[-2000:]):.3f}",
                "loss_pos": f"{np.mean(tr_pos_losses[-2000:]):.3f}",
                "loss_txt": f"{np.mean(tr_txt_losses[-2000:]):.3f}",
                # "f1_micro_ner": f"{np.mean(f1_micro_scores_ner[-2000:]):.3f}",
                "f1_ner:": f"{np.mean(f1_macro_scores_ner[-2000:]):.3f}",
                # "f1_micro_pos": f"{np.mean(f1_micro_scores_pos[-2000:]):.3f}",
                "f1_pos:": f"{np.mean(f1_macro_scores_pos[-2000:]):.3f}",
                "lr": lr_scheduler.get_last_lr()[0],
                "step": count
            }

            progress_bar.set_postfix(**logs)

            if not (count % TRAIN_EVAL_STEP):
                eval_loss = evaluate(model,
                                     test_loader,
                                     tokenizer,
                                     device=device,
                                     vocab_size=vocab_size,
                                     epoch=epoch,
                                     tr_step=step,
                                     current_date_str=current_date_str)

                if eval_loss < best_eval_loss:
                    torch.save(
                        {
                            'model_state': model.state_dict(),
                            #                         'optimizer_state': optimizer.state_dict(),
                        },
                        args.out_model_path)

                    best_eval_loss = eval_loss

                model.train()

    torch.save({
        'model_state': model.state_dict(),
    }, f"last_{args.out_model_path}")

    return model, optimizer

def get_metrics(targets, outputs, display=True):
    accuracy = metrics.accuracy_score(targets, outputs)
    f1_score_micro = metrics.f1_score(targets, outputs, average='micro')
    f1_score_macro = metrics.f1_score(targets, outputs, average='macro')
    if display:
        print(f"Accuracy Score = {accuracy}")
        print(f"F1 Score (Micro) = {f1_score_micro}")
        print(f"F1 Score (Macro) = {f1_score_macro}")
    return f1_score_micro, f1_score_macro

def evaluate(model,
             test_loader,
             tokenizer,
             vocab_size,
             device,
             epoch=0,
             tr_step=0,
             current_date_str="dummy_date"):
    print("usao")
    model.eval()
    total_loss_ner = 0
    total_loss_pos = 0
    total_loss_txt = 0
    total_loss = 0
    all_targets_ner = []
    all_preds_ner = []
    all_targets_pos = []
    all_preds_pos = []
    with torch.no_grad():
        for step, (images, tokens, tgt, tgt_y, tgt_mask, ner_tags,
                   pos_tags) in enumerate(tqdm(test_loader)):

            images, tgt = images.to(device), tgt.to(device)
            tgt_y, tgt_mask = tgt_y.to(device), tgt_mask.to(device)
            ner_tags, pos_tags = ner_tags.to(device), pos_tags.to(device)

            tgt_mask = tgt_mask.squeeze(1)

            logits_txt, logits_ner, logits_pos = model(images,
                                                       tgt,
                                                       tgt_mask=tgt_mask)

            loss_fct = nn.CrossEntropyLoss()
            loss_txt = loss_fct(logits_txt.view(-1, vocab_size),
                                tgt_y.view(-1))
            loss_ner = loss_fct(logits_ner.view(-1, len(NER_TAGS)),
                                ner_tags.view(-1))
            loss_pos = loss_fct(logits_pos.view(-1, len(POS_TAGS)),
                                pos_tags.view(-1))
            loss = loss_txt + loss_ner + loss_pos

            preds_ner = logits_ner.argmax(dim=-1)
            preds_pos = logits_pos.argmax(dim=-1)

            target_ner, preds_ner = ner_tags.cpu().detach().numpy(
            ), preds_ner.cpu().detach().numpy()
            target_pos, preds_pos = pos_tags.cpu().detach().numpy(
            ), preds_pos.cpu().detach().numpy()
            all_targets_ner.extend(target_ner.flatten())
            all_preds_ner.extend(preds_ner.flatten())
            all_targets_pos.extend(target_pos.flatten())
            all_preds_pos.extend(preds_pos.flatten())

            if step % 300 == 0:
                print("eval target_ner", [NER_TAGS[t] for t in target_ner[0]])
                print("eval preds_ner", [NER_TAGS[t] for t in preds_ner[0]])
                print("!" * 100)
                print("eval target_pos", [POS_TAGS[t] for t in target_pos[0]])
                print("eval preds_pos", [POS_TAGS[t] for t in preds_pos[0]])
                print("#" * 50)
                idxs = torch.argmax(logits_txt, dim=-1)
                for i, idx in enumerate(idxs):
                    if i == 1:
                        break
                    print('#' * 100)
                    print(
                        tokenizer.decode(tokens[i].tolist()).replace(
                            ' [PAD] ', '').replace('[PAD]', ''))
                    print('!' * 50)
                    print(
                        tokenizer.decode(idx.tolist()).replace(
                            ' [PAD] ', '').replace('[PAD]', ''))
                    print('#' * 100)

            total_loss += loss.item()
            total_loss_txt += loss_txt.item()
            total_loss_ner += loss_ner.item()
            total_loss_pos += loss_pos.item()

        total_loss /= len(test_loader)
        total_loss_txt /= len(test_loader)
        total_loss_ner /= len(test_loader)
        total_loss_pos /= len(test_loader)

        print(f"Valid Loss: {total_loss}")
        print(f"Valid Loss Text: {total_loss_txt}")
        print(f"Valid Loss NER: {total_loss_ner}")
        print(f"Valid Loss POS: {total_loss_pos}")
        get_metrics(all_targets_ner, all_preds_ner, display=True)
        print(
            metrics.classification_report(all_targets_ner,
                                          all_preds_ner,
                                          target_names=NER_TAGS,
                                          labels=list(range(len(NER_TAGS)))))
        get_metrics(all_targets_pos, all_preds_pos, display=True)
        print(
            metrics.classification_report(all_targets_pos,
                                          all_preds_pos,
                                          target_names=POS_TAGS,
                                          labels=list(range(len(POS_TAGS)))))
    return total_loss


"""
Harvester scripts

Currently only supports AVR atdf files
"""
# Python 3 compatibility for Python 2
from __future__ import print_function

import argparse
import textwrap
from xml.etree import ElementTree

from pymcuprog.deviceinfo.memorynames import MemoryNames
from pymcuprog.deviceinfo.deviceinfokeys import DeviceMemoryInfoKeys, DeviceInfoKeysAvr

def map_atdf_memory_name_to_pymcuprog_name(atdf_name):
    """
    Mapping a memory name in atdf files to the corresponding memory name used in the pymcuprog device models

    :param atdf_name: Name of memory in atdf files
    :return: Name of memory in pymcuprog device models
    """
    pymcuprog_name = 'unknown'
    if atdf_name == 'progmem':
        pymcuprog_name = MemoryNames.FLASH
    if atdf_name == 'user_signatures':
        # Datasheets actually use user_row for UPDI devices at least
        pymcuprog_name = MemoryNames.USER_ROW
    if atdf_name == 'eeprom':
        pymcuprog_name = MemoryNames.EEPROM
    if atdf_name == 'fuses':
        pymcuprog_name = MemoryNames.FUSES
    if atdf_name == 'lockbits':
        pymcuprog_name = MemoryNames.LOCKBITS
    if atdf_name == 'signatures':
        pymcuprog_name = MemoryNames.SIGNATURES
    if atdf_name == 'internal_sram':
        pymcuprog_name = MemoryNames.INTERNAL_SRAM

    return pymcuprog_name

def determine_chiperase_effect(name, architecture):
    if 'avr' in architecture:
        if name in ['user_signatures', 'fuses', 'signatures']:
            return('ChiperaseEffect.NOT_ERASED')
        elif name in ['internal_sram', 'lockbits', 'progmem']:
            return('ChiperaseEffect.ALWAYS_ERASED')
        elif name in ['eeprom']:
            return('ChiperaseEffect.CONDITIONALLY_ERASED_AVR')
    else:
        return('# To be filled in manually')

def determine_isolated_erase(name, architecture):
    if 'avr' in architecture:
        if name in ['user_signatures', 'eeprom']:
            return('True')
        elif name in ['internal_sram', 'lockbits', 'progmem', 'fuses', 'signatures']:
            return('False')
    else:
        return('# To be filled in manually')

def determine_write_size(memory_name, page_size, device_name):
    write_size = 1
    device_name = device_name.lower()
    if memory_name is 'flash':
        if (device_name.find('avr') != -1 and ((device_name.find('da') != -1) or (device_name.find('db') != -1) or (device_name.find('dd') != -1))):
            write_size = 2
        else:
            write_size = page_size
    elif memory_name is 'signatures':
        write_size = 0
    return write_size

def capture_memory_attribute(attribute, architecture, device_name):
    """
    Capture the memory attribute

    :param attribute: memory attribute to capture
    :return: attributes found as a string
    """
    name = attribute['name'].lower()
    size = attribute['size']
    start = attribute['start']

    try:
        pagesize = attribute['pagesize']
    except KeyError:
        pagesize = '1'
    # For some AVRs the ATDF gives a pagesize of fuses and lockbits equal to flash or EEPROM page size but fuses and lockbits are always byte accessible.
    if name in ['fuses', 'lockbits']:
        pagesize = '1'
    # Read size is always 1 byte except for flash that can only read complete words
    readsize = 1
    if name in ['progmem']:
        readsize = 2
    output = ""
    # These names are the names used in the atdf files and might differ from the pymcuprog MemoryNames
    if name in ['progmem', 'eeprom', 'user_signatures', 'fuses', 'lockbits', 'signatures', 'internal_sram']:
        print_name = map_atdf_memory_name_to_pymcuprog_name(name)
        output += "\n    # {}\n".format(print_name)
        output += capture_field('{}_{}_byte'.format(print_name, DeviceMemoryInfoKeys.ADDRESS), start)
        output += capture_field('{}_{}_bytes'.format(print_name, DeviceMemoryInfoKeys.SIZE), size)
        output += capture_field('{}_{}_bytes'.format(print_name, DeviceMemoryInfoKeys.PAGE_SIZE), pagesize)
        # These are the same for all AVRs
        output += "    '{}_{}_bytes': {},\n".format(print_name, DeviceMemoryInfoKeys.READ_SIZE, readsize)
        output += "    '{}_{}_bytes': {},\n".format(print_name, DeviceMemoryInfoKeys.WRITE_SIZE, determine_write_size(print_name, pagesize, device_name))
        output += "    '{}_{}': {},\n".format(print_name, DeviceMemoryInfoKeys.CHIPERASE_EFFECT, determine_chiperase_effect(name, architecture))
        output += "    '{}_{}': {},\n".format(print_name, DeviceMemoryInfoKeys.ISOLATED_ERASE, determine_isolated_erase(name, architecture))
    return output

def capture_register_offset(name, offset):
    """
    Wrapper to create a string definition

    :param name: register name
    :param offset: register offset
    :return: string of register and offset
    """
    return capture_field("{}_base".format(name.lower()), offset)

def capture_field(field, value):
    """
    Macro to create text format field

    :param field: register name
    :param value: register value
    :return: string of definition
    """
    try:
        _test_value = int(value, 16)
    except (ValueError, AttributeError):
        # Can't convert string to int, assumed to be string
        return "    '{}': '{}',\n".format(field, value)
    return "    '{}': {},\n".format(field, value)

def capture_device_element(element, device_name):
    """
    Capture data from a device element

    :param element: element with tag='device'
    :return: captured data from the device element as a string
    """
    architecture = element.attrib['architecture'].lower()
    output = capture_field('name', element.attrib['name'].lower())
    output += capture_field('architecture', architecture)
    for i in element.iterfind("address-spaces/address-space/memory-segment"):
        output += capture_memory_attribute(i.attrib, architecture, device_name)
    output += "\n    # Some extra AVR specific fields\n"
    return output

def capture_module_element(element):
    """
    Capture data from a module element

    This function will return data captured from the module element but will also check if the module
    element contains info about an UPDI fuse (fuse to configure a shared UPDI pin)
    :param element: element with tag='module'
    :return output, found_updi_fuse
        output: captured module element data as a string
        found_updi_fuse: True if the module element contained info about an UPDI fuse
    """
    output = ""
    found_updi_fuse = False
    for i in element.iterfind("instance/register-group"):
        name = i.attrib['name']
        offset = "0x{:08X}".format(int(i.attrib['offset'], 16))
        if i.attrib['name'] == 'SYSCFG':
            output += capture_register_offset(name, offset)
            output += capture_register_offset('OCD', "0x{:08X}".format(int(offset, 16) + 0x80))
        if i.attrib['name'] == 'NVMCTRL':
            output += capture_register_offset(name, offset)
    for i in element.iterfind("instance/signals/signal"):
        if i.attrib['group'] == 'UPDI' and i.attrib['pad'] is not None:
            output += capture_field('prog_clock_khz', '900')
            found_updi_fuse = True
    return output, found_updi_fuse

#This dict serves as a temporary workaround for an xml-parsing bug, where the device signature can't be extracted for some devices
spare_signatures = {
    'atmega16':	bytearray(b'\x1e\x94\x03'),
    'atmega1609': bytearray(b'\x1e\x94\x26'),
    'atmega168a': bytearray(b'\x1e\x94\x06'),
    'atmega3209': bytearray(b'\x1e\x95\x31'),
    'atmega328': bytearray(b'\x1e\x95\x14'),
    'atmega4809': bytearray(b'\x1e\x96\x51'),
    'atmega48p': bytearray(b'\x1e\x92\x0a'),
    'atmega809': bytearray(b'\x1e\x93\x2a'),
    'atmega88a': bytearray(b'\x1e\x93\x0a'),
    'attiny202': bytearray(b'\x1e\x91\x23'),
    'attiny402': bytearray(b'\x1e\x92\x27'),
    'avr128da32': bytearray(b'\x1e\x97\x09'),
    'avr32da32': bytearray(b'\x1e\x95\x33'),
    'avr64da32': bytearray(b'\x1e\x96\x14')
}

def capture_signature_from_property_groups_element(element, device_name):
    """
    Capture signature (Device ID) data from a property-group element

    :param element: element with tag='property-groups'
    :return bytearray with 3 bytes of Device ID data
    """
    signature = bytearray(3)
    success = False
    for i in element.iterfind("property-group/property"):
        if i.attrib['name'] == 'SIGNATURE0':
            signature[0] = int(i.attrib['value'], 16)
        if i.attrib['name'] == 'SIGNATURE1':
            signature[1] = int(i.attrib['value'], 16)
        if i.attrib['name'] == 'SIGNATURE2':
            signature[2] = int(i.attrib['value'], 16)
            success = True
    if success is False:
        signature = spare_signatures[device_name.lower()]
        success = True
    return signature, success

#This dict serves as a temporary workaround for an xml-parsing bug, where the flash_offset can't be extracted for some devices
spare_flash_offset = {
    'atmega809': '0x00004000',
    'atmega1609': '0x00004000',
    'attiny202': '0x00008000',
    'attiny402': '0x00008000',
    'avr32da32': '0x00800000',
    'avr64da32': '0x00800000',
    'avr128da32': '0x00800000',
}

def correct_flash_offset(element, device_name, data_string):
    corrected_data_string = ''
    device_name = device_name.lower()
    success = False
    for i in element.iterfind("property-group/property"):
        if i.attrib['name'] == 'PROGMEM_OFFSET':
            value = i.attrib['value']
            success = True
    if success is False:
        try:
            value = spare_flash_offset[device_name.lower()]
            success = True
        except:
            success = False
    if success is True:
        ind = data_string.find("'flash_address_byte': ")
        corrected_data_string += data_string[:ind+22]
        corrected_data_string += value
        corrected_data_string += data_string[ind+22+10:]
        return corrected_data_string

    return data_string

def determine_address_size(device_name):
    address_size = '16-bit'
    device_name = device_name.lower()
    if (device_name.find('avr') != -1 and ((device_name.find('da') != -1) or (device_name.find('db') != -1))):
        address_size = '24-bit'
    return address_size

def harvest_from_file(filename):
    """
    Harvest parameters from a file

    :param filename: path to file to parse
    :return: list of parameters
    """
    xml_iter = ElementTree.iterparse(filename, events=('start', 'end'))
    output = ""
    shared_updi = False
    for event, elem in xml_iter:
        if event == 'start':
            if elem.tag == 'device':
                device_name = elem.attrib['name']
                output += capture_device_element(elem, device_name)
            if elem.tag == 'module':
                module_output, found_updi_fuse = capture_module_element(elem)
                output += module_output
                if found_updi_fuse:
                    shared_updi = True
            if elem.tag == 'interface':
                output += capture_field(elem.tag, elem.attrib['name'])
            if elem.tag == 'property-groups':
                signature, signature_harvest_success = capture_signature_from_property_groups_element(elem, device_name)
                    # signature_harvest_success provides a temporary workaround for an xml-parsing bug
                output = correct_flash_offset(elem, device_name, output)

    output += capture_field('address_size', determine_address_size(device_name))
    if not shared_updi:
        output += capture_field(DeviceInfoKeysAvr.PROG_CLOCK_KHZ, '1800')
    output += capture_field(DeviceInfoKeysAvr.DEVICE_ID,
                            "0x{:02X}{:02X}{:02X}".format(signature[0], signature[1], signature[2]))

    return output, signature_harvest_success

def main():
    """
    Main function for the harvest utility
    """
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent('''\
    Harvests device data from a device data file (.atdf) for one device.

    The harvested data can be used to populate a device file in deviceinfo.devices
        '''))

    parser.add_argument("filename",
                        help="name (and path) of file to harvest data from"
                        )

    arguments = parser.parse_args()

    dict_content = harvest_from_file(arguments.filename)
    content = "\nfrom pymcuprog.deviceinfo.eraseflags import ChiperaseEffect\n\n"
    content += "DEVICE_INFO = {{\n{}}}".format(dict_content)
    print(content)

if __name__ == "__main__":
    main()


import sys
import pygame
import random
from src import hero
from src import enemy

class Controller:
    def __init__(self, width=640, height=480):
        """
        Initializes and sets up the game
        args: self.width (int) Width (left to right) of the screen
        self.height (int) Height (top to bottom) of the screen
        self.screen (display) The screen that is displayed
        self.background (surface) Background of the screen
        self.enemies (sprite) Group of enemies as sprites
        num_enemies (int) The number of enemies in the game
        x (int) Random x coordinate for each enemy
        y (int) Random y coordinate for each enemy
        self.hero (class)
        self.all_sprites (sprite) All the sprites, including the hero and the enemies
        self.state (str) String labeled "GAME"
        return: none
        """
        pygame.init()
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill((250, 250, 250))  # set the background to white
        pygame.font.init()  # you have to call this at the start, if you want to use this module.
        pygame.key.set_repeat(1, 50)  # initialize a held keey to act as repeated key strikes
        """Load the sprites that we need"""

        self.enemies = pygame.sprite.Group()
        num_enemies = 3
        for i in range(num_enemies):
            x = random.randrange(100, 400)
            y = random.randrange(100, 400)
            self.enemies.add(enemy.Enemy("Boogie", x, y, 'assets/enemy.png'))
        self.hero = hero.Hero("Conan", 50, 80, "assets/hero.png")
        self.all_sprites = pygame.sprite.Group((self.hero,) + tuple(self.enemies))
        self.state = "GAME"

    def mainLoop(self):
        """
        Controls the state of the game
        args: self.state (str) String labeled as either "GAME" or "GAMEOVER"
        return: none
        """
        while True:
            if(self.state == "GAME"):
                self.gameLoop()
            elif(self.state == "GAMEOVER"):
                self.gameOver()

    def gameLoop(self):
        """
        Enables the hero to move based on key inputs from the user
        args: self.state (str) String labeled as "GAME"
        event.type (text) The input of the event that is taking place
        event.key (int) The key that is inputted on the keyboard from the user
        fights (list) The hero and the enemies colliding as means of fighting
        e (int) The enemy that the fighter is fighting
        self.hero.health (int) The number of health points the hero has
        self.state (str) String labeled as "GAMEOVER"
        return: none
        """
        while self.state == "GAME":
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if(event.key == pygame.K_UP):
                        self.hero.move_up()
                    elif(event.key == pygame.K_DOWN):
                        self.hero.move_down()
                    elif(event.key == pygame.K_LEFT):
                        self.hero.move_left()
                    elif(event.key == pygame.K_RIGHT):
                        self.hero.move_right()

            # check for collisions
            fights = pygame.sprite.spritecollide(self.hero, self.enemies, True)
            if(fights):
                for e in fights:
                    if(self.hero.fight(e)):
                        e.kill()
                        self.background.fill((250, 250, 250))
                    else:
                        self.background.fill((250, 0, 0))
                        self.enemies.add(e)

            # redraw the entire screen
            self.enemies.update()
            self.screen.blit(self.background, (0, 0))
            if(self.hero.health == 0):
                self.state = "GAMEOVER"
            self.all_sprites.draw(self.screen)

            # update the screen
            pygame.display.flip()

    def gameOver(self):
        """
        Ends the game and shows a gameover screen
        args: myfont (font) Changes the system font
        message (str) Writes out 'Game Over'
        event.type (text) The input of the event taking place
        return: none
        """
        self.hero.kill()
        myfont = pygame.font.SysFont(None, 30)
        message = myfont.render('Game Over', False, (0, 0, 0))
        self.screen.blit(message, (self.width / 2, self.height / 2))
        pygame.display.flip()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()


# Mathematics > Probability > Sherlock and Probability
# Help Sherlock in finding the probability.
#
# https://www.hackerrank.com/challenges/sherlock-and-probability/problem
# https://www.hackerrank.com/contests/infinitum-jul14/challenges/sherlock-and-probability
# challenge id: 2534
#

from fractions import Fraction

for _ in range(int(input())):
    n, k = map(int, input().split())
    bits = input()

    counter = [0] * (n + 1)
    for i, bit in enumerate(bits):
        counter[i + 1] = counter[i] + int(bit)

    p = 0
    for i, bit in enumerate(bits):
        if bit == "1":
            p += counter[min(n, i + k + 1)] - counter[max(0, i - k)]

    r = Fraction(p, n * n)
    print("{}/{}".format(r.numerator, r.denominator))


"""REINFORCE for learning an optimization algorithm."""

import time

import tensorflow as tf
from tf_agents.agents.reinforce import reinforce_agent
from tf_agents.drivers import dynamic_episode_driver as dy_ed
from tf_agents.networks import actor_distribution_network as actor_net
from tf_agents.networks import value_network as value_net
from tf_agents.replay_buffers import tf_uniform_replay_buffer
from tf_agents.train.utils import train_utils
from tf_agents.utils import common
from tf_agents.metrics import tf_metrics

from optfuncs import tensorflow_functions as tff

from sarlopt.environments import tf_function_env as tf_fun_env
from sarlopt.typing.types import LayerParam
from sarlopt.metrics import tf_custom_metrics

from experiments.evaluation import utils as eval_utils
from experiments.training import utils as training_utils

def reinforce_train(function: tff.TensorflowFunction,
                    dims: int,
                    seed=10000,
                    training_episodes: int = 2000,
                    stop_threshold: float = None,
                    env_steps: int = 250,
                    env_eval_steps: int = 500,
                    eval_interval: int = 100,
                    eval_episodes: int = 10,
                    lr: float = 3e-4,
                    discount: float = 0.99,
                    actor_layers: LayerParam = None,
                    value_layers: LayerParam = None,
                    summary_flush_secs: int = 10,
                    debug_summaries: bool = False,
                    summarize_grads_and_vars: bool = False):
  algorithm_name = 'REINFORCE'

  # Creating the training/agent directory
  agent_dir = training_utils.create_agent_dir(algorithm_name,
                                              function,
                                              dims)

  # Conversion to TFPyEnvironment's.
  tf_env_training = tf_fun_env.TFFunctionEnv(function=function,
                                             dims=dims,
                                             seed=seed,
                                             duration=env_steps)
  tf_env_eval = tf_fun_env.TFFunctionEnv(function=function,
                                         dims=dims,
                                         seed=seed,
                                         duration=env_eval_steps)

  # Instantiating the SummaryWriter's
  print('Creating logs directories.')
  log_dir, log_eval_dir, log_train_dir = training_utils.create_logs_dir(
    agent_dir)

  train_summary_writer = tf.compat.v2.summary.create_file_writer(
    log_train_dir, flush_millis=summary_flush_secs * 1000)
  train_summary_writer.set_as_default()

  eval_summary_writer = tf.compat.v2.summary.create_file_writer(
    log_eval_dir, flush_millis=summary_flush_secs * 1000)

  # Instantiating the metrics.
  train_metrics = [tf_metrics.AverageReturnMetric(),
                   tf_metrics.MaxReturnMetric()]

  eval_metrics = [tf_metrics.AverageReturnMetric(buffer_size=eval_episodes),
                  tf_custom_metrics.AverageBestObjectiveValueMetric(
                    function=function, buffer_size=eval_episodes)]

  # Agent, Neural Networks and Optimizers.
  obs_spec = tf_env_training.observation_spec()
  act_spec = tf_env_training.action_spec()
  time_spec = tf_env_training.time_step_spec()

  if actor_layers is None:
    actor_layers = [256, 256]

  actor_network = actor_net.ActorDistributionNetwork(
    input_tensor_spec=obs_spec,
    output_tensor_spec=act_spec,
    fc_layer_params=actor_layers)

  if value_layers is None:
    value_layers = [256, 256]

  value_network = value_net.ValueNetwork(input_tensor_spec=obs_spec,
                                         fc_layer_params=value_layers)

  optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=lr)

  train_step = train_utils.create_train_step()

  agent = reinforce_agent.ReinforceAgent(
    time_step_spec=time_spec,
    action_spec=act_spec,
    actor_network=actor_network,
    value_network=value_network,
    optimizer=optimizer,
    gamma=discount,
    normalize_returns=False,
    train_step_counter=train_step,
    debug_summaries=debug_summaries,
    summarize_grads_and_vars=summarize_grads_and_vars)

  agent.initialize()

  # Creating the Replay Buffer and drivers.
  replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer(
    data_spec=agent.collect_data_spec,
    batch_size=tf_env_training.batch_size,
    max_length=env_steps + 5)

  observers_train = [replay_buffer.add_batch] + train_metrics
  driver = dy_ed.DynamicEpisodeDriver(env=tf_env_training,
                                      policy=agent.collect_policy,
                                      observers=observers_train,
                                      num_episodes=1)

  eval_driver = dy_ed.DynamicEpisodeDriver(env=tf_env_eval,
                                           policy=agent.policy,
                                           observers=eval_metrics,
                                           num_episodes=eval_episodes)

  # Converting to tf.Function's
  driver.run = common.function(driver.run)
  eval_driver.run = common.function(eval_driver.run)
  agent.train = common.function(agent.train)

  # Metric evaluation function
  def compute_eval_metrics():
    return eval_utils.eager_compute(eval_metrics,
                                    eval_driver,
                                    train_step=agent.train_step_counter,
                                    summary_writer=eval_summary_writer,
                                    summary_prefix='Metrics')

  agent.train_step_counter.assign(0)

  @tf.function
  def train_phase():
    print('tracing')
    driver.run()
    experience = replay_buffer.gather_all()
    agent.train(experience)
    replay_buffer.clear()

    # Dictionary containing the hyperparameters
    hp_dict = {
      "seed": seed,
      "discount": discount,
      "training_episodes": training_episodes,
      "stop_threshold": stop_threshold,
      "learning_rate": lr,
      "optimizer": type(optimizer).__name__,
      "train_env": {
        "steps": env_steps,
        "function": function.name,
        "dims": dims,
        "domain": function.domain
      },
      "eval_env": {
        "steps": env_eval_steps,
        "function": function.name,
        "dims": dims,
        "domain": function.domain
      },
      "networks": {
        "actor_net": {
          "class": type(actor_network).__name__,
          "actor_layers": actor_layers
        },
        "value_net": {
          "class": type(value_network).__name__,
          "value_layers": value_layers
        }
      }
    }

    training_utils.save_specs(agent_dir, hp_dict)
    tf.summary.text("Hyperparameters",
                    training_utils.json_pretty_string(hp_dict),
                    step=0)

  # Training phase
  for ep in range(training_episodes):
    start_time = time.time()
    train_phase()

    for train_metric in train_metrics:
      train_metric.tf_summaries(train_step=agent.train_step_counter)

    if ep % eval_interval == 0:
      print('-------- Evaluation --------')
      start_eval = time.time()
      results = compute_eval_metrics()
      avg_return = results.get(eval_metrics[0].name)
      avg_best_value = results.get(eval_metrics[1].name)
      print('Average return: {0}'.format(avg_return))
      print('Average best value: {0}'.format(avg_best_value))
      print('Eval delta time: {0:.2f}'.format(time.time() - start_eval))
      print('---------------------------')
      if stop_threshold is not None and avg_best_value < stop_threshold:
        break

    delta_time = time.time() - start_time
    print('Finished episode {0}. '
          'Delta time since last episode: {1:.2f}'.format(ep, delta_time))

  # Computing metrics after training.
  compute_eval_metrics()

  # Policy evaluation for 100 episodes.
  # Outputs a convergence plot for the policy in the function.
  eval_utils.evaluate_agent(tf_env_eval,
                            agent.policy,
                            function,
                            dims,
                            env_eval_steps,
                            algorithm_name=algorithm_name,
                            save_to_file=True,
                            episodes=100,
                            save_dir=agent_dir)

  # Saving the learned policy.
  # Output directory: output/REINFORCE-{dims}D-{function.name}-{num}/policy
  training_utils.save_policy(agent_dir, agent.policy)

if __name__ == '__main__':
  reinforce_train(tff.Sphere(), 2,
                  stop_threshold=1e-2,
                  eval_interval=10,
                  training_episodes=500,
                  debug_summaries=False)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from django.conf.urls import patterns, url

from .views import PromoDetail, PromoList, ChannelPromoList

urlpatterns = patterns(
    '',
    url(
        r'^$',
        PromoList.as_view(),
        name='list_promos'
    ),
    url(
        r'^channel/(?P<channel__long_slug>[\w//-]+)$',
        ChannelPromoList.as_view(),
        name='channel_promo'
    ),
    url(
        r'^(?P<slug>[\w-]+)/(?P<result>[\w-]+)$',
        PromoDetail.as_view(),
        name='result_promo'
    ),
    url(
        r'^(?P<slug>[\w-]+)\.html$',
        PromoDetail.as_view(),
        name='open_promo'
    ),
)


import decimal
import io
from nose.tools import assert_equal
import utcdatetime

from os.path import join as pjoin

from snipe import WatchListSnipesParser, parse_datetime

def test_get_snipes():
    with io.open(pjoin('sample_data', 'watch_list.html')) as f:
        parser = WatchListSnipesParser(f.read())

    assert_equal(
        list(parser.get_snipes()), [
            ('113143572176', decimal.Decimal('7.00')),
            ('292631197506', decimal.Decimal('44.00')),
        ]
    )

def test_parse_snipe_note():
    TEST_CASES = [
        ('snipe: 45', decimal.Decimal(45.00)),
        ('snipe: 45.00', decimal.Decimal(45.00)),
        ('snipe: £45.00', decimal.Decimal(45.00)),
    ]

    for note, expected_amount in TEST_CASES:
        yield assert_equal, WatchListSnipesParser._parse_snipe_note(note), expected_amount

def test_parse_datetime():
    TEST_CASES = [
        (
            '(11 Jul, 2018\n09:58:34 BST)',
            utcdatetime.utcdatetime(2018, 7, 11, 8, 58, 34)
        ),
        (
            '(11 Jan, 2018\n09:58:34 GMT)',
            utcdatetime.utcdatetime(2018, 1, 11, 9, 58, 34)
        ),
    ]

    for string, expected in TEST_CASES:
        yield assert_equal, parse_datetime(string), expected


from lyse import *
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
import scipy.constants as constants
import AnalysisSettings
import SrConstants
from Subroutines.FitFunctions import gauss
from scipy import stats

camera = AnalysisSettings.Camera
pixelSize = SrConstants.pixelSizeDict[camera]

name = "Different_Loading_Curves_20_02_04"
path = "C:\\Experiments\\example_experiment\\grating_MOT\\AnalysisData\\" + name + ".h5"
run = Run(path)
df = data()

sequence_index = df["sequence_index"]
run_number = df["run number"]
filepath = df["filepath"]
norms = df['FitMOTLoad','fluorNorm']
BlueMOTLoadTime = df['BlueMOTLoadTime']
BlueMOTBeatnote = df['BlueMOTBeatnote']
BlueMOTPower_V = df['BlueMOTPower']
atomNumber = df["splice_gaussian_fit","atomNumber"]
loadingTimeConstant = df["FitMOTLoad", "loadingTimeConstant"]
loadingRate = df["FitMOTLoad", "loadingRate"]
widthX = df["splice_gaussian_fit","widthX"]
widthZ = df["splice_gaussian_fit","widthZ"]
TimeOfFlight = df["TimeOfFlight"]
avgWidth = df["splice_gaussian_fit","avgWidth"]
avgPeakOD = df["splice_gaussian_fit","avgPeakOD"]
avgNorm = 0.04
normFlrNum = loadingRate * loadingTimeConstant * avgNorm/ norms
avgNormRate = loadingRate * avgNorm / norms
SourceCurrent = df["SourceCurrent"]
runtime = df["run time"]

fluorCounts = []
fluorCountsCulled = range(len(filepath))
time = range(len(filepath))
timeCulled = range(len(filepath))

i = 0
for file in filepath:
    fluorCounts.append(np.zeros(1))
    with h5py.File(file,'a') as f:
        for result in f['results']['FitMOTLoad']['atomNummbers']:
            np.append(fluorCounts[i],result)
    i+= 1
print(fluorCounts)

FluorCounts = df["FitMOTLoad","atomNummbers"]
FluorCountsCulled = df["FitMOTLoad","atomNummbersCulled"]
time = df["FitMOTLoad", "time"]
timeCulled = df["FitMOTLoad", "timeCulled"]

def parabola(v,a,b,c):
    return a*(v-c)**2 +b

power_params = [-758.105, 92.576, 4.087]

gaussianWidth = 0.00914
Isat = 403
BlueMOTPower_mW = parabola(BlueMOTPower_V, *power_params)
I_Isat = (BlueMOTPower_mW*(10**-3)/(np.pi*gaussianWidth**2))/Isat

#Do Calculations

pOpt, pCov = curve_fit(gauss, BlueMOTBeatnote, atomNumber, p0 = (1000, 95, 10, 0))

PeakBeatnote = pOpt[1]

Detuning = BlueMOTBeatnote - PeakBeatnote

linewidth = 30.5

# SourceCurrentSing = [12,12.5,13,13.5,14]
DetuningSing = []
normFlrNumSing = []
normFlrNumSingDev =[]
#
for beatnote in Detuning:
    if beatnote not in DetuningSing:
        DetuningSing.append(beatnote)
#
#
atomNumberSing = []
atomNumberSingDev =[]
normTauSing = []
normTauSingDev =[]
normRateSing = []
normRateSingDev = []
#
for beatnote in DetuningSing:
    points1 = []
    #points2 = []
    #points3 = []
    for i in range(len(run_number)):
        if Detuning[i] == beatnote:
            points1.append(atomNumber[i])
            # points2.append(avgNormRate[i])
            # points3.append(normFlrNum[i])
    atomNumberSing.append(np.average(points1))
    atomNumberSingDev.append(stats.sem(points1))
    # normRateSing.append(np.average(points2))
    # normRateSingDev.append(np.std(points2))
    # normFlrNumSing.append(np.average(points3))
    # normFlrNumSingDev.append(np.std(points3))

corAtomNumberSing = np.array(atomNumberSing) * 1/(1 + 4*(np.array(DetuningSing)/linewidth)**2)
corAtomNumberSingDev = np.array(atomNumberSingDev) * 1/(1 + 4*(np.array(DetuningSing)/linewidth)**2)

# fig, ax1 = plt.subplots(1)
# #plot = ax.errorbar(BlueMOTPower_V, normFlrNum, fmt = 'bo', label = 'norms')
# #plot = plt.scatter(SourceCurrent, loadingTimeConstant)
# ax1.errorbar(DetuningSing, atomNumberSing, yerr = atomNumberSingDev,fmt = 'bo')
# ax1.errorbar(DetuningSing, corAtomNumberSing, yerr = corAtomNumberSingDev,fmt = 'go')
# ax1.plot(np.linspace(-18,9,100), gauss(np.linspace(-18,9,100) + PeakBeatnote,*pOpt), 'r-')

run.save_result('norms',norms)
run.save_result('atomNumber', atomNumber)
run.save_result("loadingRate", loadingRate)
run.save_result("loadingTimeConstant", loadingTimeConstant)
run.save_result("avgWidth", avgWidth)
run.save_result("avgPeakOD", avgPeakOD)
run.save_result("avgNorm", np.average(norms))
run.save_result("TimeOfFlight", TimeOfFlight)
run.save_result("widthX", widthX)
run.save_result("widthZ", widthZ)
run.save_result("FluorCounts",FluorCounts)
run.save_result("FluorCountsCulled", FluorCountsCulled)
run.save_result("time", time)
run.save_result("timeCulled", timeCulled)
#run.save_result("BlueMOTPower_V", BlueMOTPower_V)
# run.save_result("Detuning", Detuning)
# run.save_result("DetuningSing", DetuningSing)
# run.save_result("corAtomNumberSing", corAtomNumberSing)
# run.save_result("corAtomNumberSingDev",corAtomNumberSingDev)
# run.save_result("PeakBeatnote" ,PeakBeatnote)
# run.save_result("linewidth", linewidth)
# run.save_result("atomNumberSing",atomNumberSing)
# run.save_result("atomNumberSingDev",atomNumberSingDev)
# run.save_result("SourceCurrent", SourceCurrent)
run.save_result("normFlrNum", normFlrNum)
#run.save_result("normFlrNumSing", normFlrNumSing)
#run.save_result("normFlrNumSingDev", normFlrNumSingDev)
#run.save_result("normTauSing", normTauSing)
#run.save_result("normTauSingDev", normTauSingDev)
#run.save_result("normRateSing", normRateSing)
#run.save_result("normRateSingDev", normRateSingDev)
#run.save_result("SourceCurrentSing", SourceCurrentSing)
# run.save_result("gaussianWidth", gaussianWidth)
# run.save_result("Isat", Isat)
# run.save_result("BlueMOTPower_mW", BlueMOTPower_mW)
# run.save_result("I_Isat", I_Isat)
# run.save_result("avgNormRate", avgNormRate)
# run.save_result("avgNorm", avgNorm)

files = []
for i in range(len(sequence_index)):
    files.append(str(sequence_index[i]) + '_' + str(run_number[i]))
    run.save_result("files", files)
run.save_result("filepath", filepath)


'''
print out cmds for training and inference
'''

import argparse
import os
from DPR.dpr.utils.tasks import task_map, train_cluster_map, test_cluster_map
import random
import textwrap
from tqdm import tqdm

def wrap(cmd):
    '''
    wrap cmd
    '''
    bs = ' \\\n\t '
    return bs.join(textwrap.wrap(cmd,break_long_words=False,break_on_hyphens=False))

def get_cmds(args):

    # ================================== Train Stage ===================================
    # 1. random sample prompts and score data
    prompt_pool_dir = os.path.join(args.output_dir, 'prompt_pool')
    random_sample_dir = os.path.join(args.output_dir, 'find_random')
    scored_dir = os.path.join(args.output_dir, 'scored')

    exp_name = f'train_{args.train_clusters}_test_{args.test_clusters}'
    exp_path = os.path.join(args.output_dir, 'experiment', exp_name)
    os.makedirs(exp_path, exist_ok=True)

    random_port = random.randint(21966,25000)

    if args.train_clusters is None:
        clusters = list(train_cluster_map.keys())
    else:
        clusters = args.train_clusters.split('+')
    train_cmd_list=[]
    for cluster in tqdm(clusters):
        for task in train_cluster_map[cluster]:
            echo_cmd = f'echo "scoring {task} task of {cluster} cluster..."'
            task_cls = task_map.cls_dic[task]()
            prompt_pool_path = os.path.join(prompt_pool_dir, cluster, task+'_prompts.json')
            random_sample_path = os.path.join(random_sample_dir, cluster, task+'_random_samples.json')
            find_random_cmd=\
                f'python find_random.py output_path=$PWD/{random_sample_path} \
                task_name={task} +ds_size={args.ds_size} L={task_cls.finder_L} \
                prompt_pool_path=$PWD/{prompt_pool_path} cache_dir=$PWD/{args.cache_dir}\
                hydra.run.dir=$PWD/{exp_path}'

            scored_train_path = os.path.join(scored_dir, cluster, task+'_scored_train.json')
            scored_valid_path = os.path.join(scored_dir, cluster, task+'_scored_valid.json')
            run_scorer_cmd = \
                f'accelerate launch --multi_gpu --num_processes {args.gpus} --main_process_port {random_port} \
                scorer.py example_file=$PWD/{random_sample_path} \
                output_train_file=$PWD/{scored_train_path} \
                output_valid_file=$PWD/{scored_valid_path} \
                batch_size={task_cls.run_scorer_bsz} task_name={task}  \
                model_name={args.scr_model} \
                prompt_pool_path=$PWD/{prompt_pool_path} cache_dir=$PWD/{args.cache_dir} \
                hydra.run.dir=$PWD/{exp_path}'

            train_cmd_list += [echo_cmd, find_random_cmd, run_scorer_cmd]

    # 2. train a retriever:
    echo_cmd = f'echo "start training the retriever..."'
    train_retriever_cmd = \
        f'python DPR/train_dense_encoder.py train_datasets=[uprise_dataset] dev_datasets=[uprise_valid_dataset] \
        train=biencoder_uprise output_dir=$PWD/{exp_path} \
        datasets.train_clusters={args.train_clusters} \
        datasets.train_file=$PWD/{scored_dir} \
        datasets.valid_file=$PWD/{scored_dir} \
        datasets.hard_neg=true datasets.multi_task={args.multi_task} \
        datasets.top_k={args.retriever_top_k} train.hard_negatives={args.retriever_top_k} \
        train.batch_size={args.retriever_bsz} \
        train.num_train_epochs={args.retriever_epoch} \
        datasets.prompt_pool_path=$PWD/{prompt_pool_dir} \
        datasets.prompt_setup_type={args.retriever_prompt_setup} \
        datasets.task_setup_type=q encoder.cache_dir=$PWD/{args.cache_dir}\
        hydra.run.dir=$PWD/{exp_path}'

    train_cmd_list += [echo_cmd, train_retriever_cmd]

    # write train cmds in train.sh
    train_cmd_list = [wrap(cmd) for cmd in train_cmd_list]

    # write run.sh
    with open(f"{exp_path}/train.sh","w") as f:
        f.write("\n\n".join(train_cmd_list))
    print('saved training cmds to: ', f"{exp_path}/train.sh")

    # ================================== Inference Stage ===================================
    inference_cmd_list = []
    # 1. encode the whole prompt pool, using prompt encoder of the trained retriever
    echo_cmd = f'echo "encoding the whole prompt pool..."'
    gen_emb_cmd = \
        f"python DPR/generate_dense_embeddings.py model_file=$PWD/{exp_path}/dpr_biencoder.best_valid \
        ctx_src=dpr_uprise shard_id=0 num_shards=1 \
        out_file=$PWD/{exp_path}/dpr_enc_index \
        ctx_sources.dpr_uprise.train_clusters={args.train_clusters} \
        ctx_sources.dpr_uprise.prompt_pool_path=$PWD/{prompt_pool_dir} \
        ctx_sources.dpr_uprise.prompt_setup_type={args.retriever_prompt_setup} \
        encoder.cache_dir=$PWD/{args.cache_dir} \
        hydra.run.dir=$PWD/{exp_path}"

    inference_cmd_list += [echo_cmd, gen_emb_cmd]

    def get_inference_cmd(num_prompts=3, retriever='uprise'):

        assert retriever in [None, 'Random', 'Bm25', 'Sbert', 'Uprise']
        random = True if retriever == "random" else False

        echo_cmd = f'echo "running inference on {task} task of {cluster} cluster with {retriever} retriever..."'
        pred_outpath = os.path.join(exp_path, f'preds_for_{cluster}', f'{task}_prompts{args.num_prompts}_retriever{retriever}_preds.json')

        run_inference_cmd = \
            f"accelerate launch --num_processes 1  --main_process_port {random_port} \
            inference.py prompt_file=$PWD/{retrieve_prompts_outpath} \
            task_name={task} \
            output_file=$PWD/{pred_outpath} \
            res_file=$PWD/{eval_res_outpath} \
            batch_size={args.inference_bsz} \
            train_clusters={args.train_clusters} \
            model_name={args.inf_model} \
            prompt_pool_path=$PWD/{prompt_pool_dir} \
            num_prompts={num_prompts} \
            random_sample={random} random_seed=42 \
            cache_dir=$PWD/{args.cache_dir} \
            hydra.run.dir=$PWD/{exp_path}"

        return [echo_cmd, run_inference_cmd]

    # 2. retrieve positive prompts from the prompt pool, for each task in the testing clusters:
    test_clusters = args.test_clusters.split('+')
    for cluster in test_clusters:
        eval_res_outpath = os.path.join(exp_path, f'eval_res_for_{cluster}.txt')
        for task in test_cluster_map[cluster]:
            echo_cmd = f'echo "uprise retrieves on {task} task of {cluster} cluster..."'
            retrieve_prompts_outpath = os.path.join(exp_path, f'uprise_prompts_for_{cluster}', f'{task}_prompts.json')
            retrieve_prompts_cmd = \
                f'python DPR/dense_retriever.py model_file=$PWD/{exp_path}/dpr_biencoder.best_valid \
                qa_dataset=qa_uprise ctx_datatsets=[dpr_uprise] \
                encoded_ctx_files=["$PWD/{exp_path}/dpr_enc_index_*"]\
                out_file=$PWD/{retrieve_prompts_outpath} \
                datasets.qa_uprise.task_name={task} \
                datasets.qa_uprise.task_setup_type=q  \
                datasets.qa_uprise.cache_dir=$PWD/{args.cache_dir} \
                n_docs={args.num_prompts} \
                ctx_sources.dpr_uprise.prompt_pool_path=$PWD/{prompt_pool_dir} \
                ctx_sources.dpr_uprise.train_clusters={args.train_clusters} \
                ctx_sources.dpr_uprise.prompt_setup_type={args.retriever_prompt_setup} \
                encoder.cache_dir=$PWD/{args.cache_dir} \
                hydra.run.dir={exp_path}'
            inference_cmd_list += [echo_cmd, retrieve_prompts_cmd]

            # vanilla zero shot
            inference_cmd_list += get_inference_cmd(num_prompts=0, retriever=None)

            # uprise zero shot
            inference_cmd_list += get_inference_cmd(num_prompts=args.num_prompts, retriever='Uprise')

            # Ablations: replace uprise retriever with random, bm25 and sbert
            if args.retrieve_random:
                inference_cmd_list += get_inference_cmd(num_prompts=args.num_prompts, retriever='Random')
            if args.retrieve_bm25:
                echo_cmd = f'echo "bm25 retrieves on {task} task of {cluster} cluster..."'
                retrieve_prompts_outpath = os.path.join(exp_path, f'bm25_prompts_for_{cluster}', f'{task}_prompts.json')
                retrieve_bm25_prompts_cmd = \
                    f'python retrieve_bm25.py \
                    train_clusters={args.train_clusters} \
                    task_name={task} cache_dir=$PWD/{args.cache_dir} \
                    prompt_pool_path=$PWD/{prompt_pool_dir} \
                    out_file=$PWD/{retrieve_prompts_outpath} \
                    prompt_setup_type={args.retriever_prompt_setup} n_docs={args.num_prompts} \
                    hydra.run.dir=$PWD/{exp_path} '
                inference_cmd_list += [echo_cmd, retrieve_bm25_prompts_cmd]
                inference_cmd_list += get_inference_cmd(num_prompts=args.num_prompts, retriever='Bm25')
            if args.retrieve_sbert:
                echo_cmd = f'echo "sbert retrieves on {task} task of {cluster} cluster..."'
                retrieve_prompts_outpath = os.path.join(exp_path, f'sbert_prompts_for_{cluster}', f'{task}_prompts.json')
                retrieve_sbert_prompts_cmd = \
                    f'python retrieve_sbert.py \
                    train_clusters={args.train_clusters} \
                    task_name={task} cache_dir=$PWD/{args.cache_dir} \
                    prompt_pool_path=$PWD/{prompt_pool_dir} \
                    out_file=$PWD/{retrieve_prompts_outpath} \
                    prompt_setup_type={args.retriever_prompt_setup} n_docs={args.num_prompts} \
                    hydra.run.dir=$PWD/{exp_path} '
                inference_cmd_list += [echo_cmd, retrieve_sbert_prompts_cmd]
                inference_cmd_list += get_inference_cmd(num_prompts=args.num_prompts, retriever='Sbert')

    inference_cmd_list = [wrap(cmd) for cmd in inference_cmd_list]

    # write run.sh
    with open(f"{exp_path}/inference.sh","w") as f:
        f.write("\n\n".join(inference_cmd_list))
    print('saved inference cmds to: ', f"{exp_path}/inference.sh")

    return

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--output_dir',
                        type=str, help='Directory for saving all the intermediate and final outputs.',
                        default='my_data')
    parser.add_argument('--cache_dir',
                        type=str, help='Directory for caching the huggingface models and datasets.',
                        default='../cache')
    parser.add_argument('--gpus',
                        type=int, help='number of gpus to use',
                        default=8)

    # training
    parser.add_argument('--train_clusters',
                        type=str,
                        help='a string concatenating task clusters for training, \
                            e.g., `nli+common_reason` means nli and common_reason task clusters \
                            all supoorted clusters are in DPR.dpr.utils.tasks.train_cluster_map \
                            clusters=`all supported clsuters` when the passed value is None',
                        default=None)
    parser.add_argument('--retriever_prompt_setup',
                        type=str,
                        help='setup type of prompt, recommend setting as `qa` for cross-task training \
                            and `q` for task-specific training',
                        default="qa")
    parser.add_argument('--ds_size',
                        type=int,
                        help='number of maximum data examples sampled from each training dataset',
                        default=10000)
    parser.add_argument('--scr_model',
                        type=str,
                        help='Huggingface model for scoring data',
                        default="EleutherAI/gpt-neo-2.7B")
    parser.add_argument("--multi_task",
                        action="store_true",
                        help="True for multi-task and False for task-specific, \
                            the difference reflects on the sampling of negative prompts ONLY \
                            refer to `UpriseDataset` in `DPR/dpr/data/biencoder_data.py` for details")
    parser.add_argument('--retriever_top_k',
                        type=int,
                        help='number of k (hard) negatives for training the retriever',
                        default=20)
    parser.add_argument('--retriever_bsz',
                        type=int,
                        help='sum of batch size of all gpus, NOT per gpu',
                        default=16)
    parser.add_argument('--retriever_epoch',
                        type=int,
                        help='maximum training epoch, recommend setting as `3` when cross-task training, \
                             and `10` when task-specific training',
                        default=3)

    # inference
    parser.add_argument('--inf_model',
                        type=str,
                        help='Huggingface model for inference',
                        default="EleutherAI/gpt-neo-2.7B")
    parser.add_argument('--test_clusters',
                        type=str,
                        help='a string concatenating task clusters for training, \
                            e.g., `nli+common_reason` means nli and common_reason task clusters \
                            all supoorted clusters are in DPR.dpr.utils.tasks.test_cluster_map',
                        default="nli+common_reason")
    parser.add_argument('--num_prompts',
                        type=int,
                        help='maximum number of retrieved prompts to be concatenated before the task input',
                        default=3)
    parser.add_argument('--retrieve_random',
                        action="store_true",
                        help='whether to random retrieve from our prompt pool, and run a baseline')
    parser.add_argument('--retrieve_bm25',
                        action="store_true",
                        help='whether to use bm25 retriever to retrieve from our prompt pool, and run a baseline')
    parser.add_argument('--retrieve_sbert',
                        action="store_true",
                        help='whether to use sbert to retrieve from our prompt pool, and run a baseline')
    parser.add_argument('--inference_bsz',
                        type=int,
                        help='sum of batch size of all gpus, NOT per gpu',
                        default=1)

    args = parser.parse_args()

    get_cmds(args)


"""A version of BAX pore assembly where subunits are added to the growing
complex one at a time (contrast with bax_pore.py). Also implements cargo
transport (Smac).
"""

from __future__ import print_function
from pysb import *
from pysb.macros import assemble_pore_sequential, pore_transport, pore_species

Model()

# s1,s2 are ring-formation sites; t is the transport (cargo binding) site
Monomer('Bax', ['s1', 's2', 't'])
Annotation(Bax, 'http://identifiers.org/uniprot/Q07812')
# loc is the location, (m)itochondrion or (c)ytosol; t is the transport site
Monomer('Smac', ['loc', 't'], {'loc': ['m','c']})
Annotation(Smac, 'http://identifiers.org/uniprot/Q9NR28')

Parameter('Bax_0', 8e4)
Parameter('Smac_0', 1e5)
for p in Bax_0, Smac_0:
    Annotation(p, 'http://identifiers.org/doi/10.1371/journal.pcbi.1002482',
               'isDescribedBy')

# Start with monomeric Bax and Smac in the mitochondrion
Initial(Bax(s1=None, s2=None, t=None), Bax_0)
Initial(Smac(loc='m', t=None), Smac_0)

# Maximum number of subunits in a pore
max_size = 6
# Size at which pores are "competent" to transport cargo
min_transport_size = 4

# Build handy rate "sets" (in this formulation, rates don't vary with pore size)
assembly_rates = [[2e-4, 1e-3]] * (max_size - 1)
transport_rates = [[3e-5, 1e-3, 1e1]] * (max_size - min_transport_size + 1)

# Assemble the pore
# (specify t=None so the pore can't fall apart while it's bound to cargo)
assemble_pore_sequential(Bax(t=None), 's1', 's2', max_size, assembly_rates)
# Transport Smac
pore_transport(Bax, 's1', 's2', 't', min_transport_size, max_size,
               Smac(loc='m'), 't', Smac(loc='c'), transport_rates)

# Add an observable for each pore size
for size in range(1, max_size + 1):
    Observable('Bax%d' % size, pore_species(Bax, 's1', 's2', size))
# Observe unbound Smac in each compartment
Observable('mSmac', Smac(loc='m', t=None))
Observable('cSmac', Smac(loc='c'))

if __name__ == '__main__':
    print(__doc__, "\n", model)
    print("""
NOTE: This model code is designed to be imported and programatically
manipulated, not executed directly. The above output is merely a
diagnostic aid. Please see run_bax_pore_sequential.py for example
usage.""")


#!/usr/bin/env python
import time
import openstack
NODE_COUNT = 43
def get_connection():
    # openstack.enable_logging(debug=True)
    conn = openstack.connect()
    return conn
def main():
    conn = get_connection()
    for i in range(NODE_COUNT):
        name = "euclid-ral_compute_%d" % i
        conn.delete_server(name, wait=True)
    for i in range(NODE_COUNT):
        name = "euclid-ral_compute_%d" % i
        conn.delete_volume(name, wait=True)

if __name__ == '__main__':
    main()


from django.urls import path, include
from website.views import home, blog, perfil, login, acessar, cadastrar

urlpatterns = [
    path('', home),
    path('blog', blog),
    path('login', login),
    path('acessar', acessar),
    path('perfil', perfil),
    path('cadastrar', cadastrar),
]


import base64
import logging
import sys
from string import digits, ascii_uppercase
import traceback
import time

from ins import *
from disasm import *
import gpu

class Tape:
    '''
    Tape is just a looped array of instructions.
    '''

    @classmethod
    def from_inss(cls, inss):
        '''Create tape from instructions'''
        ans = cls()
        ans.data = inss
        return ans

    @classmethod
    def from_bytes(cls, rom):
        ans = cls()
        # Each instruction is 24 bits, which is 3 bytes
        ans.data = []
        for i in range(0, len(rom), 3):
            ins_bytes = rom[i: i + 3]
            ans.data.append(Ins.from_bytes(ins_bytes))

        return ans

    def to_bytes(self):
        ans = [ins.to_bytes() for ins in self.data]
        return b''.join(ans)

    def __getitem__(self, i):
        return self.data[i]

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

    def __repr__(self):
        return str(self.data)

class Regs:
    '''
    64 registers that are 6-bit elements each.
    '''

    def __init__(self):
        self.data = [0] * 64

    def dump(self):
        n = 8
        for i in range(0, len(self.data), n):
            xs = self.data[i:i + n]
            xs = [str(x).rjust(2) for x in xs]
            print(' '.join(xs))

    def __getitem__(self, i):
        assert 0 <= self.data[i] < 64
        return self.data[i]

    def __setitem__(self, i, v):
        if i != 0:
            self.data[i] = v & 0o77

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

class Mem:
    '''
    2**18 16-bit words.
    '''

    def __init__(self):
        self.data = [0] * (2**18)
        self.addr = 0  # 18-bit index for `self.data`

    def dump(self):
        # Warning: very big
        n = 8
        for i in range(0, len(self.data), n):
            xs = self.data[i:i + n]
            xs = [str(x).rjust(2) for x in xs]
            print(' '.join(xs))

    def __getitem__(self, i):
        assert 0 <= self.data[i] < 64
        return self.data[i]

    def __setitem__(self, i, v):
        self.data[i] = v & 0o77

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

def twos_comp(n, bits=6):
    mask = (1 << bits) - 1
    neg = n ^ mask
    return (neg + 1) & mask

def to_int(n, bits=6):
    '''Turn a two's complement number into a Python int'''
    if (n & (1 << (bits - 1))) != 0:
        n = n - (1 << bits)
    return n

def bit_string(n, bits=6):
    return format(n % (1 << bits), '0{}b'.format(bits))

def from_int(n, bits=6):
    '''Turn a Python int to a two''s complement number'''
    return int(bit_string(n), 2)

def sar(n, d, bits=6):
    '''Arithmetic right shift'''
    sign = n & (1 << (bits - 1))
    if sign == 0:
        return n >> d
    else:
        inv_s = max(bits - d, 0)
        sign_mask = (1 << bits) - (1 << inv_s)
        return (n >> d) | sign_mask

def rol(n, d, bits=6):
    '''Rotate bits left'''
    s = bit_string(n)
    d = d % bits
    s = s[-d:] + s[:-d]
    return int(s, 2)

# `x` is a placeholder for an invalid character
serial_dict = digits + ascii_uppercase + ' +-*/<=>()[]{}#$_?|^&!~,.:\nx'

def chr_from_serial(n):
    return serial_dict[n]

def str_from_serial(ns):
    return ''.join([chr_from_serial(n) for n in ns])

def serial_from_chr(c):
    return serial_dict.index(c)

def serial_from_str(s):
    return bytearray([serial_from_chr(c) for c in s])

class Emu:
    def __init__(self, use_gpu=False):
        self.regs = Regs()
        self.mem = Mem()

        self.use_gpu = use_gpu
        if use_gpu:
            self.gpu = gpu.Gpu()
            self.gpu.start()

        self.pc = 0
        self.halted = False
        self.cf = False
        self.clock = 0

        self.buffer = ''
        self.out = sys.stdout

    @classmethod
    def from_filename(cls, filename, use_gpu=False):
        ans = cls(use_gpu)
        s = open(filename).read().strip()
        s = base64.b64decode(s)
        ans.tape = Tape.from_bytes(s)
        return ans

    # Ops

    def op_hlt(self, ins):
        self.halted = True

    # Arithmetic and logic

    def op_add(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] + self.regs[rb]

    def op_addi(self, ins):
        rd, ra, ib = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] + ib

    def op_sub(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] + twos_comp(self.regs[rb])

    def op_or(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] | self.regs[rb]

    def op_ori(self, ins):
        rd, ra, ib = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] | ib

    def op_xor(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] ^ twos_comp(self.regs[rb])

    def op_xori(self, ins):
        rd, ra, ib = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] ^ ib

    def op_and(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] & twos_comp(self.regs[rb])

    def op_andi(self, ins):
        rd, ra, ib = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] & ib

    def op_shl(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] << self.regs[rb]

    def op_shr(self, ins):
        rd, ra, rb = ins.a, ins.b, ins.c
        self.regs[rd] = self.regs[ra] >> self.regs[rb]

    # Comparison

    def op_cmp(self, ins):
        (cmp_type, cm, a, b) = ins.as_cmp()

        # Determine left and right sides of comparison
        if cmp_type == CmpType.RA_RB:
            left = self.regs[a]
            right = self.regs[b]
        elif cmp_type == CmpType.RB_RA:
            left = self.regs[b]
            right = self.regs[a]
        elif cmp_type == CmpType.RA_IB:
            left = self.regs[a]
            right = b
        elif cmp_type == CmpType.IA_RB:
            left = a
            right = self.regs[b]

        # Do comparison
        if cm == Cm.TR:
            self.cf = True
        elif cm == Cm.FA:
            self.cf = False
        elif cm == Cm.EQ:
            self.cf = left == right
        elif cm == Cm.NE:
            self.cf = left != right
        elif cm == Cm.SL:
            self.cf = to_int(left) < to_int(right)
        elif cm == Cm.SG:
            self.cf = to_int(left) > to_int(right)
        elif cm == Cm.UL:
            self.cf = left < right
        elif cm == Cm.UG:
            self.cf = left > right
        else:
            raise ValueError('Unhandled cm: {}'.format(cm))

    def op_shi(self, ins):
        (shi_type, rd, ra, ib) = ins.as_shi()

        if shi_type == ShiType.SHLI:
            self.regs[rd] = self.regs[ra] << ib
        elif shi_type == ShiType.SHRI:
            self.regs[rd] = self.regs[ra] >> ib
        elif shi_type == ShiType.SARI:
            self.regs[rd] = sar(self.regs[ra], ib)
        elif shi_type == ShiType.ROLI:
            self.regs[rd] = rol(self.regs[ra], ib)
        else:
            raise ValueError('Unhandled shi_type: {}'.format(shi_type))

    def op_ld(self, ins):
        rd, ra, ib = ins.a, ins.b, ins.c
        rs = (self.regs[ra] + ib) & 0o77
        self.regs[rd] = self.regs[rs]

    def op_st(self, ins):
        rs, ra, ib = ins.a, ins.b, ins.c
        rd = (self.regs[ra] + ib) & 0o77
        self.regs[rd] = self.regs[rs]

    def op_fm(self, ins):
        (fm_type, pr, rd, ra) = ins.as_fm()

        # 12 bit buffer
        ans = (self.regs[rd] * self.regs[ra]) & 0o7777
        if fm_type == FmType.U:
            ans = ans >> pr
        else:
            ans = sar(ans, pr, bits=12)

        # `ans` will be deduce back to 6 bits when stored
        self.regs[rd] = ans

    def op_lbl(self, ins):
        # Do nothing
        pass

    def op_jup(self, ins, reverse=False):
        key = (ins.partial_jump_key(), self.regs[ins.c])

        # Search for the label matching key
        start = self.pc
        i = start
        while True:
            if reverse:
                i = (i + 1) % len(self.tape)
            else:
                i = (i - 1) % len(self.tape)

            if i == start:
                raise ValueError('Couldn''t find label: {}'.format(key))

            ch_ins = self.tape[i]  # Check instruction
            if ch_ins.op == Op.LBL and \
                    self.should_execute(ch_ins) and \
                    ch_ins.label_key() == key:
                # We always increment the PC after executing an instruction.
                # To offset that, we subtract 1 here.
                self.pc = i - 1
                return

    def op_jdn(self, ins):
        self.op_jup(ins, reverse=True)

    def get_input(self):
        s = input('> ')
        self.buffer += s

    def op_io_serial_incoming(self, ins):
        (rd, ix_, rs_) = ins.as_io()

        # Optionally read more input if we don't have any in the buffer
        if len(self.buffer) == 0:
            self.get_input()

        # Send the length of buffer
        self.regs[rd] = from_int(len(self.buffer))

    def op_io_serial_read(self, ins):
        (rd, ix_, rs_) = ins.as_io()

        # Optionally read more input if we don't have any in the buffer
        if len(self.buffer) == 0:
            self.get_input()

        # Pop the first char and send it
        c = self.buffer[0]
        self.buffer = self.buffer[1:]
        self.regs[rd] = from_int(serial_from_chr(c))

    def op_io_serial_write(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        c = chr_from_serial(self.regs[rs])
        self.out.write(c)
        self.out.flush()

    def reset_clock(self):
        self.start = time.time()
        self.clock == 0
        if self.use_gpu:
            should_halt = self.gpu.update()
            if should_halt:
                self.halted = True

    def op_io_clock_lo_cs(self, ins):
        (rd, ix_, rs) = ins.as_io()
        if rs == 0:
            # Get lower 6 bits of clock
            self.regs[rd] = self.clock & 0o77
        else:
            self.reset_clock()

    def op_io_clock_hi_cs(self, ins):
        (rd, ix_, rs) = ins.as_io()
        if rs == 0:
            # Get upper 6 bits of clock
            self.regs[rd] = (self.clock & 0o7700) >> 6
        else:
            self.reset_clock()

    def op_io_mem_addr_lo(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        # Clear lower bits without clearing upper bits
        mask = (0o77 << 6) + (0o77 << (6 * 2))
        self.mem.addr &= mask
        self.mem.addr |= self.regs[rs]

    def op_io_mem_addr_mid(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        mask = 0o77 + (0o77 << (6 * 2))
        self.mem.addr &= mask
        self.mem.addr |= (self.regs[rs] << 6)

    def op_io_mem_addr_hi(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        mask = 0o77 + (0o77 << 6)
        self.mem.addr &= mask
        self.mem.addr |= (self.regs[rs] << (6 * 2))

    def op_io_mem_read(self, ins):
        (rd, ix_, rs_) = ins.as_io()
        self.regs[rd] = self.mem[self.mem.addr]
        self.mem.addr = (self.mem.addr + 1) % len(self.mem)

    def op_io_mem_write(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        self.mem[self.mem.addr] = self.regs[rs]
        self.mem.addr = (self.mem.addr + 1) % len(self.mem)

    def op_gpu_x(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        self.gpu.set_x(self.regs[rs])

    def op_gpu_y(self, ins):
        (rd_, ix_, rs) = ins.as_io()
        self.gpu.set_y(self.regs[rs])

    def op_gpu_draw(self, ins):
        (rd_, ix, rs) = ins.as_io()
        self.gpu.draw(self.regs[rs])

    op_io_switch = {
        IoDevice.SERIAL_INCOMING: op_io_serial_incoming,
        IoDevice.SERIAL_READ: op_io_serial_read,
        IoDevice.SERIAL_WRITE: op_io_serial_write,
        IoDevice.CLOCK_LO_CS: op_io_clock_lo_cs,
        IoDevice.CLOCK_HI_CS: op_io_clock_hi_cs,
        IoDevice.MEM_ADDR_HI: op_io_mem_addr_hi,
        IoDevice.MEM_ADDR_MID: op_io_mem_addr_mid,
        IoDevice.MEM_ADDR_LO: op_io_mem_addr_lo,
        IoDevice.MEM_READ: op_io_mem_read,
        IoDevice.MEM_WRITE: op_io_mem_write,
        IoDevice.GPU_X: op_gpu_x,
        IoDevice.GPU_Y: op_gpu_y,
        IoDevice.GPU_DRAW: op_gpu_draw,
    }

    def op_io(self, ins):
        (rd_, ix, rs_) = ins.as_io()
        if ix in Emu.op_io_switch:
            op_io_func = Emu.op_io_switch[ix]
            op_io_func(self, ins)
        else:
            logging.warning('Unknown IO device')
            self.halted = True

    op_switch = {
        Op.HLT: op_hlt,
        Op.ADD: op_add,
        Op.ADDI: op_addi,
        Op.SUB: op_sub,
        Op.OR: op_or,
        Op.ORI: op_ori,
        Op.XOR: op_xor,
        Op.XORI: op_xori,
        Op.AND: op_and,
        Op.ANDI: op_andi,
        Op.SHL: op_shl,
        Op.SHR: op_shr,
        Op.CMP: op_cmp,
        Op.SHI: op_shi,
        Op.LD: op_ld,
        Op.ST: op_st,
        Op.FM: op_fm,
        Op.LBL: op_lbl,
        Op.JUP: op_jup,
        Op.JDN: op_jdn,
        Op.IO: op_io,
    }

    def should_execute(self, ins):
        return \
            (ins.cond == Cond.UN) or \
            (ins.cond == Cond.TR and self.cf) or \
            (ins.cond == Cond.FA and not self.cf)

    def execute(self, ins):
        if self.should_execute(ins):
            if ins.op in Emu.op_switch:
                op_func = Emu.op_switch[ins.op]
                op_func(self, ins)
            else:
                raise ValueError('Unhandled op: {}'.format(ins.op))

    def run(self, log_inss=False):
        if log_inss:
            # Keep a log of instructions executed
            inss_log = []

        self.start = time.time()
        self.clock = 0

        while not self.halted:
            ins = self.tape[self.pc]

            if log_inss:
                inss_log.append(self.pc)

            # 19483
            self.execute(ins)
            self.pc = (self.pc + 1) % len(self.tape)  # Tape is looped

            now = time.time()
            elapsed = now - self.start
            elapsed = round(elapsed, 2)
            elapsed = int(elapsed * 100)

            # Clock does not wrap
            self.clock = min(elapsed, 0o7777)

        if self.use_gpu:
            self.gpu.quit()

        if log_inss:
            return inss_log

    def execute_dbg_cmd(self, cmd):
        '''Return value: whether to go to the next instruction or not'''
        cmd = cmd.split()
        if len(cmd) == 0:
            print('No cmd')
            return False

        try:
            if cmd[0] == 'p':
                i = int(cmd[1])
                print('regs[{}] = {}'.format(i, self.regs[i]))
            elif cmd[0] == 'x':
                i = int(cmd[1])
                print('mem[{}] = {}'.format(i, self.mem[i]))
            elif cmd[0] == 'xi':
                print('mem.addr = {}'.format(self.mem.addr))
            elif cmd[0] == 'reg':
                self.regs.dump()
            elif cmd[0] == 'l':
                inss = self.tape[self.pc: self.pc + 5]
                disasm = Disasm.disasm_tape(inss)
                print(disasm)
            elif cmd[0] in {'n', 's'}:
                return True
            elif cmd[0] == 'q':
                self.halted = True
                return True
            elif cmd[0] == 'c':
                self.stepping = False
                return True
            else:
                print('Unknown cmd')
                return False
        except ValueError:
            print('Error processing cmd')
            return False

    def run_dbg(self):
        self.clock = 0
        self.stepping = True
        prev_cmd = None

        while not self.halted:
            ins = self.tape[self.pc]
            print('{}: {}'.format(self.pc, Disasm.disasm(ins)))

            if self.stepping:
                while True:
                    cmd = input('edb> ')
                    if cmd == '' and prev_cmd:
                        cmd = prev_cmd

                    go_next = self.execute_dbg_cmd(cmd)
                    prev_cmd = cmd
                    if go_next:
                        break

            self.execute(ins)
            self.pc = (self.pc + 1) % len(self.tape)  # Tape is looped

            # Use fake clock where each instruction takes one centisecond
            self.clock = min(self.clock + 1, 0o7777)

    def save_tape(self, filename):
        with open(filename, 'w') as f:
            b = self.tape.to_bytes()
            s = base64.b64encode(b).decode()
            f.write(s)

if __name__ == '__main__':
    assert len(sys.argv) >= 2
    filename = sys.argv[1]

    emu = Emu.from_filename(filename, use_gpu=True)

    try:
        emu.run()
    except KeyboardInterrupt:
        traceback.print_exc(file=sys.stdout)
        # Print the PC before we quit
        print('EMU PC:', emu.pc)
        sys.exit(1)


# A very simple Flask Hello World app for you to get started with...
import logging, sys
from flask import Flask, request
from CrapBot import Bot

crap_bot = Bot()
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

@app.route('/bot', methods=['POST', 'GET'])
def bot():
    update = request.json
    if update:
        crap_bot.handle(update)
    return 'Ok'

if __name__ == '__main__':
    from CrapBot import Logger
    Logger.warn('bot started')
    #from CrapBot.Api import set_webhook
    #r = set_webhook()
    #print(r)
    crap_bot.listen()


import re

pattern = r'gr.y'

if re.match(pattern, "greyyyy"):
    print('match1')
if re.match(pattern, 'grayy'):
    print('match2')

pattern1 = r'^gr.y$'

if re.match(pattern1, "grey"):
    print('match3')

pattern3 = r'[aucf]'

if re.match(pattern3, "ale"):
    print('match4')

pattern4 = r'[A-Z][a-z][0-9]'

if re.search(pattern4, 'Ca6'):
    print('match5')

pattern5 = r'[^A-Z]'

if re.search(pattern5, 'dgdfgadegd545454'):
    print('match6')

pattern6 = r'egg(spam)*'

if re.match(pattern6, 'egg'):
    print('match7')
if re.match(pattern6, 'eggspamspamspam'):
    print('match8')

pattern7 = r'g+'

if re.match(pattern7, 'gggggggg'):
    print('match9')

pattern8 = r'a(bc)(de)(f(g)h)i'
m = re.match(pattern8, "abcdefghifsdgfsg")
print(m.group())
print(m.group(0))
print(m.group(2))
print(m.groups())

pattern9 = r'gr(e|a)y'

if re.match(pattern9, "gray"):
    print('match10')

pattern10 = r'(.+)\1'

if re.match(pattern10, 'wordword'):
    print('match11')


import os, sys
import torch
import torchvision as tv
import cv2
import numpy as np
from matplotlib import pyplot as plt

from dataset import coco_labels

def box_cxcywh_to_xyxy(box):
    """
    Convert bounding box from center-size to xyxy format.
    :param box: bounding box in center-size format
    :return: bounding box in xyxy format
    """
    x_c, y_c, w, h = box[...,0], box[...,1], box[...,2], box[...,3]
    b = [
        (x_c - (w*0.5)), (y_c - (h*0.5)),
        (x_c + (w*0.5)), (y_c + (h*0.5)),
    ]
    return torch.stack(b, dim=-1)

def box_iou(b1, b2):
    """returns the iou between the set of boxes 1 and boxes 2

    assumes that b1 and b2 are in xyxy format

    Args:
        b1 (torch.Tensor): first set of boxes
        b2 (torch.Tensor): second set of boxes
    Returns:
        tuple(torch.Tensor, torch.Tensor): the iou and the union of the boxes in [N, M] format
    """

    area1 = (b1[...,2] - b1[...,0]) * (b1[...,3] - b1[...,1])
    area2 = (b2[...,2] - b2[...,0]) * (b2[...,3] - b2[...,1])

    lt = torch.max(b1[:, None, :2], b2[:, :2]) # [N,M,2]
    rb = torch.min(b1[:, None, 2:], b2[:, 2:]) # [N,M,2]

    wh = (rb - lt).clamp(min=0) # [N,M,2]

    inter = wh[...,0] * wh[...,1] # [N,M]
    union = area1[:, None] + area2 - inter # [N,1]

    iou = inter / (union + 1e-6)
    return iou, union

def box_giou(b1, b2):
    """returns the giou between the set of boxes 1 and boxes 2

    assumes that b1 and b2 are in xyxy format

    Args:
        b1 (torch.Tensor): first set of boxes
        b2 (torch.Tensor): second set of boxes
    Returns:
        tuple(torch.Tensor, torch.Tensor): the giou and iou of the boxes in [N, M] format
    """

    iou, union = box_iou(b1, b2)

    lt = torch.min(b1[:, None, :2], b2[:, :2]) # [N,M,2]
    rb = torch.max(b1[:, None, 2:], b2[:, 2:]) # [N,M,2]

    wh = (rb - lt).clamp(min=0) # [N,M,2]
    area = wh[...,0] * wh[...,1] # [N,M]

    giou = iou - (area - union) / (area + 1e-6)

    return giou, iou