import pydantic.schema
## Imports
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter    # To print to tensorboard for visualization

print(torch.__version__)

## Build Simple CNN

class CNN(nn.Module):
    def __init__(self, in_channels=3, num_classes=10):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=6, kernel_size=(5,5), stride=1, padding=0)       #kernel_size, stride, padding can be given 2 arguments or single can also work
        self.pool1 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0)
        self.pool2 = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84,10)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        # print("Conv1 in Layer1 : ", x.shape)
        x = self.pool1(x)
        # print("Pool1 in Layer1 : ", x.shape)
        x = F.relu(self.conv2(x))
        # print("Conv2 in Layer1 : ", x.shape)
        x = self.pool2(x)
        # print("Pool2 in Layer1 : ", x.shape)
        x = x.reshape(x.shape[0], -1)
        # print("Reshape : ", x.shape)
        x = self.fc1(x)
        # print("FC1 : ", x.shape)
        x = self.fc2(x)
        # print("FC2 : ", x.shape)
        x = self.fc3(x)
        # print("FC3 : ", x.shape)
        # m = nn.Softmax(dim=1)
        # x = m(self.fc3)
        return x

## Check Model
# model = CNN()
# x = torch.randn(4, 3, 32, 32)    #
# print('Model shape is : ', model(x).shape)           # Shapes from forward pass are printed only if model.shape is called

## Set Device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Device Being Used : ", device)

## Hyperparameters
learning_rate = 0.001
in_channels = 3
num_classes = 10
batch_size = 4
num_epochs = 2

## Load Data
train_dataset = datasets.CIFAR10(root = '/dataset', train = True, transform=transforms.ToTensor(), download=True)
train_loader = DataLoader(dataset = train_dataset, batch_size = batch_size, shuffle = True)

## Initialize Model
model = CNN(in_channels=in_channels, num_classes=num_classes)
model.to(device)
print('Your Model Looks Like This ==> \n', model)
total_param = sum(p.numel() for p in model.parameters())
print('## Total Parameters ==> : ', total_param)

from prettytable import PrettyTable
def count_parameters(model):
    table = PrettyTable(["Modules", "Parameters"])
    total_params = 0
    for name, parameter in model.named_parameters():
        if not parameter.requires_grad:
            continue
        params = parameter.numel()
        table.add_row([name, params])
        total_params += params
    print('Parameter Table is ==> \n:', table)
    print(f"Total Trainable Params : {total_params}")
    return total_params

count_parameters((model))

## Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr = learning_rate, weight_decay=0.0)

for epoch in range(num_epochs):
    losses = []
    accuracies = []

    for batch_idx, (data, targets) in enumerate(train_loader):
        data = data.to(device=device)

        # Forward
        pred_scores = model(data)
        loss = criterion(pred_scores, targets)
        losses.append(loss.item())

        # Backward
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    print(f'Mean loss at epoch {epoch} was {sum(losses) / len(losses)}')

    #     # Calculate running training accuracy
    #     _, predictions = pred_scores.max(1)
    #     num_correct = (predictions == targets).sum()
    #
    # print(f'Mean loss at epoch {epoch} was {sum(losses)/ len(losses)}')

## Check Accuaracy
def check_accuracy(loader, model):
    if loader.dataset.train:
        print("Checking accuracy on Training data")
    else:
        print("Checking accuracy on Test data")

    num_correct = 0
    num_samples = 0
    model.eval()

    with torch.no_grad():
        for x , y in loader:
            x = x.to(device=device)
            y = y.to(device=device)

            scores = model(x)
            _, predictions = scores.max(1)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)

        print(f'Got {num_correct}/{num_samples} with accuracy {float(num_correct) / float(num_samples) * 100:.2f}')
    model.train()

check_accuracy(train_loader, model)


import argparse

from utils.config_initializer import ConfigInitializer
from utils.random_seed import seeding
from worker import Worker

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', type=str)
    parser.add_argument('--data', type=str)
    parser.add_argument('--cuda', type=int, default=None)

    args = parser.parse_args()
    config = ConfigInitializer.init(
        model_path=args.model,
        data_path=args.data,
    )
    seeding(2022)

    worker = Worker(config=config, cuda=args.cuda)
    worker.run()


import requests
from requests.compat import urljoin
from  ..models import Model_ConfigProduct

def PRODUCT_ID():
    return Model_ConfigProduct.find_one_by_attr({
            "config_product_code": "CI21000"
        }).config_product_id

def DATA_RATE_GROUP(product_id: int):
    return [
    {
        'config_product_id': product_id,
        'config_rate_group_code': 'APU',
        'config_rate_group_label': "Annual Rate per $1000",
        'unit_value': 1000,
        'apply_discretionary_factor': True
    },
    {
        'config_product_id': product_id,
        'config_rate_group_code': 'FLAT',
        'config_rate_group_label': "Flat Rate",
        'unit_value': 1,
        'apply_discretionary_factor': False
    },
]

def load(hostname: str, *args, **kwargs) -> None:
    product_id = PRODUCT_ID()
    url = urljoin(hostname, f'api/config/product/{product_id}/rate-groups')
    res = requests.post(url, json=DATA_RATE_GROUP(product_id))
    if not res.ok:
        raise Exception(res.text)


import os, sys
import subprocess
import pandas as pd
import multiprocessing as mp
from p_tqdm import p_map
import time

def get_repo_root():
    """Get the root directory of the repo."""
    dir_in_repo = os.path.dirname(os.path.abspath('__file__'))  # os.getcwd()
    return subprocess.check_output('git rev-parse --show-toplevel'.split(),
                                   cwd=dir_in_repo,
                                   universal_newlines=True).rstrip()

ROOT_dir = get_repo_root()
sys.path.append(ROOT_dir)
sys.path.insert(0, ROOT_dir + '/lib')

import lib.routing as routing

if __name__ == "__main__":
    region = 'vg'
    # Parallelize the routing requests
    print('Parallelize the routing requests...')
    df_od = pd.read_csv(ROOT_dir + f'/example/od_pairs_{region}.csv')

    def parallel_process(x):
        folder2save = ROOT_dir + f'/example/output/'
        mode = "BICYCLE,WALK"
        walkdistance = 300
        region = 'vg'
        routing.requesting_origin_batch(data=x, walkdistance=walkdistance,
                                        folder2save=folder2save, region=region, mode=mode)
    p_map(parallel_process, [x for _, x in df_od.groupby("origin")])


"""
* Title: Egg Drop
* Author: Sam Gavis-Hughson
* Date: 11/28/2017
*
* Consider dropping eggs off of a building. If you drop an egg from the Xth floor
* and it breaks, then you know for all eggs dropped from floor X or above, they
* will break. Similarly, if you drop an egg from floor X and it doesn't break, no
* egg will break when dropped from floors 1 through X. Given a building with N
* floors and E eggs, find the minimum number of drops needed to determine the
* maximum floor that you can drop an egg from without it breaking.
*
* eg.
*
* eggs = 1
* floors = 10
* eggDrop(eggs, floors) = 10
* If you only have one egg, you need to try every floor from 1 to floors.
*
* eggs = 2
* floors = 10
* eggDrop(eggs, floors) = 4
* Drop the egg from the 4th floor, then the 7th, then the 10th. Each time, if
* the egg breaks, go to one above the previous test and progress up linearly.
*
* Execution: python egg_drop.py
"""
import unittest
from typing import List

def brute_force_egg_drop(eggs: int, floors: int):
    """
    Brute force recursive solution. Try dropping an egg from every floor,
    then recursively try the case where it breaks and the case where it
    doesn't to see how many drops they take. floors indicates the range of
    floors that need to be tested, not the total floors. Eg. if the egg broke
    at floor 20 and not at floor 5, then floors = 15
    """
    if floors == 0:
        return 0
    if floors == 1:
        return 1
    # If we only have one egg, we have to go up one at a time through each
    # floor to guarantee that we find the exact floor
    if eggs == 1:
        return floors

    max_drops = float("inf")

    # Try dropping the egg from each height. Compute the worst case for if
    # it breaks and if it doesn't break
    for i in range(1, floors):
        # If the egg breaks, then the floor we're looking for could be any
        # floor less than i
        breaks = brute_force_egg_drop(eggs - 1, i - 1)

        # If the egg doesn't break, we can exclude all the floors below and
        # including the current floor (i)
        doesnt_break = brute_force_egg_drop(eggs, floors - i)
        max_drops = min(max_drops, max(breaks, doesnt_break))
    return max_drops + 1

def top_down_egg_drop(eggs: int, floors: int):
    dp = [[0]*(floors+1)]*(eggs+1)
    return top_down_egg_drop_helper(eggs, floors, dp)

def top_down_egg_drop_helper(eggs: int, floors: int, dp: List[List[int]]):
    if floors == 0:
        return 0
    if floors == 1:
        return 1
    if eggs == 1:
        return floors

    if dp[eggs][floors] == 0:
        max_drops = float("inf")
        for i in range(1, floors):
            breaks = top_down_egg_drop_helper(eggs - 1, i - 1, dp)
            doesnt_break = top_down_egg_drop_helper(eggs, floors - i, dp)
            max_drops = min(max_drops, max(breaks, doesnt_break))
        dp[eggs][floors] = max_drops + 1
    return dp[eggs][floors]

def bottom_up_egg_drop(eggs: int, floors: int):
    dp = [[0]*(floors+1)]*(eggs+1)
    for i in range(1, len(dp)):
        for j in range(1, len(dp[0])):
            if j == 1:
                dp[i][j] = 1
            elif i == 1:
                dp[i][j] = j
            else:
                max_drops = float("inf")
                for k in range(1, j+1):
                    breaks = dp[i-1][k-1]
                    doesnt_break = dp[i][j-k]
                    max_drops = min(max_drops, max(breaks, doesnt_break))
                dp[i][j] = max_drops + 1
    return dp[eggs][floors]

class TestEggDrop(unittest.TestCase):
    """Unit test for egg_drop."""

    def test_brute_force(self):
        self.assertEqual(brute_force_egg_drop(1, 1), 1)
        self.assertEqual(brute_force_egg_drop(1, 10), 10)
        self.assertEqual(brute_force_egg_drop(2, 10), 4)
        self.assertEqual(brute_force_egg_drop(2, 20), 6)
        self.assertEqual(brute_force_egg_drop(3, 10), 4)

    def test_top_down(self):
        self.assertEqual(top_down_egg_drop(1, 1), 1)
        self.assertEqual(top_down_egg_drop(1, 10), 10)
        self.assertEqual(top_down_egg_drop(2, 10), 4)
        self.assertEqual(top_down_egg_drop(2, 20), 6)
        self.assertEqual(top_down_egg_drop(3, 10), 4)

    def test_bottom_up(self):
        self.assertEqual(bottom_up_egg_drop(1, 1), 1)
        self.assertEqual(bottom_up_egg_drop(1, 10), 10)
        self.assertEqual(bottom_up_egg_drop(2, 10), 4)
        self.assertEqual(bottom_up_egg_drop(3, 10), 4)

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


import json
import os
import time
import warnings
import numpy as np
import torch
from collections import defaultdict
from torch.utils.data import DataLoader
from transformers import get_linear_schedule_with_warmup
from typing import List, Tuple, Dict

from pathlib import Path

from error_correction.modelling.base_transformer import BaseTransformer
from error_correction.modelling.dataset.error_correction_dataset import (
    ErrorCorrectionSeq2SeqDataset,
)
from error_correction.modelling.lightning_base import add_generic_args
from error_correction.modelling.reader.mask_based_correction_reader import (
    MaskBasedCorrectionReader,
)
from error_correction.modelling.reader.supervised_correction_reader import (
    SupervisedCorrectionReader,
)
from error_correction.modelling.utils import (
    is_truthy,
    SARI_KEYS,
    use_task_specific_params,
    pickle_save,
    freeze_params,
    lmap,
    flatten_list,
    save_json,
    calculate_sari,
    post_clean,
    assert_all_frozen,
)

class ErrorCorrectionModule(BaseTransformer):
    mode = "error-correction"
    val_metric = SARI_KEYS[2]
    loss_names = ["loss"]
    metric_names = SARI_KEYS

    def __init__(self, hparams, **kwargs):
        super().__init__(hparams, num_labels=None, mode=self.mode, **kwargs)
        use_task_specific_params(self.model, "error-correction")

        self.metrics_save_path = Path(self.output_dir) / (
            "metrics.json"
            if not self.hparams.do_predict
            else "metrics_test_{}.json".format(os.path.basename(self.hparams.test_file))
        )

        self.hparams_save_path = Path(self.output_dir) / "hparams.pkl"
        pickle_save(self.hparams, self.hparams_save_path)
        self.step_count = 0
        self.metrics = defaultdict(list)

        self.dataset_kwargs: dict = dict(
            max_source_length=self.hparams.max_source_length,
            mutation_source=is_truthy(self.hparams.mutation_source),
            mutation_target=is_truthy(self.hparams.mutation_target),
        )

        n_observations_per_split = {
            "train": self.hparams.n_train,
            "val": self.hparams.n_val,
            "test": self.hparams.n_test,
        }
        self.n_obs = {
            k: v if v >= 0 else None for k, v in n_observations_per_split.items()
        }

        self.target_lens = {
            "train": self.hparams.max_target_length,
            "val": self.hparams.val_max_target_length,
            "test": self.hparams.test_max_target_length,
        }

        self.data_paths = {
            "train": self.hparams.train_file,
            "val": self.hparams.val_file,
            "test": self.hparams.test_file,
        }

        assert (
            self.target_lens["train"] <= self.target_lens["val"]
        ), f"target_lens: {self.target_lens}"
        assert (
            self.target_lens["train"] <= self.target_lens["test"]
        ), f"target_lens: {self.target_lens}"

        if self.hparams.freeze_embeds:
            self.freeze_embeds()

        if self.hparams.freeze_encoder:
            freeze_params(self.model.get_encoder())
            assert_all_frozen(self.model.get_encoder())

        self.num_workers = hparams.num_workers
        self.decoder_start_token_id = None
        self.dataset_class = ErrorCorrectionSeq2SeqDataset
        self.wiki_reader = self.get_reader(self.hparams.reader, self.hparams.do_predict)

    def get_reader(self, name, test):
        labels = set()
        if self.hparams.labels == "all":
            labels.add("SUPPORTS")
            labels.add("REFUTES")
        else:
            labels.add(self.hparams.labels.upper())

        print("XXX Reader labels {}".format(self.hparams.labels))
        print("XXX labels", labels)

        if name == "supervised":
            return SupervisedCorrectionReader(labels, test)
        elif name == "mask":
            return MaskBasedCorrectionReader(labels, test)
        else:
            raise RuntimeError(f"Unknown reader {name}")

    def freeze_embeds(self):
        """Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
        try:
            freeze_params(self.model.model.shared)
            for d in [self.model.model.encoder, self.model.model.decoder]:
                freeze_params(d.embed_positions)
                freeze_params(d.embed_tokens)
        except AttributeError:
            freeze_params(self.model.shared)
            for d in [self.model.encoder, self.model.decoder]:
                freeze_params(d.embed_tokens)

    def forward(self, input_ids, **kwargs):
        return self.model(input_ids, **kwargs)

    def ids_to_clean_text(self, generated_ids: List[int]):
        gen_text = self.tokenizer.batch_decode(
            generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
        )
        return lmap(str.strip, gen_text)

    def _step(self, batch: dict) -> Tuple:
        pad_token_id = self.tokenizer.pad_token_id
        src_ids, src_mask = batch["input_ids"], batch["attention_mask"]
        tgt_ids = batch["decoder_input_ids"]

        decoder_input_ids = self.model._shift_right(tgt_ids)

        outputs = self(
            src_ids,
            attention_mask=src_mask,
            decoder_input_ids=decoder_input_ids,
            use_cache=False,
        )
        lm_logits = outputs[0]

        # Same behavior as modeling_bart.py, besides ignoring pad_token_id
        ce_loss_fct = torch.nn.CrossEntropyLoss(ignore_index=pad_token_id)

        assert lm_logits.shape[-1] == self.model.config.vocab_size
        loss = ce_loss_fct(lm_logits.view(-1, lm_logits.shape[-1]), tgt_ids.view(-1))

        return (loss,)

    def training_step(self, batch, batch_idx) -> Dict:
        loss_tensors = self._step(batch)
        logs = {name: loss for name, loss in zip(self.loss_names, loss_tensors)}
        return {"loss": loss_tensors[0], "log": logs}

    def validation_step(self, batch, batch_idx) -> Dict:
        return self._generative_step(batch)

    def validation_epoch_end(self, outputs, prefix="val") -> Dict:
        self.step_count += 1
        losses = {
            k: torch.stack([x[k] for x in outputs]).mean() for k in self.loss_names
        }
        loss = losses["loss"]

        rouges = {
            k: np.array(flatten_list([x[k] for x in outputs])).mean().item()
            for k in self.metric_names + ["gen_time", "summ_len"]
        }
        rouge_tensor: torch.FloatTensor = torch.tensor(rouges[self.val_metric]).type_as(
            loss
        )
        rouges.update({k: v.item() for k, v in losses.items()})
        losses.update(rouges)
        metrics = {f"{prefix}_avg_{k}": x for k, x in losses.items()}
        metrics["step_count"] = self.step_count

        self.save_metrics(metrics, prefix)  # writes to self.metrics_save_path

        preds = flatten_list([x["preds"] for x in outputs])
        targets = flatten_list([x["target"] for x in outputs])
        metadata = flatten_list([x["metadata"] for x in outputs])

        self.save_predictions(preds, targets, metadata, prefix)

        return {
            "log": metrics,
            "preds": preds,
            f"{prefix}_loss": loss,
            f"{prefix}_{self.val_metric}": rouge_tensor,
        }

    def save_metrics(self, latest_metrics, type_path) -> None:
        self.metrics[type_path].append(latest_metrics)
        save_json(self.metrics, self.metrics_save_path)

    def save_predictions(self, predictions, actual, metadata, type_path) -> None:
        with open(
            Path(self.output_dir)
            / (
                "predictions_set_{}_epoch_{}_steps_{}_.jsonl".format(
                    type_path, self.trainer.current_epoch, self.step_count
                )
                if not self.hparams.do_predict
                else "final_predictions_set_{}_file_{}".format(
                    type_path, os.path.basename(self.hparams.test_file)
                )
            ),
            "w+",
        ) as f:
            for p, a, m in zip(predictions, actual, metadata):
                f.write(
                    json.dumps({"prediction": p, "actual": a, "metadata": m}) + "\n"
                )

    def _generative_step(self, batch: dict) -> dict:
        pad_token_id = self.tokenizer.pad_token_id
        (
            source_ids,
            source_mask,
            y,
            original_ids,
        ) = ErrorCorrectionSeq2SeqDataset.trim_seq2seq_batch(batch, pad_token_id)
        t0 = time.time()
        generated_ids = self.model.generate(
            input_ids=source_ids,
            attention_mask=source_mask,
            use_cache=True,
            decoder_start_token_id=self.decoder_start_token_id,
            max_length=self.hparams.val_max_target_length,
        )
        gen_time = (time.time() - t0) / source_ids.shape[0]
        preds = self.ids_to_clean_text(generated_ids)
        target = self.ids_to_clean_text(y)
        original = self.ids_to_clean_text(original_ids)
        loss_tensors = self._step(batch)
        base_metrics = {name: loss for name, loss in zip(self.loss_names, loss_tensors)}
        metrics: Dict = self.calc_generative_metrics(original, preds, target)
        summ_len = lmap(len, generated_ids)
        base_metrics.update(
            gen_time=[gen_time],
            summ_len=summ_len,
            preds=preds,
            target=target,
            metadata=batch["metadata"],
            **metrics,
        )
        return base_metrics

    def calc_generative_metrics(self, originals, preds, target) -> Dict:
        # return calculate_rouge([p.replace(": ","") for p in preds], [p.replace("correction: ","") for p in target])

        return calculate_sari(
            originals, [post_clean(p) for p in preds], [post_clean(p) for p in target]
        )

    def test_step(self, batch, batch_idx):
        return self._generative_step(batch)

    def test_epoch_end(self, outputs):
        return self.validation_epoch_end(outputs, prefix="test")

    def get_dataset(self, type_path) -> ErrorCorrectionSeq2SeqDataset:
        n_obs = self.n_obs[type_path]
        max_target_length = self.target_lens[type_path]
        instance_generator = self.wiki_reader.read(self.data_paths[type_path])

        dataset = self.dataset_class(
            self.tokenizer,
            instance_generator=instance_generator,
            n_obs=n_obs,
            max_target_length=max_target_length,
            **self.dataset_kwargs,
        )
        return dataset

    def get_dataloader(
        self, type_path: str, batch_size: int, shuffle: bool = False
    ) -> DataLoader:
        dataset = self.get_dataset(type_path)
        sampler = None
        if self.hparams.sortish_sampler and type_path == "train":
            assert self.hparams.gpus <= 1  # TODO: assert earlier
            sampler = dataset.make_sortish_sampler(batch_size)
            shuffle = False

        dataloader = DataLoader(
            dataset,
            batch_size=batch_size,
            collate_fn=dataset.collate_fn,
            shuffle=shuffle,
            num_workers=self.num_workers,
            sampler=sampler,
        )
        return dataloader

    def train_dataloader(self) -> DataLoader:
        dataloader = self.get_dataloader(
            "train", batch_size=self.hparams.train_batch_size, shuffle=True
        )
        t_total = (
            (
                len(dataloader.dataset)
                // (self.hparams.train_batch_size * max(1, self.hparams.gpus))
            )
            // self.hparams.accumulate_grad_batches
            * float(self.hparams.max_epochs)
        )
        scheduler = get_linear_schedule_with_warmup(
            self.opt,
            num_warmup_steps=self.hparams.warmup_steps,
            num_training_steps=t_total,
        )
        if max(scheduler.get_last_lr()) > 0:
            warnings.warn("All learning rates are 0")
        self.lr_scheduler = scheduler
        return dataloader

    def val_dataloader(self) -> DataLoader:
        return self.get_dataloader("val", batch_size=self.hparams.eval_batch_size)

    def test_dataloader(self) -> DataLoader:
        return self.get_dataloader("test", batch_size=self.hparams.eval_batch_size)

    @staticmethod
    def add_model_specific_args(parser, root_dir):
        BaseTransformer.add_model_specific_args(parser, root_dir)
        add_generic_args(parser, root_dir)

        parser.add_argument("--reader", default="wiki", type=str)
        parser.add_argument("--train_file", required=True, type=str)
        parser.add_argument("--val_file", required=True, type=str)
        parser.add_argument("--test_file", required=False, type=str)
        parser.add_argument(
            "--max_source_length",
            default=512,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--max_target_length",
            default=256,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--val_max_target_length",
            default=256,  # these defaults are optimized for CNNDM. For xsum, see README.md.
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument(
            "--test_max_target_length",
            default=256,
            type=int,
            help="The maximum total input sequence length after tokenization. Sequences longer "
            "than this will be truncated, sequences shorter will be padded.",
        )
        parser.add_argument("--freeze_encoder", action="store_true")
        parser.add_argument("--freeze_embeds", action="store_true")
        parser.add_argument("--sortish_sampler", action="store_true", default=False)
        parser.add_argument(
            "--logger_name",
            type=str,
            choices=["default", "wandb", "wandb_shared"],
            default="default",
        )
        parser.add_argument(
            "--n_train",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument(
            "--n_val",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument(
            "--n_test",
            type=int,
            default=-1,
            required=False,
            help="# examples. -1 means use all.",
        )
        parser.add_argument("--mutation_source", required=True)
        parser.add_argument("--mutation_target", required=True)
        parser.add_argument(
            "--labels", type=str, choices=["supports", "refutes", "all"], required=True
        )

        return parser


class chartdata:
    def chartdataorder(dic):
        import datetime
        month=datetime.datetime.now().month
        year=datetime.datetime.now().year
        monthnames={1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}

        ls=[]
        i=1
        j=12
        newyear=False
        for key, value in monthnames.items():
            if key==month:
                ls.append(month)
                for l in range(1,12):
                    if ls[-1] !=12 and newyear==False:
                        newmonth=month-i
                        ls.insert(0,newmonth)
                        i=i+1
                        if(newmonth==1):
                            newyear=True
                    else:
                        ls.insert(0,j)
                        j=j-1

        #{k: sample_dict[k] for k in desired_order_list}
        monthreorderdict={k: monthnames[k] for k in ls}
        #print(monthreorderdict)
        changeyear=False
        for key, value in monthreorderdict.items():
            if value!='Jan' and changeyear==False:
                monthreorderdict[key]=value+' '+str(year-2001)
                if value=='Dec':
                    changeyear=True
            else:
                monthreorderdict[key]=value+' '+str(year-2000)

        #print(monthreorderdict)
        monthreorderdict = {value:0 for key, value in monthreorderdict.items()}
        for key,values in dic.items():
            if key in monthreorderdict.keys():
               # print(key)
                monthreorderdict[key]=values

        return monthreorderdict


from google.cloud import bigquery
from google.oauth2 import service_account

BYTES_PER_GB = 2**30
QUERY_LIMIT = 1*BYTES_PER_GB

def estimate_bytes_processed(query, bq_client):
    """A function to estimate query size without execution of query.
    Modified from: https://www.kaggle.com/sohier/beyond-queries-exploring-the-bigquery-api/
    """
    # Below we initialize the dry_run attribute to true so that our query
    # is not actually executed and does not use any of our precious quota
    estjob_config = bigquery.job.QueryJobConfig(dry_run=True)

    estjob = bq_client.query(query, job_config=estjob_config)

    return estjob.total_bytes_processed

creds_path = "secrets/ml-for-finance-296211-4c54724037a8.json"
credentials = service_account.Credentials.from_service_account_file(
    creds_path, scopes=["https://www.googleapis.com/auth/cloud-platform"])

#Establishes our connection with the bigQuery API
client = bigquery.Client(credentials=credentials, project=credentials.project_id)

# Queries written in SQL
query_smol = """
        SELECT
          timestamp,
          size,
          version,
          bits,
          transaction_count
        FROM `bigquery-public-data.crypto_bitcoin.blocks`
"""

query = """
        SELECT
          block_timestamp,
          is_coinbase,
          input_value,
          output_value,
          input_count,
          output_count,
          fee
        FROM `bigquery-public-data.crypto_bitcoin.transactions`
"""

estimate = estimate_bytes_processed(query_smol, client)
print(f"This query will process {estimate/BYTES_PER_GB:.3f} GB")

safe_config = bigquery.QueryJobConfig(maximum_bytes_billed=QUERY_LIMIT)

query_job = client.query(query_smol, job_config=safe_config)

df = query_job.result().to_dataframe()
print(df)
print(df.memory_usage(deep=True))

# Save DataFrame to csv file
df.to_csv('data/blocks.csv', index=False)


import openai
import json
from PyQt5.QtCore import QThread,pyqtSignal

class chatCompletion(QThread):
    responseChanged = pyqtSignal(str)
    with open("API_key.txt","r") as key:
        API_key = key.read()
    def __init__(self, context, maxtkns,temp, destination):
        super().__init__()
        self.context = context
        self.tokens = maxtkns
        self.temp = temp
        self.destination = destination
        destination.append("\nBetsy: ")
        self.chunk_message = None
        self.answer = ""
    def run(self):
        with open("API_key.txt","r") as key:
            API_key = key.read()
            print(API_key)
        openai.api_key = API_key
        with open(self.context) as f:
            messages = json.load(f)
        completion = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages,
            temperature=self.temp,
            max_tokens=self.tokens,
            stream = True
            )
        for chunk in completion:
            try:
                self.chunk_message = chunk['choices'][0]['delta']['content']
                # move the cursor to the end of the text
                cursor = self.destination.textCursor()
                cursor.movePosition(cursor.End)

                # insert the new word
                cursor.insertText(self.chunk_message)
                self.answer = self.answer + str(self.chunk_message)
            except:
                self.answer = " \n \n ERREUR LORS DE LA COMPLETION ESSAYEZ RENVOYER LE MESSAGE SI LE PROBLÉME PERSISTE VERIFIEZ 'ETAT DE LA CONNECTION ET REDEMARREZ L'APPLICATION "

        self.responseChanged.emit(self.chunk_message)
    @property
    def response(self):
        return self.chunk_message


import urllib3
import tempfile
import os
import sys
import argparse
import json
import certifi
import ast

api_dict = json.load(open("./api_dict.json", 'r'))

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED',
        ca_certs=certifi.where())

class Request():
    '''
    This class handles performing a web request for information from a specified government economic database.

    :param type:
    :param method:
    :param parameters: A list of tuples containing parameter and value pairs.
    '''
    def __init__(self, type, method, parameters=None, format='json'):
        #Base parameters
        self.type = type
        self.uri = api_dict[type]['uri']
        self.method = method
        self.handle = api_dict[type]['method'][method]['handle']
        self.key = '&UserID=' + os.environ[api_dict[type]['key']]
        self.format = api_dict[type]['format'][format]

        self.parameters = dict(parameters)
        self.required = api_dict[type]['method'][method]['parameters']['required']
        self.optional = api_dict[type]['method'][method]['parameters']['optional']

        #print('In class type is: {}'.format(self.type))
        #print('In class method is: {}'.format(self.method))
        #print('In class handle is: {}'.format(self.handle))
        #print('In class uri is: {}'.format(self.uri))
        #print('In class format is: {}'.format(self.format))
        #print('In class parameters is: {}'.format(self.parameters))

        #self.url = self.uri + self.key + self.method + self.format
    def execute_method(self):
        print('Running execute_method...')
        if self.method in ["list_datasets", "get_parameter_list", "get_parameter_values", "get_parameter_values_filtered"]:
            print('Running get_metadata method...')
            response = self.get_metadata()
            data = json.loads(response.data)
        if self.method == 'get_data':
            print('Running get_data method...')
            data = self.get_data()
        else: pass

        return data

    def get_metadata(self):
        url = self.uri + self.key + self.format + self.handle
        for param, value in self.parameters.items():
            url += '&{}={}'.format(param, value)

        print('Request URL: {}'.format(url))
        response = http.request('GET', url)
        return response

    def get_data(self):
        url = self.uri + self.key + self.format + self.handle
        for param, value in self.parameters.items():
            url += '&{}={}'.format(param, value)

        response = http.request('GET', url)
        #print(response.data)
        data = json.loads(response.data)
        return data
        #for i in data['BEAAPI']['Results']['Dataset']:
            #print(i['DatasetName'] + '\n' + i['DatasetDescription'] + '\n\n')
        #output = json.loads(data)
        #return output

    def check_parameters(self):
        for req in self.required:
            if req not in self.parameters:
                print('Missing required parameter {}. Please supply this parameter and retry.'.format(req))
            else: continue

    def display_data(self):
        data = self.get_data()
        for i in data['BEAAPI']['Results']['Dataset']:
            print(i['DatasetName'] + '\n' + i['DatasetDescription'] + '\n\n')
        #print(json.dumps(data, indent=4, sort_keys=True))

# Sysarg format: Type, Method, Dataset_Name, Table_Name, Frequency, Year
# Example: DataSet = NIPA, TableName = T10101, Frequency = Q, Year = 2018
# Table Results: BEAAPI > Results > Data/Notes
if __name__ == '__main__':
    # Database/type specified by command line argument.
    type = sys.argv[1]
    print('Type: {}'.format(type))

    # Method is a command line argument.
    method = sys.argv[2]
    print('Method: {}'.format(method))
    print('Example Parameters for Get Data: DataSet = NIPA, TableName = T10101, Frequency = Q, Year = 2018')

    # Parameters defined by prompting for user keyboard input (for now).
    parameters = list()
    dataset = None
    required_parameters = api_dict[type]['method'][method]['parameters']['required']
    optional_parameters = api_dict[type]['method'][method]['parameters']['optional']
    for param in required_parameters:
        # Do not need to input key/UserID, included in configuration. Method is a command line argument.
        if param in ['UserID', 'Method']: continue
        value = input('{} (required):'.format(param))
        # Keep dataset name stored outside for grabbing extra parameters for a get_data method.
        if param == 'DatasetName': dataset = value
        parameters.append( (str(param), value) )
    for param in optional_parameters:
        # Do not need to define result format, already set to json by default.
        if param == 'ResultFormat': continue
        value = input('{} (optional):'.format(param))
        parameters.append( (str(param), value) )

    # If the method is get_data, need to run a second loop to add dataset specific parameters.
    if method == 'get_data':
        required_parameters = api_dict[type]['method'][method]['parameters'][dataset]['required']
        try:
            optional_parameters = api_dict[type]['method'][method]['parameters'][dataset]['optional']
        except:
            optional_parameters = None
        for param in required_parameters:
            value = input('{} (required):'.format(param))
            parameters.append( (param, value) )
        if optional_parameters != None:
            for param in optional_parameters:
                value = input('{} (optional):'.format(param))
                parameters.append( (param, value) )

    print('Complete Parameters list: {}'.format(parameters))

    #print('kwargs: {}'.format(kwargs))
    obj = Request(type, method, parameters)
    data = obj.execute_method()
    print('Data JSON Object: {}'.format(data))
    data_dict = ast.literal_eval(json.dumps(data))
    #print(data_dict['BEAAPI']['Results'])
    print(json.dumps(data, indent=4, sort_keys=True))


from rest_framework.authtoken.models import Token
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from rest_framework.views import APIView
from user.models import User
from rest_framework import generics
from rest_framework.authentication import TokenAuthentication
from user.serializers import UserSerializer
from rest_framework.permissions import (
    AllowAny,
    IsAuthenticated)

class ValidateToken(APIView):
    permission_classes = [AllowAny]

    def get(self, request):
        return JsonResponse({'status': False,
                             'message': "Método get não permitido"})

    def post(self, request):
        data = request.data
        token = data['token']
        obj = get_object_or_404(Token, key=token)
        if obj is not None:
            return JsonResponse({'status': True})

        message = "Token not found !"
        return JsonResponse({'status': False, 'message': message})

class AddNewUser(generics.CreateAPIView):
    permission_classes = [AllowAny]
    queryset = User.objects.all()
    serializer_class = UserSerializer

class DeleteUser(generics.DestroyAPIView):
    authenticantion_classes = [TokenAuthentication]
    permission_classes = [IsAuthenticated]
    serializer_class = UserSerializer
    lookup_field = "username"

    def get_queryset(self):
        username = self.kwargs.get('username', None)
        queryset = User.objects.filter(username=username)
        return queryset

    def perform_destroy(self, instance):
        serializer = UserSerializer(data=self.get_queryset())
        serializer.is_valid()
        return instance.delete()


from django import shortcuts
from django.views.decorators import vary
from django.shortcuts import render

import horizon

from openstack_auth import views

#huoqu yonghuming , panduan yonghu quanxian ,yongyu shezhi tiaozhuanjiemian

def get_user_home(user):
    if user.is_superuser:
        return horizon.get_dashboard('admin').get_absolute_url()
    return horizon.get_dashboard('project').get_absolute_url()

@vary.vary_on_cookie
#huoqu denglu jiemian de yonghuming he mima , yanzheng tongguo ze panduan yonghuquanxian, fouze shuaxin
def splash(request):
    if request.user.is_authenticated():
       return shortcuts.redirect(get_user_home(request.user))
#       return shortcuts.render(request, "admin")
    form = views.Login(request)
    request.session.clear()
    request.session.set_test_cookie()
    return shortcuts.render(request, 'splash.html', {'form': form})

def index(request):
    return render(request,'index.html')


import torch
import os
import logging
import shutil
from tqdm import tqdm
logger = logging.getLogger('Attention')

def save_checkpoint(state, is_best, epoch, checkpoint, ins_name=-1):
    '''Saves model and training parameters at checkpoint + 'last.pth.tar'. If is_best==True, also saves
    checkpoint + 'best.pth.tar'
    Args:
        state: (dict) contains model's state_dict, may contain other keys such as epoch, optimizer state_dict
        is_best: (bool) True if it is the best model seen till now
        checkpoint: (string) folder where parameters are to be saved
        ins_name: (int) instance index
    '''
    if ins_name == -1:
        filepath = os.path.join(checkpoint, f'epoch_{epoch}.pth.tar')
    else:
        filepath = os.path.join(checkpoint, f'epoch_{epoch}_ins_{ins_name}.pth.tar')
    if not os.path.exists(checkpoint):
        logger.info(f'Checkpoint Directory does not exist! Making directory {checkpoint}')
        os.mkdir(checkpoint)
    torch.save(state, filepath)
    logger.info(f'Checkpoint saved to {filepath}')
    if is_best:
        shutil.copyfile(filepath, os.path.join(checkpoint, 'best.pth.tar'))
        logger.info('Best checkpoint copied to best.pth.tar')

def load_checkpoint(checkpoint, model, optimizer=None):
    '''Loads model parameters (state_dict) from file_path. If optimizer is provided, loads state_dict of
    optimizer assuming it is present in checkpoint.
    Args:
        checkpoint: (string) filename which needs to be loaded
        model: (torch.nn.Module) model for which the parameters are loaded
        optimizer: (torch.optim) optional: resume optimizer from checkpoint
        gpu: which gpu to use
    '''
    if not os.path.exists(checkpoint):
        raise FileNotFoundError(f"File doesn't exist {checkpoint}")
    if torch.cuda.is_available():
        checkpoint = torch.load(checkpoint, map_location='cuda')
    else:
        checkpoint = torch.load(checkpoint, map_location='cpu')
    model.load_state_dict(checkpoint['state_dict'])

    if optimizer:
        optimizer.load_state_dict(checkpoint['optim_dict'])

    return checkpoint

def set_logger(log_path):
    '''Set the logger to log info in terminal and file `log_path`.
    In general, it is useful to have a logger so that every output to the terminal is saved
    in a permanent file. Here we save it to `model_dir/train.log`.
    Example:
    logging.info('Starting training...')
    Args:
        log_path: (string) where to log
    '''
    _logger = logging.getLogger('Attention')
    _logger.setLevel(logging.INFO)

    fmt = logging.Formatter('[%(asctime)s] %(name)s: %(message)s', '%H:%M:%S')

    class TqdmHandler(logging.StreamHandler):
        def __init__(self, formatter):
            logging.StreamHandler.__init__(self)
            self.setFormatter(formatter)

        def emit(self, record):
            msg = self.format(record)
            tqdm.write(msg)

    file_handler = logging.FileHandler(log_path)
    file_handler.setFormatter(fmt)
    _logger.addHandler(file_handler)
    _logger.addHandler(TqdmHandler(fmt))


from collections import OrderedDict
from polity.models import Polity

def navigation(request):

    # For now, we only support one level of sub-polities in the navigation.
    # This is mostly because we haven't designed the interface to accommodate
    # sub-sub-polities, and we're not likely to use them any time soon.
    #
    # This may seem overly convoluted (and hopefully adequately explained in
    # comments) but the effect we want is:
    #
    # a) All visible sub-polities of the uppermost polities being shown.
    # b) Only show polity types which actually contain sub-polities.
    # c) Order polity types as they are defined in the `Polity` model.
    # d) Sub-polity navigation being available from within sub-polities.

    # The resulting object, used in the template.
    polity_nav = OrderedDict()

    # Order of polity types as defined in Polity.POLITY_TYPES.
    type_order = [t[0] for t in Polity.POLITY_TYPES]

    # Get visible sub-polities of uppermost polity.
    polities = Polity.objects.visible().exclude(parent=None).filter(parent__parent=None)

    # Sort polities by type, in the order defined in Polity.POLITY_TYPES.
    polities = sorted(polities, key=lambda p: type_order.index(p.polity_type))

    # Add polities to the navigation, organized and ordered by type.
    for polity in polities:
        # Create the entry of the polity type if it doesn't exist.
        if polity.polity_type not in polity_nav:
            polity_nav[polity.polity_type] = {
                'polity_type_name': polity.get_polity_type_display(),
                'polities': [],
            }

        polity_nav[polity.polity_type]['polities'].append(polity)

    return { 'polity_nav': polity_nav }


import secrets
import uuid

from django.contrib.auth.models import User
from django.shortcuts import render
from requests import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView

from .serializer import *
from .models import *
from rest_framework import generics, status

class QuesttionList(generics.ListAPIView):
    queryset = Questtion.objects.all()
    serializer_class = QuesttionSerializer

class DirectionList(generics.ListAPIView):
    queryset = Direction.objects.all()
    serializer_class = DirectionSerializer

class CandidateList(generics.ListCreateAPIView):
    queryset = Candidate.objects.all()
    serializer_class = CandidateSerializer

class CandidateDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Candidate.objects.all()
    serializer_class = CandidateSerializer

    def get_serializer_class(self):
        if self.request.method == 'PUT' or self.request.method == 'PATCH':
            return CandidateSerializer
        return CandidateCreateSerializer

class UserCreateAnswer(generics.ListCreateAPIView):
    queryset = UserAnswer.objects.all()
    serializer_class = AnswerSerializer

class UserAnswerDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = UserAnswer.objects.all()
    serializer_class = AnswerSerializer

    def get_serializer_class(self):
        if self.request.method == 'PUT' or self.request.method == 'PATCH':
            return AnswerSerializer
        return AnswerCreateSerializer

class APIKeyView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request, format=None):
        user = request.user
        key = str(uuid.uuid4())
        api_key = APIKey.objects.create(user=user, key=key)
        return Response({'key': api_key.key}, status=status.HTTP_201_CREATED)


from random import randrange
import googletrans
import discord
import sqlite3

data = sqlite3.connect('database.db')
cursor = data.cursor()

img, style = "", ""

client = discord.Client()
token = "put your bot token here" # <-- TOKEN
embed = discord.Embed

Help_Message = ("*kiss {pessoa} => todas as preferencias.\n"
                "*hug {pessoa} => todas as preferencias.\n"
                "*slap {pessoa} => todas as preferencias.\n"
                "-kiss {pessoa} => preferencia lesbica.\n"
                "-hug {pessoa} => preferencia lesbica.\n"
                "+kiss {pessoa} => preferencia homosexual.")

commands = ["*kiss", "*hug", "*slap", "-kiss", "-hug", "+kiss"]
MaxLen = [61, 51, 29, 38, 18, 38]
titles = ["amorzinho", "amorzinho", "briga briga", "Amorzinho", "Amorzinho", "Amorzinho"]
tables = ["kiss", "hugs", "slap", "kiss_yuri", "hugs_yuri", "kiss_yaoi"]
actions = ["beijou", "abraçou", "bateu em", "beijou", "abraçou", "beijou"]

def function(_title, table, max_table_len, author, person, option):
    global img, style
    element_position = randrange(0, max_table_len)
    element = cursor.execute(f"SELECT link FROM {table} WHERE id={element_position}")
    for i in element:
        img = i
        break
    img = f"{img[0]}"
    style = embed(title=f"{_title}", description=f"{author} {option} {person}")
    style.set_image(url=img)
    return style

def translator(message):
    global embed
    translator = googletrans.Translator()
    translation = translator.translate(message, dest="pt")
    translation = translation.text
    embed = discord.Embed(title=f"{message} = {translation}")

@client.event
async def on_ready():
    print("started!")
    return

@client.event
async def on_message(message):
    global embed
    message.content = message.content.lower()
    if message.content.startswith("--help"):
        embed = discord.Embed(title="HELP MESSAGE", description=Help_Message)
        await message.channel.send(embed=embed)

    if message.content.startswith("+translate"):
        msg = message.content[10:]
        translator(msg)
        await message.channel.send(embed=embed)

    for i in range(len(commands)):
        if message.content.startswith(commands[i]):
            function(titles[i], tables[i], MaxLen[i], message.author.mention, message.content[len(commands[i]):28], actions[i])
            await message.channel.send(embed=style)
            break

client.run(token)


import tkinter as tk
import soundfile as sf
import sounddevice as sd

class Mixer:   #
    def __init__(self):
        # creating the box type window

        self.window = tk.Tk()
        self.window.title("Matrix Multiplication")
        self.window.geometry("600x200")

        self.fs=44100

        self.track_0_name="music/vettah-bgm_0.wav"
        self.track_1_name="music/vocal.wav"
        self.track_2_name="music/bg_0.wav"

        y, fs = sf.read(self.track_0_name, dtype='float32')
        self.track_0_audio=y[slice(0,5*fs,1)]

        y, fs = sf.read(self.track_1_name, dtype='float32')
        self.track_1_audio=y[slice(0,5*fs,1)]

        y, fs = sf.read(self.track_2_name, dtype='float32')
        self.track_2_audio=y[slice(0,5*fs,1)]/3

        # creating string variable in Tkinter

        self.x = 0
        self.y = 0
        self.z = 0

        # First label-scale pair

        self.label_track_0 = tk.Label(self.window, text="Track 0",width=10,font=("Courier",20))
        self.scale_0=tk.Scale(self.window, from_=0, to=10, orient=tk.HORIZONTAL,command=self.Get_x,activebackground='yellow',bd=3,bg='red')
        self.button_0_play = tk.Button(self.window, text="Play",width=10,font=("Courier",10),bd=3,command=self.Play_track_0)
        # Second label-scale pair

        self.label_track_1 = tk.Label(self.window, text="Track 1",width=10,font=("Courier",20))
        self.scale_1 = tk.Scale(self.window, from_=0, to=10, orient=tk.HORIZONTAL,command=self.Get_y,activebackground='yellow',bd=3,bg='green')
        self.button_1_play=tk.Button(self.window,text="Play",width=10,font=("Courier",10),bd=3,command=self.Play_track_1)

        # Third label-scale pair

        self.label_track_2 = tk.Label(self.window, text="Track 2",width=10,font=("Courier",20))
        self.scale_2 = tk.Scale(self.window, from_=0, to=10, orient=tk.HORIZONTAL,command=self.Get_z,activebackground='yellow',bd=3,bg='blue')
        self.button_2_play = tk.Button(self.window, text="Play",width=10,font=("Courier",10),bd=3,command=self.Play_track_2)
        # Packing Widgets

        self.button_mix=tk.Button(self.window,text="Mix",width=15,font=("Courier",15),command=self.Mix)
        self.button_quit=tk.Button(self.window,text="Quit",width=15,font=("Courier",15),command=self.window.quit)

        self.label_track_0.grid(row=0,column=0)
        self.scale_0.grid(row=0,column=1)
        self.button_0_play.grid(row=0,column=2)

        self.label_track_1.grid(row=1, column=0)
        self.scale_1.grid(row=1, column=1)
        self.button_1_play.grid(row=1, column=2)

        self.label_track_2.grid(row=2, column=0)
        self.scale_2.grid(row=2, column=1)
        self.button_2_play.grid(row=2, column=2)

        self.button_mix.grid(row=3,column=1)
        self.button_quit.grid(row=3, column=2)

        self.window.mainloop()

    def Get_x(self,event):
        self.x=self.scale_0.get()
    def Get_y(self,val):
        self.y=self.scale_1.get()
    def Get_z(self,event):
        self.z=self.scale_2.get()
    def Play_track_0(self):
        sd.play(self.track_0_audio,self.fs)
        sd.wait()
    def Play_track_1(self):
        sd.play(self.track_1_audio, self.fs)
        sd.wait()
    def Play_track_2(self):
        sd.play(self.track_2_audio, self.fs)
        sd.wait()
    def Mix(self):
        sd.play((self.x*self.track_0_audio)+(self.y*self.track_1_audio)+(self.z*self.track_2_audio), self.fs)
        sd.wait()

# object = Mixer()


#$Rev: 117 $
#$Author: fnight $
#$Date: 2009-08-20 18:16:18 +0400 (Чт, 20 авг 2009) $

#This file is part of Vitamin Project

from vitamin.modules.tpl.builtins import methods, modificators
import operator
from functools import partial
from collections import OrderedDict

builtin_methods = {x:getattr(methods, x) for x in dir(methods) if not x.startswith("_")}
builtin_modificators = {x:getattr(modificators, x) for x in dir(modificators) if not x.startswith("_")}

#переменаая, зависящая от контекста
class ContextVar(str):

    def __repr__(self):
        return "ctx.var:" + self.__str__()

#функция, зависящая от контекста
class ContextFunction():

    def __init__(self, name, *args, **kwargs):

        """
        Переменные должны быть только следующих типов:
            int, float, String, ContextVar, ContextFunction
        """

        self.name = name
        self.args = args
        self.kwargs = kwargs

class Context(dict):

    """
    Контекст - объект связывания переменных в шаблоне
    с переменными окружения. Каждая сессия отрисовки шаблона
    имеет свой контекст со своим набором переменных, изменяющихся
    в зависимости от ситуации. Контекст также позвляет шаблону
    получить доступ к определенным заранее модификаторам и методам
    обработки информации, которые расположены в модуле builtins.
    """

    def get(self, var):

        if isinstance(var, ContextVar):
            #разруливание переменных любой глубины var1.var2.var3...
            if "." in var:
                name, *path = var.split(".")
                getter = operator.attrgetter(path)
                if name in self:
                    getter(self[name])
                else:
                    raise Exception("context var failure: " + repr(name))
            else:
                if var in self:
                    return self[var]
                else:
                    raise Exception("context var failure: " + repr(var))

        elif isinstance(var, ContextFunction):
            #разруливание вызова функций с аргументами из шаблона
            function = None
            if var.name in self and hasattr(self[var.name], "__call__"):
                function = self[var.name]
            elif var.name in builtin_methods:
                function = builtin_methods[var.name]
            elif var.name in builtin_modificators:
                function = builtin_modificators[var.name]
            else:
                raise Exception("context function failure: " + repr(var.name))

            args = list(map(self.get, var.args))
            return partial(function, *args)

        else:
            #неразруливаемые типы(int, String, и проч.) просто возвращаются
            return var

class Aggregator(list):

    def __init__(self):
        self.mod_line = OrderedDict()
        self.aggregation = []

    def push_modificator(self, name, state):

        """
        Добавляет новое состояние модификатора в аггрегатор.
        Модификатор имеет состояние вкл/выкл(bool), и начинает
        применяться ко всем дальнейшим добавляемым строкам.

        Перед изменением состояния модификатора вызывает
        flush_mods().
        """

        assert name in builtin_modificators
        self.flush_mods()
        if not name in self.mod_line:
            self.mod_line[name] = []
        self.mod_line[name].append(state)

    def del_modificator(self, name):

        """
        Удаляет состояние модификатора из аггрегатора. Если
        удаляемое состояние - последнее из состояний модификатора,
        модификатор удаляется из аггрегатора.

        Перед изменением состояния модификатора вызывает
        flush_mods().
        """

        self.flush_mods()
        if self.mod_line[name]:
            del self.mod_line[name][-1]
        if not self.mod_line[name]:
            del self.mod_line[name]

    def slice(self):

        """
        Возвращает словарь - срез текущих состояних модификаторов,
        подставляя каждому активному модификатору только одно (последнее)
        состояние. Таким образом мы получаем возможность включать -
        отключать модификаторы в шаблоне по мере необходимости.
        """

        return OrderedDict(((x, self.mod_line[x][-1]) for x in self.mod_line))

    def append(self, item):

        """
        Операция добавления текста в аггрегатор.
        """

        if self.mod_line:
            self.aggregation.append(item)
        else:
            list.append(self, item)

    def flush_mods(self):

        """
        Операция применения модификаторов в состоянии текущего
        среза на накопленный буфер и сброс буфера. Применяется
        при изменении состояния какого- либо модификатора.
        """

        slice = self.slice()
        result = "".join(self.aggregation)
        for name, active in slice.items():
            if active:
                result = builtin_modificators[name](result)
        list.append(self, result)
        self.aggregation = []

    def join(self):

        """
        Свертка аггрегатора в результирующую строку
        """
        return "".join(self)


import fastf1 as ff1
import pandas as pd
import numpy as np
import math
from fastf1 import plotting
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.collections import LineCollection

# Enable the cache
ff1.Cache.enable_cache('cache')

# Enable plotting settings
ff1.plotting.setup_mpl()

# Choosing an f1 session
count = 0

# -------------- YEAR SESSION --------------
while count == 0:
    year = int(input("Choose a year between 2015 and 2022 for the session: "))
    if 2015 <= year <= 2022:
        grand_prix_list = ff1.get_event_schedule(year)
        print(grand_prix_list['Location'])
        count = 1
    else:
        print("\nYear not valid. Try again.")

# -------------- GRAND PRIX --------------
while count == 1:
    grand_prix = input("\nChoose a Grand Prix from the list:")
    for i in grand_prix_list['Location']:
        if i == grand_prix:
            count = 2

    if count == 1:
        print("\nGrand Prix not valid. Try Again")

# -------------- SESSION TYPE --------------
while count == 2:
    session_type = input("\nChoose session type: (FP1, FP2, FP3, Q, R): ")
    if session_type == "FP1" or session_type == "FP2" or session_type == "FP3" or session_type == "Q" or session_type == "R":
        session = ff1.get_session(year, grand_prix, session_type)
        count = 3
    else:
        print("\nSession not valid. Try again")

if count == 3:
    count = 4
    session.load()  # Load the session

# -------------- DRIVER 1 --------------
print("\nChoose one of the following driver.")
print(session.results['Abbreviation'])
while count == 4:
    driver_1 = input("\nWrite the driver 1:")
    for i in session.results['Abbreviation']:
        if i == driver_1:
            laps_driver_1 = session.laps.pick_driver(driver_1)
            fastest_driver_1 = laps_driver_1.pick_fastest()
            telemetry_driver_1 = fastest_driver_1.get_telemetry()

            count = 5

    if count == 4:
        print("\nDriver's abbreviation incorrect. Try again.")

# -------------- DRIVER 2 --------------
print("\nChoose one of the following driver.")
print(session.results['Abbreviation'])
while count == 5:
    driver_2 = input("\nWrite the driver 2:")
    for i in session.results['Abbreviation']:
        if i == driver_2:
            laps_driver_2 = session.laps.pick_driver(driver_2)
            fastest_driver_2 = laps_driver_2.pick_fastest()
            telemetry_driver_2 = fastest_driver_2.get_telemetry()

            count = -1

    if count == 5:
        print("\nDriver's abbreviation incorrect. Try again.")

# Variables for the plots
team_driver_1 = fastest_driver_1['Team']
team_driver_2 = fastest_driver_2['Team']

driver_speed_1 = telemetry_driver_1['Speed']
driver_speed_2 = telemetry_driver_2['Speed']

driver_throttle_1 = telemetry_driver_1['Throttle']
driver_throttle_2 = telemetry_driver_2['Throttle']

driver_brake_1 = telemetry_driver_1['Brake']
driver_brake_2 = telemetry_driver_2['Brake']

driver_gear_1 = telemetry_driver_1['nGear']
driver_gear_2 = telemetry_driver_2['nGear']

driver_distance_1 = telemetry_driver_1['Distance']
driver_distance_2 = telemetry_driver_2['Distance']

# Plot configuration
plot_size = [15,15]
plot_title = f"{session.event.year} {session.event.EventName} - {session.name} - {driver_1}"
plot_ratios = [1, 3, 2, 1, 1, 2, 1]
plot_filename = plot_title.replace(" ", " ") + ".png"

plt.rcParams['figure.figsize'] = [20, 15]
fig, ax = plt.subplots(7, gridspec_kw={'height_ratios': [5, 5, 5, 5, 5, 7, 7]})

# Subplot 1: Speed
ax[0].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Speed'], label=driver_1, color = ff1.plotting.team_color(team_driver_1))
ax[0].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Speed'], label=driver_2, color = ff1.plotting.team_color(team_driver_2))
ax[0].set(ylabel='Speed')
ax[0].legend(loc="lower right")

# Subplot 2: Throttle
ax[1].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Throttle'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[1].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Throttle'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[1].set(ylabel='Throttle')

# Subplot 3: Brake
ax[2].plot(telemetry_driver_1['Distance'], telemetry_driver_1['Brake'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[2].plot(telemetry_driver_2['Distance'], telemetry_driver_2['Brake'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[2].set(ylabel='Brake')

# Subplot 4: Gear
ax[3].plot(telemetry_driver_1['Distance'], telemetry_driver_1['nGear'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[3].plot(telemetry_driver_2['Distance'], telemetry_driver_2['nGear'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[3].set(ylabel='Gear')

# Subplot 5: RPM
ax[4].plot(telemetry_driver_1['Distance'], telemetry_driver_1['RPM'], label=driver_1, color=ff1.plotting.team_color(team_driver_1))
ax[4].plot(telemetry_driver_2['Distance'], telemetry_driver_2['RPM'], label=driver_2, color=ff1.plotting.team_color(team_driver_2))
ax[4].set(ylabel='RPM')

def longAcceleration():
    driver_time_1 = (telemetry_driver_1['Time'] / np.timedelta64(1, 's')).astype(float)
    driver_time_2 = (telemetry_driver_2['Time'] / np.timedelta64(1, 's')).astype(float)

    driver_speed_1 = telemetry_driver_1['Speed']
    driver_speed_2 = telemetry_driver_2['Speed']

    raw_acceleration_data_1 = []
    raw_acceleration_data_2 = []

    for i in driver_time_1.index:
        if i > 0 and i < driver_time_1.size:
            s_p = driver_speed_1.iloc[i-1]            # Previous speed
            s_f = driver_speed_1.iloc[i]              # Current speed
            t_p = driver_time_1.iloc[i-1]             # Previous time
            t_f = driver_time_1.iloc[i]               # Current time

            with np.errstate(divide='ignore', invalid = 'ignore'):
                acc = ((s_f - s_p) / 3.6) / (t_f - t_p)
                g_force = acc / 9.81
                raw_acceleration_data_1.append(g_force)

            acceleration_data_1 = [v for v in raw_acceleration_data_1 if not (math.isinf(v) or math.isnan(v) or v < -6.0 or v > 6)]

    for i in driver_time_2.index:
            if i > 0 and i < driver_time_2.size:
                s_p = driver_speed_2.iloc[i-1]          # Previous speed
                s_f = driver_speed_2.iloc[i]            # Current speed
                t_p = driver_time_2.iloc[i-1]           # Previous time
                t_f = driver_time_2.iloc[i]             # Current time

                with np.errstate(divide='ignore', invalid = 'ignore'):
                    acc = ((s_f - s_p) / 3.6) / (t_f - t_p)
                    g_force = acc / 9.81
                    raw_acceleration_data_2.append(g_force)

                acceleration_data_2 = [v for v in raw_acceleration_data_2 if not (math.isinf(v) or math.isnan(v) or v < -6.0 or v > 6)]

    # Subplot 5: Longitudinal Acceleration
    ax[5].plot(acceleration_data_1, label = driver_1, color = ff1.plotting.team_color(team_driver_1))
    ax[5].plot(acceleration_data_2, label = driver_2, color = ff1.plotting.team_color(team_driver_2))
    ax[5].set_ylabel("Long Acc")

def latAcceleration():
    if grand_prix == "Imola":
        if driver_1 == "VER" or driver_2 == "VER":
            driver = "VER"
            laps_driver = session.laps.pick_driver(driver)
            fastest_driver = laps_driver.pick_fastest()
            telemetry_driver = fastest_driver.get_telemetry()
            team_driver = fastest_driver['Team']

            driver_time = (telemetry_driver['Time'] / np.timedelta64(1, 's')).astype(float)
            driver_speed = telemetry_driver['Speed']

            excel_data = pd.read_excel('Dataset/tempi_curve.xlsx')                  # Load xlsx file
            df = pd.DataFrame(excel_data)                                           # Read the values of the file in the dataframe

            raw_acceleration_data = []

            for i in df.index:
                for j in driver_time.index:
                    if df['Start time'][i] <= driver_time[j] <= df['End time'][i]:
                        radius = df['Radius'][i]
                        speed = driver_speed[j]

                        with np.errstate(divide='ignore', invalid = 'ignore'):
                                speed_ms = speed / 3.6                               # Speed in meters/seconds
                                acceleration = (math.pow(speed_ms, 2)) / radius
                                g_force = acceleration / 9.81
                                raw_acceleration_data.append(g_force)

                        acceleration_data = [v for v in raw_acceleration_data if not (math.isinf(v) or math.isnan(v) or v > 6.0 or v == 0)]

            # Subplot 6: Lateral Acceleration
            ax[6].plot(acceleration_data, label = driver, color = ff1.plotting.team_color(team_driver))
            ax[6].set(ylabel='Lat Acc')

longAcceleration()
latAcceleration()

# using padding
fig.tight_layout(pad=2.5)
plt.show()


import streamlit as st
from miscellanious_functions import *
from ClarkeAndWrightSavingsAlgorithm import ClarkeAndWrightSavingsAlgorithmWithVehConstraint
from MDVRP_KMeansFunc import KMeansClusteringBasedNodesSelection
from MDVRP_HierClustFunc import HierClusteringBasedNodeSelection
import datetime

st.set_page_config(page_title = "Multi-Depot Vehicle Routing Problem Simulator",
page_icon="🚚",
layout="wide"
)

st.title("Multi-Depot Vehicle Routing Problem Simulator")
st.subheader("Batch Simulations")

with st.sidebar:
    st.subheader("Problem Instances")

    noc = st.number_input('Insert number of clients 🏬',
    min_value=1,
    max_value=1000,
    value=50,
    step=1,
    help="Insert the total number of clients that need to be served"
    )

    nos = st.slider("Choose seed range 🌱",
    min_value=1,
    max_value=200,
    value=20,
    step=1,
    help="Seed range for which mass testing will be implemented")

    nov = st.number_input('Insert number of vehicles 🚚',
    min_value=1,
    max_value=20,
    value=2,
    step=1,
    help="The number of vehicles each depot has"
    )

    nod = st.number_input('Insert number of depots 🏭',
    min_value=2,
    max_value=20,
    value=2,
    step=1,
    help="The total number of exisitng depots"
    )

    nog = st.number_input('Insert grid size 📏',
    min_value=20,
    max_value=200,
    value=100,
    step=5,
    help="Grid is the size of the plot's height and width. It is also the maximum value a node's x and y coordinates can be equal to"
    )

    novc = st.number_input('Insert vehicle capacity 📦',
    min_value=2,
    max_value=800,
    value=50,
    step=1,
    help="The maximum capacity of each truck"
    )

    nomd = st.number_input('Insert maximum customer demand 📬',
    min_value=1,
    max_value=20,
    value=3,
    step=1,
    help="The maximum demand a customer can have"
    )

if st.button('Start Batch Simulations'):
    dir_path = os.getcwd() # Get the current working directory

    # Loop through all the files in the current directory
    for file_name in os.listdir(dir_path):
        if file_name.endswith(".xlsx"):  # Check if the file is an xlsx file
            file_path = os.path.join(dir_path, file_name)
            if os.path.exists(file_path):
                os.remove(file_path)

    with st.spinner('Wait for it...'):
        start_time = time.time()
        BatchTestingExcelWriter(7, 1, nos, noc, nov, nod, nog, novc, nomd)
        st.write("..Done")
        end_time = time.time()
        st.write("Elapsed time:", str(datetime.timedelta(seconds=end_time-start_time)))

        dir_path = os.getcwd()
        for file_name in os.listdir(dir_path):
            if file_name.endswith(".xlsx"):
                file_path = os.path.join(dir_path, file_name)
                with open(file_path, 'rb') as file:
                    file_contents = file.read()
                    if st.download_button(label='📥 Export Excel File', data=file, file_name="Simulations.xlsx", mime="text/xlsx", key=2):
                        break


from django.db.models.signals import post_save, pre_delete
from django.contrib.auth import get_user_model
from django.dispatch import receiver

from profiles.models import Profile, Relationship

@receiver(post_save, sender=get_user_model())
def post_save_create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=Relationship)
def post_save_add_to_friends(sender, instance, created, **kwargs):
    if instance.status == Relationship.StatusChoices.ACCEPTED:
        sender_profile = instance.sender
        receiver_profile = instance.receiver

        sender_profile.friends.add(receiver_profile.user)
        receiver_profile.friends.add(sender_profile.user)

        sender_profile.save()
        receiver_profile.save()

@receiver(pre_delete, sender=Relationship)
def pre_delete_remove_from_friends(sender, instance, **kwargs):
    sender = instance.sender
    receiver = instance.receiver

    sender.friends.remove(receiver.user)
    receiver.friends.remove(sender.user)

    sender.save()
    receiver.save()


# -*- coding: utf-8 -*-
"""
Created on Tue Apr 13 18:41:38 2021

@author: divyoj
"""# -*- coding: utf-8 -*-
"""
Created on Tue Apr 13 18:41:38 2021

@author: divyoj
"""
## importing libraries:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
import os
# # note that this must be executed before 'import numba'
# os.environ['NUMBA_DISABLE_INTEL_SVML'] = '1'
from numba import njit
import time as process_time
import plotting
from scipy.integrate import solve_ivp
@njit
def do_timestep(t,z,aT,bT,alpha, beta, gamma, zeta):
    ''' function to give dxdt at a time step '''

    aL = z[0*(nx*ny):1*(nx*ny)].reshape((ny,nx))
    bL = z[1*(nx*ny):2*(nx*ny)].reshape((ny,nx))
    aR = z[2*(nx*ny):3*(nx*ny)].reshape((ny,nx))
    bR = z[3*(nx*ny):4*(nx*ny)].reshape((ny,nx))

    # total membrane protein concentration:
    a0 = aL + aR
    b0 = bL + bR
    # intitating dxdt to arrays of zeros:
    daL=np.zeros((ny,nx));daR=np.zeros((ny,nx));dbL=np.zeros((ny,nx));dbR=np.zeros((ny,nx));
    ## Equations for al,aR,bl and bR:
    # aL
    daL[0,1:nx-1]=alpha*(aT[0,1:nx-1]-a0[0,1:nx-1])-beta*aL[0,1:nx-1]+beta*gamma*( aL[0,1:nx-1]*bR[0,1-1:nx-1-1] ) -zeta*(aL[0,1:nx-1]-aR[0,1:nx-1])**3;
    # aR
    daR[0,1:nx-1]=alpha*(aT[0,1:nx-1]-a0[0,1:nx-1])-beta*aR[0,1:nx-1]+beta*gamma*( aR[0,1:nx-1]*bL[0,1+1:nx-1+1] ) -zeta*(aR[0,1:nx-1]-aL[0,1:nx-1])**3;
    # bL
    dbL[0,1:nx-1]=alpha*(bT[0,1:nx-1]-b0[0,1:nx-1])-beta*bL[0,1:nx-1]+beta*gamma*( bL[0,1:nx-1]*aR[0,1-1:nx-1-1] ) -zeta*(bL[0,1:nx-1]-bR[0,1:nx-1])**3;
    # bR
    dbR[0,1:nx-1]=alpha*(bT[0,1:nx-1]-b0[0,1:nx-1])-beta*bR[0,1:nx-1]+beta*gamma*( bR[0,1:nx-1]*aL[0,1+1:nx-1+1] ) -zeta*(bR[0,1:nx-1]-bL[0,1:nx-1])**3;

    # Boundary Conditions:
    #aL
    daL[0,0]=daL[0,1];
    daL[0,nx-1]=alpha*(aT[0,nx-1]-a0[0,nx-1])-beta*aL[0,nx-1]+beta*gamma*(aL[0,nx-1]*bR[0,nx-1-1])-zeta*(aL[0,nx-1]-aR[0,nx-1])**3;

    #aR
    daR[0,0]=alpha*(aT[0,0]-a0[0,0])-beta*aR[0,0]+beta*gamma*( aR[0,0]*bL[0,1] ) -zeta*(aR[0,0]-aL[0,0])**3;
    daR[0,nx-1]=daR[0,nx-2];

    #bL
    dbL[0,0]=dbL[0,1];
    dbL[0,nx-1]=alpha*(bT[0,nx-1]-b0[0,nx-1])-beta*bL[0,nx-1]+beta*gamma*(bL[0,nx-1]*aR[0,nx-1-1])-zeta*(bL[0,nx-1]-bR[0,nx-1])**3;

    #bR
    dbR[0,0]=alpha*(bT[0,0]-b0[0,0])-beta*bR[0,0]+beta*gamma*( bR[0,0]*aL[0,1] ) -zeta*(bR[0,0]-bL[0,0])**3;
    dbR[0,nx-1]=dbR[0,nx-2];

    # checking if membrane concentration went above total concetration:
    for i in range(ny):
        for j in range(nx):
            if a0[i,j]>aT[i,j]:

                if daL[i,j]>0:
                    daL[i,j]=0
                if daR[i,j]>0:
                    daR[i,j]=0

            if b0[i,j]>bT[i,j]:
                if dbL[i,j]>0:
                    dbL[i,j]=0
                if dbR[i,j]>0:
                    dbR[i,j]=0

            if aL[i,j]<=0:
                daL[i,j]=0
            if bL[i,j]<=0:
                dbL[i,j]=0

            if aR[i,j]<=0:
                daR[i,j]=0
            if bR[i,j]<=0:
                dbR[i,j]=0

    return np.concatenate((daL.flatten(),dbL.flatten(),daR.flatten(),dbR.flatten()))

#@njit
def simulate(rho,alpha, beta, gamma, zeta):
    ''' function to iterate over time and return arrays with the result '''
    ## initilizing the arrays to store the values over time:
    aL_t = np.zeros((T_max+1,ny,nx)); aR_t = np.zeros((T_max+1,ny,nx));
    bL_t = np.zeros((T_max+1,ny,nx)); bR_t = np.zeros((T_max+1,ny,nx));

    # total proteins in the cells
    aT = rho + np.zeros((ny,nx))
    bT = rho + np.zeros((ny,nx))

    ## initializing aL,bR,bL,aR
    aL = np.zeros((ny,nx)) + 0.6*rho; aR = np.zeros((ny,nx)) + 0.1*rho
    bL = np.zeros((ny,nx)) + 0.1*rho; bR = np.zeros((ny,nx)) + 0.6*rho

    ## Deletion in one range:
    aL[0,:int(nx/2)]=aR[0,:int(nx/2)]=aT[0,:int(nx/2)]=0

    ## Collecting the initial conditions into a single array:
    ic = np.array(aL.flatten().tolist()+bL.flatten().tolist()+aR.flatten().tolist()+bR.flatten().tolist())

    ## Solving the initial value problem:
    sol = solve_ivp(lambda t,y: do_timestep(t,y,aT,bT,alpha, beta, gamma, zeta),t_span=[0,T_max],y0=ic,t_eval=list(np.linspace(0,T_max,T_max+1)))

    t = sol.t
    aball = sol.y

    for t_index, ts in enumerate(t):

        aball_at_ts = aball[:,t_index]

        aL_t[t_index]= aball_at_ts[0*(nx*ny):1*(nx*ny)].reshape((ny,nx));
        bL_t[t_index]= aball_at_ts[1*(nx*ny):2*(nx*ny)].reshape((ny,nx));
        aR_t[t_index]= aball_at_ts[2*(nx*ny):3*(nx*ny)].reshape((ny,nx));
        bR_t[t_index]= aball_at_ts[3*(nx*ny):4*(nx*ny)].reshape((ny,nx));

    #return (aL_t[:,:,10:nx-10],aR_t[:,:,10:nx-10],bL_t[:,:,10:nx-10],bR_t[:,:,10:nx-10])
    return (aL_t,bL_t,aR_t,bR_t)

if __name__ == "__main__":
    # Lattice:
    w,h = 10,2;
    dx,dy=0.01,0.1;
    nx=int(w/dx)
    ny=1;#int(h/dx);
    # time:
    T_max=100;

    # parameters:
    alpha=10;
    gamma=1; beta=1;
    zeta=0.0;

#%% Characterisation over rho:
    # folder for storing the data:
    folder="./"+"zeta="+str(zeta)+"_alpha="+str(alpha)+"/"
    if not os.path.exists(folder):
        os.makedirs(folder)

    rhos = np.around(np.linspace(0.8,1.2,21),2);rho0_array=rhos.copy();
    p_array=rhos.copy();delta_p_array=rhos.copy()

    for ri, rho in enumerate(rhos):
        print (ri, rho)

        aL_t, bL_t, aR_t, bR_t = simulate(rho,alpha, beta, gamma, zeta)

         # Plotting at each rho
        #%% Plotting at each rho:
        rho0_array[ri],p_array[ri],delta_p_array[ri]=plotting.plots_at_rho(aL_t,bL_t,aR_t,bR_t,rho,folder)

    # Plotting final graphs after iterating over rhos
    plotting.plots_over_rho(rhos,rho0_array,p_array,delta_p_array,folder)


from django.shortcuts import render
from django.http import HttpResponse

from . import forms

## Initalize dictionary of neighborhood coordinates
neighborhood_coord_dict = forms.get_neighborhood_dict()
c = {"neighborhoods": neighborhood_coord_dict}

## Blank/unknown site page
def blank(request):
	return render(request, "maps/default.html")

## Main site page
def healthy_neighborhoods(request):
	c["form"] = forms.Variable()

	if request.method == "POST":
		if "ex_1" in request.POST:
			var_1 = "Below Poverty Level"
			var_2 = "Childhood Blood Lead Level Screening"
		elif "ex_2" in request.POST:
			var_1= "Assault (Homicide)"
			var_2 = "Per Capita Income"
		elif "ex_3" in request.POST:
			var_1 = "Dependency"
			var_2 = "Firearm-related"
		else:
			form = forms.Variable(request.POST)
			if form.is_valid():
				var_1 = form.cleaned_data["variable_1"]
				var_2 = form.cleaned_data["variable_2"]

		c["variables"] = [var_1.lower(), var_2.lower()]

		coef, neighborhood_list = forms.get_result_list(var_1, var_2)

		c["coef"] = '{0:.3f}'.format(coef)

		## Add color identifiers to neighborhood dictionaries
		forms.get_combo_dict(neighborhood_list, neighborhood_coord_dict)

		return render(request, "maps/healthy_neighborhoods.html", c)
	return render(request, "maps/healthy_neighborhoods_start.html", c)

## Site documentaion page
def documentation(request):
	return render(request, "maps/documentation.html")


import control as ct
import numpy as np

# %% Projeto do Compensador
class Compensador:
    def __init__(self, planta, P, Q):

        # Ordem do sistema
        n = planta.A.shape[0]

        # Projeto numérico do regulador integral
        Aa = np.block([[planta.A, np.zeros((n, 1))], [-planta.C, 0]])
        Ba = np.block([[planta.B], [0]])
        Ka = ct.acker(Aa, Ba, P)
        K = Ka[:, :n]
        Ki = Ka[:, n]

        # Projeto numérico do observador
        L = ct.acker(planta.A.T, planta.C.T, Q).T

        # Matrizes para implementação do compensador
        Ar = np.block(
            [[planta.A-planta.B@K-L@planta.C, -planta.B*Ki], [np.zeros((1, n+1))]])
        Br = np.block([[L, np.zeros((n, 1))], [-1, 1]])

        # Armazena atributos do objeto
        self.Ar = Ar
        self.Br = Br
        self.K = K[0, :]
        self.Ki = Ki
        self.Ka = Ka[0, :]
        self.L = L

    # Definindo os sinais de referência para rastreamento
    @staticmethod
    def ref_seno(t):
        return (mag.x0*np.sin(2*pi*t))

    @staticmethod
    def ref_quad(t):
        return (mag.x0)*(np.sin(2*pi*t) >= 0)


import torch
import torch.nn as nn
import torch.nn.functional as F
from multi_head_att.submodules import MultiHeadAttention, PositionwiseFeedForward, PositionalEncoding

def masked_mean(input, mask=None, dim=1):
    # input: [batch_size, seq_len, hidden_size]
    # mask: Float Tensor of size [batch_size, seq_len], where 1.0 for unmask, 0.0 for mask ones
    if mask is None:
        return torch.mean(input, dim=dim)
    else:
        mask = mask.unsqueeze(-1)
        mask_input = input * mask
        sum_mask_input = mask_input.sum(dim=dim)
        for dim in range(mask.size(0)):
            sum_mask_input[dim] = sum_mask_input[dim] / mask[dim].sum()
        return sum_mask_input

def masked_max(input, mask=None, dim=1):
    # input: [batch_size, seq_len, hidden_size]
    # mask: Float Tensor of size [batch_size, seq_len], where 1.0 for unmask, 0.0 for mask ones
    if mask is None:
        max_v, _ = torch.max(input, dim=dim)
        return max_v
    else:
        mask = mask.unsqueeze(-1)
        mask = mask.repeat(1, 1, input.size(-1))
        input = input.masked_fill(mask == 0.0, float('-inf'))
        max_v, _ = torch.max(input, dim=dim)
        return max_v

class MaskedSoftmax(nn.Module):
    def __init__(self, dim):
        super(MaskedSoftmax, self).__init__()
        self.dim = dim

    def forward(self, logit, mask=None):
        if mask is None:
            dist = F.softmax(logit - torch.max(logit, dim=self.dim, keepdim=True)[0], dim=self.dim)
        else:
            dist_ = F.softmax(logit - torch.max(logit, dim=self.dim, keepdim=True)[0], dim=self.dim) * mask
            normalization_factor = dist_.sum(self.dim, keepdim=True)
            dist = dist_ / normalization_factor
        return dist

class Attention(nn.Module):
    def __init__(self, decoder_size, memory_bank_size, need_mask=False):
        super(Attention, self).__init__()
        self.need_mask = need_mask
        self.v = nn.Linear(decoder_size, 1, bias=False)
        self.decode_project = nn.Linear(decoder_size, decoder_size)
        self.memory_project = nn.Linear(memory_bank_size, decoder_size, bias=False)
        self.softmax = MaskedSoftmax(dim=1)
        self.tanh = nn.Tanh()
        self.linear = nn.Linear(decoder_size + memory_bank_size, memory_bank_size)

    def score(self, memory_bank, decoder_state):
        """
        :param memory_bank: [batch_size, max_src_len, self.bi_hidden_size]
        :param decoder_state: [batch_size, decoder_size]
        :return: score: [batch_size, max_src_len]
        """
        batch_size, max_src_len, memory_bank_size = list(memory_bank.size())
        decoder_size = decoder_state.size(1)

        # Bahdanau style attention
        # project memory_bank
        memory_bank_ = memory_bank.view(-1, memory_bank_size)  # [batch_size*max_src_len, memory_bank_size]
        encoder_feature = self.memory_project(memory_bank_)  # [batch_size*max_src_len, decoder size]
        # project decoder state
        dec_feature = self.decode_project(decoder_state)  # [batch_size, decoder_size]
        dec_feature_expanded = dec_feature.unsqueeze(1).expand(batch_size, max_src_len, decoder_size).contiguous()
        dec_feature_expanded = dec_feature_expanded.view(-1, decoder_size)  # [batch_size*max_src_len, decoder_size]
        # sum up attention features
        att_features = encoder_feature + dec_feature_expanded  # [batch_size*max_src_len, decoder_size]

        # compute attention score and normalize them
        e = self.tanh(att_features)  # [batch_size*max_src_len, decoder_size]
        scores = self.v(e)  # [batch_size*max_src_len, 1]

        scores = scores.view(-1, max_src_len)  # [batch_size, max_src_len]
        return scores

    def forward(self, decoder_state, memory_bank, src_mask=None, return_attn=False):
        """
        :param decoder_state: [batch_size, decoder_size]
        :param memory_bank: [batch_size, max_input_seq_len, self.bi_hidden_size]
        :param src_mask: [batch_size, max_input_seq_len]
        :return: context, attn_dist
        """
        # init dimension info
        batch_size, max_src_len, memory_bank_size = list(memory_bank.size())

        if self.need_mask:
            assert src_mask is not None

        # if src_mask is None:  # if it does not supply a source mask, create a dummy mask with all ones
        #     src_mask = memory_bank.new_ones(batch_size, max_src_len)

        scores = self.score(memory_bank, decoder_state)
        attn_dist = self.softmax(scores, mask=src_mask)

        # Compute weighted sum of memory bank features
        attn_dist = attn_dist.unsqueeze(1)  # [batch_size, 1, max_input_seq_len]
        memory_bank = memory_bank.view(-1, max_src_len, memory_bank_size)  # batch_size, max_src_len, memory_bank_size]
        context = torch.bmm(attn_dist, memory_bank)  # [batch_size, 1, memory_bank_size]
        context = context.squeeze(1)  # [batch_size, memory_bank_size]
        attn_dist = attn_dist.squeeze(1)  # [batch_size, max_input_seq_len]

        # Update coverage
        assert attn_dist.size() == torch.Size([batch_size, max_src_len])
        assert context.size() == torch.Size([batch_size, memory_bank_size])

        context_output = self.linear(torch.cat([context, decoder_state], dim=1))
        if return_attn:
            return context_output, attn_dist
        return context_output

class CoAttention(nn.Module):
    def __init__(self, text_feat_size, img_feat_size, input_type='text_img'):
        """Initialize model."""
        super(CoAttention, self).__init__()
        self.text_feat_size = text_feat_size
        self.img_feat_size = img_feat_size
        self.input_type = input_type
        assert input_type in ['text_img', 'img_text', 'text_text']
        self.v_text = nn.Linear(text_feat_size, 1, bias=False)
        self.v_img = nn.Linear(img_feat_size, 1, bias=False)
        self.text2img_project = nn.Linear(text_feat_size, img_feat_size, bias=False)
        self.img2text_project = nn.Linear(img_feat_size, text_feat_size, bias=False)
        self.img_project = nn.Linear(img_feat_size, img_feat_size)
        self.text_project = nn.Linear(text_feat_size, text_feat_size)
        self.softmax = MaskedSoftmax(dim=1)
        self.linear = nn.Linear(text_feat_size + img_feat_size, text_feat_size)

    def text_att_scores(self, text_feat, img_feats):
        batch_size, img_num, img_feat_size = list(img_feats.size())
        batch_size, text_feat_size = list(text_feat.size())

        img_feats_ = img_feats.view(-1, img_feat_size)  # [batch_size*img_num, img_feat_size]
        img_feature = self.img2text_project(img_feats_)  # [batch_size*img_num, text_feat_size]

        # Project decoder state: text_feats (in our case)
        text_feature = self.text_project(text_feat)  # [batch_size, text_feat_size]
        text_feature_expanded = text_feature.unsqueeze(1).expand(batch_size, img_num, text_feat_size).contiguous()
        text_feature_expanded = text_feature_expanded.view(-1, text_feat_size)  # [batch_size*img_num, text_feat_size]

        # sum up attention features
        att_features = img_feature + text_feature_expanded  # [batch_size*img_num, text_feat_size]
        e = torch.tanh(att_features)  # [batch_size*img_num, text_feat_size]
        scores = self.v_text(e)  # [batch_size*img_num, 1]
        scores = scores.view(-1, img_num)  # [batch_size, img_num]
        return scores

    def img_att_scores(self, img_feat, text_feats):
        batch_size, max_src_len, text_feat_size = list(text_feats.size())
        batch_size, img_feat_size = list(img_feat.size())

        text_feats_ = text_feats.view(-1, text_feat_size)  # [batch_size*max_src_len, text_feat_size]
        text_feature = self.text2img_project(text_feats_)  # [batch_size*max_src_len, img_feat_size]

        # Project decoder state: text_feats (in our case)
        img_feature = self.img_project(img_feat)  # [batch_size, img_feat_size]
        img_feature_expanded = img_feature.unsqueeze(1).expand(batch_size, max_src_len, img_feat_size).contiguous()
        img_feature_expanded = img_feature_expanded.view(-1, img_feat_size)  # [batch_size*max_src_len, img_feat_size]

        # sum up attention features
        att_features = text_feature + img_feature_expanded  # [batch_size*max_src_len, img_feat_size]
        e = torch.tanh(att_features)  # [batch_size*max_src_len, img_feat_size]
        scores = self.v_img(e)  # [batch_size*max_src_len, 1]
        scores = scores.view(-1, max_src_len)  # [batch_size, max_src_len]
        return scores

    def forward(self, text_feats, img_feats, src_mask):
        # Text
        batch_size, img_num, img_feat_size = list(img_feats.size())
        batch_size, max_src_len, text_feat_size = list(text_feats.size())

        if self.input_type in ['text_img', 'text_text']:
            text_feat = masked_mean(text_feats, src_mask, dim=1)
        elif self.input_type in ['img_text']:
            text_feat = torch.mean(text_feats, dim=1)

        text_scores = self.text_att_scores(text_feat, img_feats)

        if self.input_type in ['text_img']:
            text_att_dist = self.softmax(text_scores)
        elif self.input_type in ['img_text', 'text_text']:
            text_att_dist = self.softmax(text_scores, mask=src_mask)

        text_att_dist = text_att_dist.unsqueeze(1)  # [batch_size, 1, img_num]
        img_feats = img_feats.view(-1, img_num, img_feat_size)  # batch_size, img_num, img_feat_size]
        img_context = torch.bmm(text_att_dist, img_feats)  # [batch_size, 1, img_feat_size]
        img_context = img_context.squeeze(1)  # [batch_size, img_feat_size]

        img_scores = self.img_att_scores(img_context, text_feats)

        if self.input_type in ['text_img', 'text_text']:
            img_att_dist = self.softmax(img_scores, mask=src_mask)
        elif self.input_type in ['img_text']:
            img_att_dist = self.softmax(img_scores)

        img_att_dist = img_att_dist.unsqueeze(1)  # [batch_size, 1, max_src_len]
        text_feats = text_feats.view(-1, max_src_len, text_feat_size)  # [batch_size, max_src_len, text_feat_size]
        text_context = torch.bmm(img_att_dist, text_feats)  # [batch_size, 1, text_feat_size]
        text_context = text_context.squeeze(1)  # [batch_size, text_feat_size]

        combined_features = torch.cat([img_context, text_context], dim=1)
        combined_features = self.linear(combined_features)
        return combined_features

class MyMultiHeadAttention(nn.Module):
    def __init__(self, n_head, d_model, d_kv, dropout=0.1, need_mask=False, is_regu=False):
        super(MyMultiHeadAttention, self).__init__()
        self.need_mask = need_mask
        self.is_regu = is_regu
        self.slf_attn = MultiHeadAttention(n_head, d_model, d_kv, d_kv, dropout=dropout, is_regu=is_regu)
        self.pos_ffn = PositionwiseFeedForward(d_model, d_model, dropout=dropout)

    def forward(self, q, k, v, mask=None):
        # q: [batch_size, d_model] ==>  k: [batch_size, 1, d_model]
        # mask: [batch_size, seq_len] == > [batch_size, 1, seq_len]
        # when there is only one query, we need to expand the dimension
        if len(q.shape) == 2:
            q = q.unsqueeze(1)
        if mask is not None:
            mask = mask.unsqueeze(1)  # [batch_size, 1, seq_len]
        if self.need_mask:
            assert mask is not None, 'Please pass the attention mask to the multi-head'
        if self.is_regu:
            enc_output, enc_slf_attn, head_diff = self.slf_attn(q, k, v, mask)
        else:
            enc_output, enc_slf_attn = self.slf_attn(q, k, v, mask)
        enc_output = self.pos_ffn(enc_output)

        # enc_output: [batch_size, 1, d_model] ==>  k: [batch_size, d_model]
        enc_output = enc_output.squeeze(1)
        if self.is_regu:
            return enc_output, enc_slf_attn, head_diff
        return enc_output, enc_slf_attn

class MyTransformer(nn.Module):
    def __init__(self, n_layers, n_head, d_model, d_kv, emb_size=200, dropout=0.1):
        super(MyTransformer, self).__init__()

        self.position_enc = PositionalEncoding(emb_size, n_position=200)
        self.dropout = nn.Dropout(p=dropout)
        self.layer_stack = nn.ModuleList([
            MyMultiHeadAttention(n_head, d_model, d_kv, dropout=dropout)
            for _ in range(n_layers)])
        self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)

    def forward(self, src_seq_embed, memory_bank, src_mask, return_attns=False):

        enc_slf_attn_list = []

        enc_output = self.dropout(self.position_enc(src_seq_embed))
        # (enc_output, memory_bank, memory_bank, src_mask)
        for enc_layer in self.layer_stack:
            enc_output, enc_slf_attn = enc_layer(enc_output, memory_bank, memory_bank, src_mask)
            enc_slf_attn_list += [enc_slf_attn] if return_attns else []

        enc_output = self.layer_norm(enc_output)

        if return_attns:
            return enc_output, enc_slf_attn_list
        return enc_output


#!/usr/bin/python3

import os
import sys
import string
from pathlib import Path

word_freqs = []

with open(os.path.join(Path.cwd(), 'res/ignore-words.txt')) as f:
    ignore_words = f.read().split(',')
ignore_words.extend(list(string.ascii_lowercase))

for line in open(sys.argv[1]):
    start_index = None
    i = 0
    for c in line:
        if start_index == None:
            if c.isalnum():
                start_index = i
        else:
            if not c.isalnum():
                found = False
                word = line[start_index:i].lower()
                if word not in ignore_words:
                    pair_index = 0
                    for pair in word_freqs:
                        if word == pair[0]:
                            pair[1] += 1
                            found = True
                            break
                        pair_index += 1
                    if not found:
                        word_freqs.append([word, 1])
                    elif len(word_freqs) > 1:
                        for n in reversed(range(pair_index)):
                            if word_freqs[pair_index][1] > word_freqs[n][1]:
                                word_freqs[n], word_freqs[pair_index] = word_freqs[pair_index], word_freqs[n]
                                pair_index = n
                start_index = None
        i += 1

for wf in word_freqs[0:25]:
    print(f"{wf[0]} - {wf[1]}")


"""
fixtures for unit tests
"""

from typing import List, Dict

import pytest
from transformers import AutoTokenizer

from text_classification.dataset_utils import (
    InputMultilabelExample,
    MultilabelDataset,
    OutputMultilabelExample)

@pytest.fixture
def num_labels() -> int:
    """number of class labels"""
    return 4

@pytest.fixture
def class_labels(num_labels: int) -> List[str]:
    """list of class labels (strings)"""
    return ["Label " + str(i) for i in range(num_labels)]

@pytest.fixture
def output_multilabel_examples(class_labels: List[str]) -> List[OutputMultilabelExample]:
    """list of 10 output multi-label examples, each labeled with all class labels
       with confidences 0.9"""
    confidences = [0.9] * len(class_labels)
    return [OutputMultilabelExample(str(i), "Text " + str(i), class_labels, confidences)
            for i in range(10)]

@pytest.fixture
def input_multilabel_examples(class_labels: List[str]) -> List[InputMultilabelExample]:
    """list of 10 input multi-label examples, each labeled with all class labels"""
    return [InputMultilabelExample(str(i), "Text " + str(i), class_labels)
            for i in range(10)]

@pytest.fixture
def input_multilabel_examples_without_labels() -> List[InputMultilabelExample]:
    """list of 10 input multi-label examples with class_labels set to None"""
    return [InputMultilabelExample(str(i), "Text " + str(i), None)
            for i in range(10)]

@pytest.fixture
def multilabel_dataset(
        input_multilabel_examples: List[InputMultilabelExample],
        class_labels: List[str]) -> MultilabelDataset:
    """
    dummy multilabel dataset
    """
    tokenizer = AutoTokenizer.from_pretrained("roberta-base")
    max_length = 128
    return MultilabelDataset(
        input_multilabel_examples,
        class_labels,
        tokenizer,
        max_length,
        predict=False
    )

@pytest.fixture
def input_multilabel_example_dictionaries(input_multilabel_examples) -> List[Dict]:
    """
    list of dictionaries containing multilabel examples, e.g.:
       [{"guid": "0",
         "text": "Text 0",
         "labels": ["Label 0", "Label 1"]},
        {"guid": "1",
         "text": "Text 1",
         "labels": ["Label 0"]}]
    """
    return [multilabel_example.__dict__
            for multilabel_example in input_multilabel_examples]

@pytest.fixture
def input_multilabel_example_dictionaries_without_labels(
        input_multilabel_examples_without_labels) -> List[Dict]:
    """
    list of dictionaries containing multilabel examples without labels, e.g.:
       [{"guid": "0",
         "text": "Text 0"},
        {"guid": "1",
         "text": "Text 1"}]
    """
    return [{"guid": multilabel_example.guid,
             "text": multilabel_example.text}
            for multilabel_example in input_multilabel_examples_without_labels]