text:
import torch
# import torch.nn as nn
# import torch.nn.functional as F
import torch.optim as optim
# from torch.optim.lr_scheduler import ReduceLROnPlateau, ExponentialLR
from beam import BeamSearch
from tensorboardX import SummaryWriter
from utils import batch_bleu, bert_dual_sequence_mask
import os
import logging
from utils import mkdir
from dotmap import DotMap
import tqdm
import json
# import sacrebleu
import numpy as np
# from transformers import BertModel, RobertaModel
from itertools import chain
from bert_score import BERTScorer
# from itertools import islice

from data import bos_token

from pytorch_memlab import profile

scorer = BERTScorer(lang="en")
bert_score = scorer.score

# __transformers__ = [BertModel, RobertaModel]

def build_trainer(model, args, datamaker, phase="train"):
    if phase not in ["train"]:
        raise NotImplementedError(
            "PRETRAIN and TUNE modes to be implemented, only TRAIN mode is supported"
        )

    trainer = Trainer(
        model,
        patience=args.patience,
        # val_interval=100,
        serialization_dir=args.serialization_dir,
        # max_vals=50,
        device=args.device,
        clip_grad_norm_val=args.clip,
        initial_lr=args.initial_lr,
        lr_decay=None,
        min_lr=args.min_lr,
        lr_patience=args.lr_patience,
        keep_all_checkpoints=args.keep_all_checkpoints,
        val_data_limit=args.val_data_limit,
        max_epochs=args.max_epochs,
        training_data_fraction=args.training_data_fraction,
        beam_size=args.beam_size,
        min_length=args.min_length,
        max_length=args.max_length,
        n_best=1,
        ratio=None,
        datamaker=datamaker,
        lr_scheduling_metric=args.lr_scheduling_metric,
        metric_decreases=args.metric_decreases,
        load_model=args.load_model,
        load_optimizer=args.load_optimizer,
        kl_reach_point=args.kl_reach_point,
        warmup_steps=args.warmup_steps,
        validation_interval=args.validation_interval,
    )

    return trainer

logging.basicConfig(
    filename="app.log",
    filemode="w",
    format="%(name)s - %(levelname)s - %(message)s",
    level=logging.DEBUG,
)

class Trainer(object):
    def __init__(
        self,
        model,
        patience=4,
        # val_interval=100,
        serialization_dir=None,
        device="cuda",
        clip_grad_norm_val=None,
        initial_lr=None,
        lr_decay=None,
        min_lr=None,
        lr_patience=None,
        keep_all_checkpoints=False,
        val_data_limit=None,
        max_epochs=-1,
        training_data_fraction=0,
        beam_size=1,
        min_length=3,
        max_length=512,
        n_best=1,
        ratio=None,
        datamaker=None,
        lr_scheduling_metric=None,
        metric_decreases=None,
        kl_reach_point=None,
        load_model=None,
        load_optimizer=None,
        warmup_steps=None,
        validation_interval=None,
    ):
        """
        The training coordinator. Unusually complicated to handle MTL with tasks of
        diverse sizes.
        Parameters
        ----------
        model : ``Model``, required.
            An PyTorch model to be optimized. Can  be optimized if
            their ``forward`` method returns a dictionary with a "loss" key, containing a
            scalar tensor representing the loss function to be optimized.
        patience , optional (default=2)
            Number of validations to be patient before early stopping.
        val_metric , optional (default="loss")
            Validation metric to measure for whether to stop training using patience
            and whether to serialize an ``is_best`` model after each validation. The metric name
            must be prepended with either "+" or "-", which specifies whether the metric
            is an increasing or decreasing function.
        serialization_dir , optional (default=None)
            Path to directory for saving and loading model files. Models will not be saved if
            this parameter is not passed.
        cuda_device , optional (default = -1)
            An integer specifying the CUDA device to use. If -1, the CPU is used.
            Multi-gpu training is not currently supported, but will be once the
            Pytorch DataParallel API stabilises.
        grad_norm : float, optional, (default = None).
            If provided, gradient norms will be rescaled to have a maximum of this value.
        grad_clipping : ``float``, optional (default = ``None``).
            If provided, gradients will be clipped `during the backward pass` to have an (absolute)
            maximum of this value.  If you are getting ``NaNs`` in your gradients during training
            that are not solved by using ``grad_norm``, you may need this.
        keep_all_checkpoints : If set, keep checkpoints from every validation. Otherwise, keep only
            best and (if different) most recent.
        val_data_limit: During training, use only the first N examples from the validation set.
            Set to -1 to use all.
        training_data_fraction: If set to a float between 0 and 1, load only the specified
            percentage of examples. Hashing is used to ensure that the same examples are loaded
            each epoch.
        """
        self._model = model
        print(model)
        self._load_model = load_model
        if self._load_model:
            self._model.load_state_dict(torch.load(self._load_model))

        self._patience = patience
        self._serialization_dir = serialization_dir
        self._device = device
        self._clip_grad_norm_val = clip_grad_norm_val
        self._lr_decay = lr_decay
        self._min_lr = min_lr
        self._lr_patience = lr_patience
        self._keep_all_checkpoints = keep_all_checkpoints
        self._max_epochs = max_epochs
        self._training_data_fraction = training_data_fraction
        self._initial_lr = initial_lr
        self._lr_scheduling_metric = lr_scheduling_metric
        self._metric_decreases = metric_decreases
        self._val_data_limit = val_data_limit
        self._kl_reach_point = kl_reach_point
        self._warmup_steps = warmup_steps

        self._metric_infos = {}
        # TO BE REMOVED
        self._test_metric_infos = {}
        self._patience_exceeded = False

        no_decay = ["bias", "LayerNorm.weight"]
        if ("transformers" in str(type(self._model.encoder))) or (
            "dataparallel" in str(type(self._model.encoder)).lower()
        ):
            self._trainable_params = filter(
                lambda p: p.requires_grad, self._model.parameters()
            )

            self._optimizer = optim.AdamW(
                [
                    {
                        "params": list(
                            chain(
                                *[
                                    list(
                                        (
                                            filter(
                                                lambda p: p.requires_grad,
                                                module.parameters(),
                                            )
                                        )
                                    )
                                    for module in self._model.children()
                                    if (
                                        ("transformers" in str(type(module)).lower())
                                        or ("dataparallel" in str(type(module)).lower())
                                    )
                                ]
                            )
                        ),
                        "lr": 5e-5,
                        "weight_decay": 0.0,
                    },
                    {
                        "params": list(
                            chain(
                                *[
                                    list(
                                        (
                                            filter(
                                                lambda p: p.requires_grad,
                                                module.parameters(),
                                            )
                                        )
                                    )
                                    for module in self._model.children()
                                    if (
                                        ("transformers" not in str(type(module)))
                                        and (
                                            "dataparallel"
                                            not in str(type(module)).lower()
                                        )
                                    )
                                ]
                            )
                        ),
                        "weight_decay": 0.0,
                    },
                ],
                lr=self._initial_lr,
                eps=1e-8,
            )
        else:
            self._trainable_params = filter(
                lambda p: p.requires_grad, self._model.parameters()
            )
            self._optimizer = optim.Adam(self._trainable_params, lr=self._initial_lr)
        self._epoch_steps = 0
        self._train_counter = 0
        self._validation_counter = 0
        self._validation_steps = 0
        self._initial_lr = [
            param_group["lr"] for param_group in self._optimizer.param_groups
        ]
        self._load_optimizer = load_optimizer
        self._optimizer_loaded = False
        if self._load_optimizer:
            self._optimizer.load_state_dict(torch.load(load_optimizer))
            self._optimizer_loaded = True

        self._bad_epochs = 0
        if self._kl_reach_point:
            self._kl_anneal_function = lambda x: 1 / (
                1 + np.exp(-((x / (self._kl_reach_point / 10)) - 5))
            )
            if self._optimizer_loaded:
                self._train_counter = self._kl_reach_point

        if beam_size == 1:
            log.warining(
                "WARNING: Beam size is 1, note that this is equivalent to greedy search"
            )
        self._beam_size = beam_size
        self._n_best = n_best
        self._min_length = min_length
        self._max_length = max_length
        self._ratio = ratio

        self._datamaker = datamaker

        self._tgt_pad_idx = self._datamaker.vocab.definition.stoi["<pad>"]
        self._tgt_bos_idx = self._datamaker.vocab.definition.stoi["<sos>"]
        self._tgt_eos_idx = self._datamaker.vocab.definition.stoi["<eos>"]
        self._tgt_unk_idx = self._datamaker.vocab.definition.stoi["<unk>"]
        self._exclusion_idxs = {self._tgt_unk_idx, self._tgt_pad_idx, self._tgt_bos_idx}

        self._validation_interval = validation_interval
        self._TB_dir = None
        if self._serialization_dir is not None:
            self._TB_dir = mkdir(os.path.join(self._serialization_dir, "tensorboard"))
            self._TB_train_log = SummaryWriter(
                mkdir(os.path.join(self._TB_dir, "train"))
            )
            self._TB_validation_log = SummaryWriter(
                mkdir(os.path.join(self._TB_dir, "val"))
            )

            self._validation_log_dir = mkdir(
                os.path.join(self._serialization_dir, "valid")
            )
            mkdir(os.path.join(self._serialization_dir, "model"))
            mkdir(os.path.join(self._serialization_dir, "optimizer"))
            self._train_log_dir = mkdir(os.path.join(self._serialization_dir, "train"))
            with open(self._serialization_dir + "/optimizer_printout", "w") as f:
                f.write(repr(self._optimizer))

    def _check_metric_history(
        self, metric_history, current_score, should_decrease=False
    ):
        """
        Given a the history of the performance on a metric
        and the current score, check if current score is
        best so far and if out of patience.
        """
        assert current_score in metric_history

        patience = self._patience + 1
        best_fn = min if should_decrease else max
        best_score = best_fn(metric_history)
        if best_score == current_score:
            best_so_far = metric_history.index(best_score) == len(metric_history) - 1
        else:
            best_so_far = False

        if should_decrease:
            index_of_last_improvement = metric_history.index(min(metric_history))
            out_of_patience = index_of_last_improvement <= len(metric_history) - (
                patience + 1
            )
        else:
            index_of_last_improvement = metric_history.index(max(metric_history))
            out_of_patience = index_of_last_improvement <= len(metric_history) - (
                patience + 1
            )

        return best_so_far, out_of_patience

    def _update_metric_history(
        self, val_pass, metric, current_value, metric_infos, metric_decreases,
    ):
        """
        This function updates metric history with the best validation score so far.
        Parameters
        ---------
        val_pass: int.
        all_val_metrics: dict with performance on current validation pass.
        metric: str, name of metric
        task_name: str, name of task
        metric_infos: dict storing information about the various metrics
        metric_decreases: bool, marker to show if we should increase or
        decrease validation metric.
        should_save: bool, for checkpointing
        new_best: bool, indicator of whether the previous best preformance score was exceeded
        Returns
        ________
        metric_infos: dict storing information about the various metrics
        this_val_metric: dict, metric information for this validation pass, used for optimization
            scheduler
        should_save: bool
        new_best: bool
        """

        metric_exists = metric_infos.get(metric)
        if metric_exists is None:
            metric_infos[metric] = {}
        metric_history = metric_infos[metric].get("hist")
        if metric_history is None:
            metric_infos[metric]["hist"] = []
            metric_history = metric_infos[metric]["hist"]
        metric_history.append(current_value)
        is_best_so_far, out_of_patience = self._check_metric_history(
            metric_history, current_value, metric_decreases
        )
        if is_best_so_far:
            logging.info("Best result seen so far for %s.", metric)
            metric_infos[metric]["best"] = (val_pass, current_value)
            should_save = True
        if out_of_patience:
            metric_infos[metric]["stopped"] = True
        else:
            metric_infos[metric]["stopped"] = False
        return is_best_so_far, out_of_patience

    # @profile
    def _train(self, batch_size):

        assert isinstance(self._model, torch.nn.Module), (
            "Before calling train, you must supply a PyTorch model using the"
            " `Trainer._set_model` method"
        )
        self._epoch_steps += 1
        if self._epoch_steps > self._max_epochs:
            logging.info(
                f"Max Epoch Steps {self._max_epochs} reached. Training Stopped."
            )
            return
        if self._patience_exceeded:
            logging.info(
                f"Patience has already been exceeded for every metric. In other words,"
                f" I've become IMPATIENT. Training Stopped."
            )
            return

        train_iterator = self._datamaker.get_iterator(
            "train", batch_size, device=self._device
        )

        # for testing
        # _test_train_iterator = islice(train_iterator, 0, 1)

        validate_interval = None
        if self._validation_interval is not None:
            if len(train_iterator) > self._validation_interval:
                validation_iters_per_epoch = round(
                    len(train_iterator) / self._validation_interval
                )
                validate_interval = len(train_iterator) // validation_iters_per_epoch
                max_val_interval = (
                    self._epoch_steps * len(train_iterator)
                ) + validate_interval * (validation_iters_per_epoch - 1)

        generations = []
        targets = []
        sources = []
        words = []
        log = {
            "bleu": [],
            "perplexity": [],
            "kld": [],
            "bert-score-p": [],
            "bert-score-r": [],
            "bert-score-f1": [],
        }
        for i, batch in enumerate(
            tqdm.tqdm(train_iterator, desc=f"Training (Epoch {self._epoch_steps}): ")
        ):
            try:
                self._train_counter += 1
                self._model.zero_grad()
                self._model.train()

                example, example_lens = batch.example
                definition, definition_lens = batch.definition
                word, word_lens = batch.word
                if self._model.variational or self._model.defbert:
                    definition_ae, definition_ae_lens = batch.definition_ae
                else:
                    definition_ae, definition_ae_lens = None, None

                sentence_mask = bert_dual_sequence_mask(
                    # example, self._datamaker.vocab.example.encode("</s>")[1:-1], device=self._device
                    # example, self._datamaker.vocab.example.encode(self._datamaker.vocab.example._sep_token)[1:-1], device=self._device
                    example, self._datamaker.vocab.example.encode("[SEP]")[1:-1], device=self._device
                    # example, self._tgt_bos_idx, device=self._device
                )
                current_batch_size = word.shape[0]

                model_out = self._forward(
                    "train",
                    input=example,
                    seq_lens=example_lens,
                    span_token_ids=word,
                    target=definition,
                    target_lens=definition_lens,
                    definition=definition_ae,
                    definition_lens=definition_ae_lens,
                    sentence_mask=sentence_mask,
                )
                if torch.isnan(model_out.perplexity):
                    print(
                        "Loss is NaN. If this happens to often you must debug. YOU MUST"
                    )
                    continue
                torch.cuda.empty_cache()

                torch.nn.utils.clip_grad_norm_(
                    self._trainable_params, self._clip_grad_norm_val
                )

                generations.extend(
                    self._datamaker.decode(
                        model_out.predictions, "definition", batch=True
                    )
                )
                targets.extend(
                    self._datamaker.decode(definition, "definition", batch=True)
                )
                sources.extend(self._datamaker.decode(example, "example", batch=True))
                words.extend(self._datamaker.decode(word, "word", batch=True))

                self._TB_train_log.add_scalar(
                    "loss", model_out.loss.mean().item(), self._train_counter
                )
                current_bleu = batch_bleu(
                    targets[-current_batch_size:],
                    generations[-current_batch_size:],
                    reduction="average",
                )
                try:
                    P, R, F1 = bert_score(
                        generations[-current_batch_size:],
                        targets[-current_batch_size:],
                    )
                except:
                    P, R, F1 = (torch.Tensor([0]), torch.Tensor([0]), torch.Tensor([0]))
                log["bert-score-p"].append(P.mean().item())
                log["bert-score-r"].append(R.mean().item())
                log["bert-score-f1"].append(F1.mean().item())

                self._TB_train_log.add_scalar(
                    "batch_BLEU", current_bleu, self._train_counter
                )

                log["bleu"].append(current_bleu)
                log["perplexity"].append(model_out.perplexity.item())

                loss = model_out.loss
                if model_out.kl is not None:
                    self._TB_train_log.add_scalar(
                        "KL_Divergence", model_out.kl.mean().item(), self._train_counter
                    )
                    self._TB_train_log.add_scalar(
                        "KL_Weight",
                        self._kl_anneal_function(self._train_counter),
                        self._train_counter,
                    )
                    loss = (
                        loss
                        + self._kl_anneal_function(self._train_counter)
                        * model_out.fake_kl
                    ).mean()
                    log["kld"].append(model_out.kl.mean().item())

                loss.backward()
                self._optimizer.step()
                del loss

                for i, param_group in enumerate(self._optimizer.param_groups):
                    self._TB_train_log.add_scalar(
                        f"Learning_rate_{i}", param_group["lr"], self._train_counter,
                    )
                if self._warmup_steps:
                    if not self._optimizer_loaded:
                        if self._train_counter <= self._warmup_steps:
                            learning_rate = [
                                lr * (self._train_counter / self._warmup_steps)
                                for lr in self._initial_lr
                            ]
                            for i, param_group in enumerate(
                                self._optimizer.param_groups
                            ):
                                param_group["lr"] = learning_rate[i]

            except RuntimeError as e:
                # catch out of memory exceptions during fwd/bck (skip batch)
                if "out of memory" in str(e):
                    logging.warning(
                        "| WARNING: ran out of memory, skipping batch. "
                        "if this happens frequently, decrease batch_size or "
                        "truncate the inputs to the model."
                    )
                    torch.cuda.empty_cache()

                    if "loss" in locals():
                        del loss

                    continue
                else:
                    raise e
            if self._train_counter % 200 == 0:
                write_str = (
                    f"| epoch {self._epoch_steps} |"
                    f" {self._train_counter%len(train_iterator)}/{len(train_iterator)}"
                    f" | ppl {sum(log['perplexity'])/len(log['perplexity']):.2f} | bleu"
                    f" {100*sum(log['bleu'])/len(log['bleu']):.2f} | bert-score,"
                    f" p:{sum(log['bert-score-p'])/len(log['bert-score-p']):.2f}"
                    f" r:{sum(log['bert-score-r'])/len(log['bert-score-r']):.2f}"
                    f" f1:{sum(log['bert-score-f1'])/len(log['bert-score-f1']):.2f} |"
                )
                if self._model.variational:
                    write_str += f" KLD {sum(log['kld'])/len(log['kld']):.2f} |"
                for i, param_group in enumerate(self._optimizer.param_groups):
                    write_str += f" lr #{i} {param_group['lr']} |"
                tqdm.tqdm.write(write_str)
                for key in log:
                    log[key] = []
            if validate_interval is not None:
                if validate_interval != 0:
                    if (
                        self._train_counter % validate_interval == 0
                        and self._train_counter <= max_val_interval
                    ):
                        self._validate(64)

        bleu = batch_bleu(targets, generations, reduction="average")
        self._TB_train_log.add_scalar("BLEU", bleu, self._epoch_steps)

        with open(
            os.path.join(self._train_log_dir, f"iter_{self._epoch_steps}.json"), "w",
        ) as f:
            f.write(
                "\n".join(
                    [
                        json.dumps(
                            {
                                "src": sources[i],
                                "tgt": targets[i],
                                "gen": generations[i],
                                "word": words[i],
                            }
                        )
                        for i in range(len(generations))
                    ]
                )
            )
        return DotMap({"src": sources, "tgt": targets, "gen": generations})

    def _validate(self, batch_size):

        assert isinstance(self._model, torch.nn.Module), (
            "Before calling _validate, you must supply a PyTorch model using the"
            " `Trainer._set_model` method"
        )
        valid_iterator = self._datamaker.get_iterator(
            "valid", batch_size, device=self._device
        )

        # for testing
        # _test_valid_iterator = islice(valid_iterator, 1)

        generations = []
        targets = []
        sources = []
        words = []

        self._validation_steps += 1
        ppl = 0
        kld = 0
        for i, batch in enumerate(
            tqdm.tqdm(
                valid_iterator, desc=f"Validating (Epoch {self._validation_steps}): "
            )
        ):
            try:
                self._validation_counter += 1
                self._model.zero_grad()
                self._model.eval()

                example, example_lens = batch.example
                definition, definition_lens = batch.definition
                word, word_lens = batch.word
                if self._model.variational:
                    definition_ae, definition_ae_lens = batch.definition_ae
                else:
                    definition_ae, definition_ae_lens = None, None

                sentence_mask = bert_dual_sequence_mask(
                    # example, self._datamaker.vocab.example.encode("</s>")[1:-1], device=self._device
                    # example, self._datamaker.vocab.example.encode(self._datamaker.vocab.example._sep_token)[1:-1], device=self._device
                    # example, self._tgt_bos_idx, device=self._device
                    example, self._datamaker.vocab.example.encode("[SEP]")[1:-1], device=self._device
                )
                current_batch_size = word.shape[0]

                decode_strategy = BeamSearch(
                    self._beam_size,
                    current_batch_size,
                    pad=self._tgt_pad_idx,
                    bos=self._tgt_bos_idx,
                    eos=self._tgt_eos_idx,
                    n_best=1 if self._n_best is None else self._n_best,
                    global_scorer=self._model.global_scorer,
                    min_length=self._min_length,
                    max_length=self._max_length,
                    return_attention=False,
                    block_ngram_repeat=3,
                    exclusion_tokens=self._exclusion_idxs,
                    stepwise_penalty=None,
                    ratio=self._ratio if self._ratio is not None else 0,
                )
                with torch.no_grad():
                    model_out = self._forward(
                        "valid",
                        input=example,
                        seq_lens=example_lens,
                        span_token_ids=word,
                        target=definition,
                        target_lens=definition_lens,
                        decode_strategy=decode_strategy,
                        definition=definition_ae,
                        definition_lens=definition_ae_lens,
                        sentence_mask=sentence_mask,
                        device=self._device,
                    )
                torch.cuda.empty_cache()

                generations.extend(
                    [
                        self._datamaker.decode(gen[0], "definition", batch=False)
                        for gen in model_out.predictions
                    ]
                )
                targets.extend(
                    self._datamaker.decode(definition, "definition", batch=True)
                )
                sources.extend(self._datamaker.decode(example, "example", batch=True))
                words.extend(self._datamaker.decode(word, "word", batch=True))

                if torch.isnan(model_out.perplexity):
                    tqdm.tqdm.write(
                        "NaN Fouuuuuuuuuund!!!!!!!!!!!!!!! If this happens too often,"
                        " check WTF is going on"
                    )
                    continue
                ppl += model_out.perplexity.item()

                self._TB_validation_log.add_scalar(
                    "batch_perplexity",
                    model_out.perplexity.item(),
                    self._validation_counter,
                )

                current_bleu = batch_bleu(
                    targets[-current_batch_size:],
                    generations[-current_batch_size:],
                    reduction="average",
                )
                self._TB_validation_log.add_scalar(
                    "batch_BLEU", current_bleu, self._validation_counter
                )

                if model_out.kl is not None:
                    kld += model_out.kl.item()
                    self._TB_validation_log.add_scalar(
                        "kl", model_out.kl.item(), self._validation_counter
                    )
                if self._val_data_limit:
                    if i * batch_size > self._val_data_limit:
                        break
            except RuntimeError as e:
                # catch out of memory exceptions during fwd/bck (skip batch)
                if "out of memory" in str(e):
                    logging.warning(
                        "| WARNING: ran out of memory, skipping batch. "
                        "if this happens frequently, decrease batch_size or "
                        "truncate the inputs to the model."
                    )
                    continue
                else:
                    raise e

        torch.cuda.empty_cache()

        bleu = batch_bleu(targets, generations, reduction="average")
        self._TB_validation_log.add_scalar("BLEU", bleu, self._validation_steps)
        try:
            P, R, F1 = bert_score(generations, targets)
        except:
            P, R, F1 = (torch.Tensor([0]), torch.Tensor([0]), torch.Tensor([0]))

        self._TB_validation_log.add_scalar(
            "bert-score-p", P.mean().item(), self._validation_counter
        )
        self._TB_validation_log.add_scalar(
            "bert-score-r", R.mean().item(), self._validation_counter
        )
        self._TB_validation_log.add_scalar(
            "bert-score-f1", F1.mean().item(), self._validation_counter
        )
        ppl = ppl / len(valid_iterator)
        # kld = kld / len(valid_iterator)
        self._TB_validation_log.add_scalar("kl", kld, self._validation_counter)

        # Had to do this for memory issues

        self._TB_validation_log.add_scalar("Perplexity", ppl, self._validation_steps)

        metric_dict = {"bleu": bleu, "perplexity": ppl, "kl": kld}

        bleu_best, bleu_patience = self._update_metric_history(
            self._validation_steps,
            "bleu",
            bleu,
            self._metric_infos,
            metric_decreases=False,
        )

        ppl_best, ppl_patience = self._update_metric_history(
            self._validation_steps,
            "perplexity",
            ppl,
            self._metric_infos,
            metric_decreases=True,
        )

        bert_score_p_best, bert_score_p_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_p",
            P.mean().item(),
            self._metric_infos,
            metric_decreases=False,
        )

        bert_score_r_best, bert_score_r_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_r",
            R.mean().item(),
            self._metric_infos,
            metric_decreases=False,
        )
        bert_score_f1_best, bert_score_f1_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_f1",
            F1.mean().item(),
            self._metric_infos,
            metric_decreases=False,
        )
        # kld_best, kld_patience = self._update_metric_history(
        #    self._epoch_steps,
        #    "KL Divergence",
        #    kld,
        #    self._metric_infos,
        #    metric_decreases=True,
        # )

        if self._keep_all_checkpoints:
            torch.save(
                self._model.state_dict(),
                os.path.join(
                    self._serialization_dir,
                    "model",
                    f"iter_{self._validation_steps}.pth",
                ),
            )
            torch.save(
                self._optimizer.state_dict(),
                os.path.join(
                    self._serialization_dir,
                    "optimizer",
                    f"iter_{self._validation_steps}.pth",
                ),
            )
        if bleu_best:
            torch.save(
                self._model.state_dict(),
                os.path.join(self._serialization_dir, "model", f"bleu_best.pth",),
            )
            torch.save(
                self._optimizer.state_dict(),
                os.path.join(self._serialization_dir, "optimizer", f"bleu_best.pth",),
            )
            self._bad_epochs = 0

        self._write_metric_info()

        if ppl_best:
            torch.save(
                self._model.state_dict(),
                os.path.join(self._serialization_dir, "model", f"ppl_best.pth",),
            )
            torch.save(
                self._optimizer.state_dict(),
                os.path.join(self._serialization_dir, "optimizer", f"ppl_best.pth",),
            )
            self._bad_epochs = 0
        if bert_score_f1_best:
            torch.save(
                self._model.state_dict(),
                os.path.join(self._serialization_dir, "model", f"bert_score_best.pth",),
            )
            torch.save(
                self._optimizer.state_dict(),
                os.path.join(
                    self._serialization_dir, "optimizer", f"bert_score_best.pth",
                ),
            )
            self._bad_epochs = 0
        if not bleu_best and not ppl_best and not bert_score_f1_best:
            self._bad_epochs += 1
        if bleu_patience and ppl_patience and bert_score_f1_patience:
            logging.info(
                "Ran out of patience for both BLEU, BERTScore and perplexity. Stopping"
                " training"
            )

            self._patience_exceeded = True
        with open(
            os.path.join(self._validation_log_dir, f"iter_{self._epoch_steps}.json"),
            "w",
        ) as f:
            f.write(
                "\n".join(
                    [
                        json.dumps(
                            {
                                "src": sources[i],
                                "tgt": targets[i],
                                "gen": generations[i],
                                "word": words[i],
                            }
                        )
                        for i in range(len(generations))
                    ]
                )
            )

        if self._bad_epochs == 3:
            tqdm.tqdm.write(
                "4 Bad Epochs in a row, reverting optimizer and model to the last"
                " bert_score_best.pth"
            )
            self._model.load_state_dict(
                torch.load(
                    os.path.join(
                        self._serialization_dir, "model", f"bert_score_best.pth"
                    )
                ),
            )
            self._optimizer.load_state_dict(
                torch.load(
                    os.path.join(
                        self._serialization_dir, "optimizer", f"bert_score_best.pth"
                    )
                )
            )
            for param_group in self._optimizer.param_groups:
                param_group["lr"] *= 0.5

        return DotMap({"src": sources, "tgt": targets, "gen": generations})

    def _test(self, batch_size):

        self.load_model()
        assert isinstance(self._model, torch.nn.Module), (
            "Before calling _validate, you must supply a PyTorch model using the"
            " `Trainer._set_model` method"
        )
        test_iterator = self._datamaker.get_iterator(
            "test", batch_size, device=self._device
        )

        generations = []
        targets = []
        sources = []
        words = []

        ppl = 0
        kld = 0
        for i, batch in enumerate(
            tqdm.tqdm(test_iterator, desc=f"Testing (Epoch {self._validation_steps}): ")
        ):
            try:
                self._model.zero_grad()
                self._model.eval()

                example, example_lens = batch.example
                definition, definition_lens = batch.definition
                word, word_lens = batch.word
                if self._model.variational or self._model.defbert:
                    definition_ae, definition_ae_lens = batch.definition_ae
                else:
                    definition_ae, definition_ae_lens = None, None

                sentence_mask = bert_dual_sequence_mask(
                    # example, self._datamaker.vocab.example.encode("</s>")[1:-1], device=self._device
                    # example, self._datamaker.vocab.example.encode(self._datamaker.vocab.example._sep_token)[1:-1], device=self._device
                    # example, self._tgt_bos_idx, device=self._device
                    example, self._datamaker.vocab.example.encode("[SEP]")[1:-1], device=self._device
                )
                current_batch_size = word.shape[0]

                decode_strategy = BeamSearch(
                    self._beam_size,
                    current_batch_size,
                    pad=self._tgt_pad_idx,
                    bos=self._tgt_bos_idx,
                    eos=self._tgt_eos_idx,
                    n_best=1 if self._n_best is None else self._n_best,
                    global_scorer=self._model.global_scorer,
                    min_length=self._min_length,
                    max_length=self._max_length,
                    return_attention=False,
                    block_ngram_repeat=3,
                    exclusion_tokens=self._exclusion_idxs,
                    stepwise_penalty=None,
                    ratio=self._ratio if self._ratio is not None else 0,
                )
                with torch.no_grad():
                    model_out = self._forward(
                        "test",
                        input=example,
                        seq_lens=example_lens,
                        span_token_ids=word,
                        target=definition,
                        target_lens=definition_lens,
                        decode_strategy=decode_strategy,
                        definition=definition_ae,
                        definition_lens=definition_ae_lens,
                        sentence_mask=sentence_mask,
                    )
                torch.cuda.empty_cache()

                generations.extend(
                    [
                        self._datamaker.decode(gen[0], "definition", batch=False)
                        for gen in model_out.predictions
                    ]
                )
                targets.extend(
                    self._datamaker.decode(definition, "definition", batch=True)
                )
                sources.extend(self._datamaker.decode(example, "example", batch=True))
                words.extend(self._datamaker.decode(word, "word", batch=True))

                ppl += model_out.perplexity.item()
                if model_out.kl is not None:
                    kld += model_out.kl.item()
                    self._TB_validation_log.add_scalar(
                        "kl", model_out.kl.item(), self._validation_counter
                    )

                current_bleu = batch_bleu(
                    targets[-current_batch_size:],
                    generations[-current_batch_size:],
                    reduction="average",
                )
            except RuntimeError as e:
                # catch out of memory exceptions during fwd/bck (skip batch)
                if "out of memory" in str(e):
                    logging.warning(
                        "| WARNING: ran out of memory, skipping batch. "
                        "if this happens frequently, decrease batch_size or "
                        "truncate the inputs to the model."
                    )
                    torch.cuda.empty_cache()
                    continue
                else:
                    raise e

        torch.cuda.empty_cache()

        bleu = batch_bleu(targets, generations, reduction="average")

        ppl = ppl / len(test_iterator)
        if self._model.variational:
            kld = kld / len(test_iterator)
            kld_best, kld_patience = self._update_metric_history(
                self._validation_steps,
                "KL Divergence",
                kld,
                self._test_metric_infos,
                metric_decreases=False,
            )

        metric_dict = {"bleu": bleu, "perplexity": ppl, "kl": kld}

        try:
            P, R, F1 = bert_score(generations, targets)
        except:
            P, R, F1 = (torch.Tensor([0]), torch.Tensor([0]), torch.Tensor([0]))

        bleu_best, bleu_patience = self._update_metric_history(
            self._validation_steps,
            "bleu",
            bleu,
            self._test_metric_infos,
            metric_decreases=False,
        )

        ppl_best, ppl_patience = self._update_metric_history(
            self._validation_steps,
            "perplexity",
            ppl,
            self._test_metric_infos,
            metric_decreases=True,
        )

        bert_score_p_best, bert_score_p_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_p",
            P.mean().item(),
            self._test_metric_infos,
            metric_decreases=False,
        )

        bert_score_r_best, bert_score_r_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_r",
            R.mean().item(),
            self._test_metric_infos,
            metric_decreases=False,
        )
        bert_score_f1_best, bert_score_f1_patience = self._update_metric_history(
            self._validation_steps,
            "bert_score_f1",
            F1.mean().item(),
            self._test_metric_infos,
            metric_decreases=False,
        )
        # kld_best, kld_patience = self._update_metric_history(
        #     self._epoch_steps,
        #     "KL Divergence",
        #     kld,
        #     self._test_metric_infos,
        #     metric_decreases=False,
        # )

        self._test_write_metric_info()

        with open(
            os.path.join(
                self._validation_log_dir, f"test_iter_{self._epoch_steps}.json"
            ),
            "w",
        ) as f:
            f.write(
                "\n".join(
                    [
                        json.dumps(
                            {
                                "src": sources[i],
                                "tgt": targets[i],
                                "gen": generations[i],
                                "word": words[i],
                            }
                        )
                        for i in range(len(generations))
                    ]
                )
            )

        return DotMap({"src": sources, "tgt": targets, "gen": generations})

    def _reset_steps(self):
        self._epoch_steps = 0
        self._validation_counter = 0
        self._train_counter = 0

    def _forward(self, phase: str = "train",  **batch):

        if phase not in ["train", "valid", "test"]:
            raise NotImplementedError(f"{phase} must be in ['train','test','valid']")
        if phase == "train":
            return self._model(device=self._device, **batch)
        elif phase in ["valid", "test"]:
            return self._model._validate(**batch)
        else:
            raise NotImplementedError

    def _set_model(self, model):
        self._model = model
        if self._load_model:
            self._model.load_state_dict(torch.load(self._load_model))

    def load_model(self):
        if self._load_model:
            self._model.load_state_dict(torch.load(self._load_model))
        else:
            self._model.load_state_dict(
                torch.load(self._serialization_dir + "/model/bert_score_best.pth")
            )

    def _write_metric_info(self):
        with open(os.path.join(self._serialization_dir, "metric.json"), "w") as fp:
            json.dump(self._metric_infos, fp)
        # TO BE REMOVED

    def _test_write_metric_info(self):
        with open(os.path.join(self._serialization_dir, "test_metric.json"), "w") as fp:
            json.dump(self._test_metric_infos, fp)

max_stars_repo_name: minaph/vcdm

max_issues_repo_name: minaph/vcdm

max_forks_repo_name: minaph/vcdm


text:
import numpy as np

x = np.random.rand(100)
y = np.random.rand(100)

sort_inds = np.argsort(x)
x = x[sort_inds]
y = y[sort_inds]

ext: py

reason: import numpy


text:
from sklearn.svm import SVC
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)

from sklearn.cross_validation import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.model_selection import cross_val_score, KFold
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import scale
from sklearn.preprocessing import normalize
import sys
import numpy as np

import music_genre_fft as mg_fft
import music_genre_mfcc as mg_mfcc
import plots

genres_dir = '../genres'

def run_classifier(data, target):
   data_train, data_test, target_train, target_test = train_test_split(data, target, test_size=0.25, random_state=42)
   #pline = Pipeline([('clf', SVC(C=1.0, kernel='rbf', gamma=0.01))])
   svc = SVC(probability=True)
   print "Shape of training set: %s" % (data_train.shape,)
   print "Shape of test set: %s" % (data_test.shape,)

   #params = {
   #	   'kernel': ('poly', 'rbf', 'sigmoid', 'precomputed'),
   #	   'gamma': (0.01, 0.03, 0.1, 0.3, 1, 3, 5),
   #	   'C': (0.1, 0.3, 1, 3, 10, 30, 50, 100),
   #}
   params = [
	   {'C': [0.1, 0.3, 1, 3, 10, 30], 'gamma': [0.01, 0.03, 0.1, 0.3, 1, 3, 5], 'degree': [2,3,4],
           'kernel': ['rbf', 'poly', 'sigmoid']},
   	  # {'C': (0.1, 0.3, 1, 3, 10, 30, 50, 100), 'gamma': (0.01, 0.03, 0.1, 0.3, 1, 3, 5), 'kernel': ['rbf']},
   ]

   gsearch = GridSearchCV(svc, params, n_jobs=2,
		   verbose=1, scoring='f1_micro', cv=5)

   gsearch.fit(data_train, target_train)
   print 'Best score: %0.3f' % gsearch.best_score_

   print 'Best parameters set:'
   best_params = gsearch.best_estimator_.get_params()

   #for param_name in sorted(params.keys()):
   #	   print '\t%s: %r' % (param_name, best_params[param_name])

   for dict_item in params:
	for param_name in sorted(dict_item.keys()):
		print '\t%s: %r' % (param_name, best_params[param_name])

   preds = gsearch.predict(data_test)
   print classification_report(target_test, preds, target_names=mg_fft.GENRES)

   conf_matr = confusion_matrix(target_test, preds)
   conf_matr = normalize(conf_matr)
   plots.plot_confusion_matrix(conf_matr, mg_fft.GENRES, "Confusion Matrix", "Music Genres")

   #gather data for ROC curves
   fprs = []
   tprs = []
   AUCs = []
   for label in range(len(mg_fft.GENRES)):
	   target_label_test = np.asarray(target_test==label, dtype=int)
	   proba = gsearch.predict_proba(data_test)
	   proba_label = proba[:, label]
   	   fpr, tpr, roc_thresholds = roc_curve(target_label_test, proba_label)
	   fprs.append(fpr)
	   tprs.append(tpr)
	   AUCs.append(roc_auc_score(target_label_test, proba_label))

   plots.plot_roc_curves(fprs, tprs, AUCs, mg_fft.GENRES)

if __name__ == '__main__':
   if (len(sys.argv)) < 2:
      print "Usage: \"python svm_classification.py -fft\" or \"python svm_classification.py -mfcc\""
      sys.exit(1)
   X = []
   y = []
   if (sys.argv[1] == '-fft'):
      X, y = mg_fft.read_fft(mg_fft.GENRES, genres_dir)  #read data
      #X = normalize(X)
      X = scale(X, 0, False)
      run_classifier(X, y)
   elif (sys.argv[1] == '-mfcc'):
      X, y = mg_mfcc.read_ceps(mg_fft.GENRES, genres_dir)  #read data
      X = np.nan_to_num(X)
      #X = normalize(X)
      X = scale(X, 0, False)
      run_classifier(X, y)
   else:
      print "Illegal third argument",

max_stars_repo_name: Cyofanni/music-genre-classification

max_issues_repo_name: Cyofanni/music-genre-classification

max_forks_repo_name: Cyofanni/music-genre-classification


text:
"""
This experiment tests the best size of the training data
"""
import os
import logging
import pickle

import numpy as np

import al
from al.train.active_train import ActiveTrain
from al.helpers.experiment import set_up_experiment, load_config
from al.experiments import set_up_learner

EXPERIMENT_NAME = 'initial_training_data'
FOLDER_PATH = os.path.expanduser(f'~/Documents/active-learning/experiments/{EXPERIMENT_NAME}')
DATASETS = ['mnist', 'cifar', 'pascalvoc_detection']
dataset_to_initsizes = {
    'mnist': [10, 30, 100, 300, 1000],
    'cifar': [100, 300, 1000, 3000, 10000],
    'pascalvoc_detection': [100, 300, 500, 1000]
}
REPEATS = 2

OUTPUT_DIR, FIGURE_DIR, logger, logger_name = set_up_experiment(EXPERIMENT_NAME)

def run_single_experiment(dataset_name, init_size):
    logger.info(f'INITIAL SIZE : {init_size}')
    config = load_config(FOLDER_PATH, dataset_name)
    setupper = set_up_learner(dataset_name)
    config['active_learning']['output_dir'] = OUTPUT_DIR
    config['active_learning']['init_size'] = init_size
    config['experiment']['logger_name'] = logger_name
    logger.debug('Getting dataset and learner')
    dataset, learner = setupper(config, OUTPUT_DIR, logger)
    logger.debug('Getting trainer')
    trainer = ActiveTrain(learner, dataset, config['experiment']['strategy'], logger_name)
    logger.debug('Training...')
    scores = None
    # scores = trainer.train(config['train_parameters'], **config['active_learning'])
    logger.debug('Done training...')
    logger.info('-------------------------')
    return scores

def run_on_dataset(dataset):
    logger.info('-------------------------')
    logger.info('--LAUNCHING EXPERIMENT--')
    logger.info(f'--DATASET {dataset}--')
    logger.info('-------------------------')
    data_scores = []
    for init_size in dataset_to_initsizes[dataset]:
        for repeat in range(REPEATS):
            logger.info('-------------------------')
            logger.info(f'--REPEAT #{repeat+1}/{REPEATS}--')
            logger.info('-------------------------')
            scores = run_single_experiment(dataset_name, init_size)
            data_scores.append({
                'repeat': repeat,
                'score': scores,
                'init_size': init_size
            })
    return data_scores

logger.info('-------------------------')
logger.info('--LAUNCHING EXPERIMENTS--')
logger.info('-------------------------')

for dataset_name in DATASETS:
    score_data = run_on_dataset(dataset_name)
    with open(f'{OUTPUT_DIR}/scores_{dataset_name}.pickle', 'wb') as f:
        pickle.dump(score_data, f)

max_stars_repo_name: kili-technology/active-learning

max_issues_repo_name: kili-technology/active-learning

max_forks_repo_name: kili-technology/active-learning


text:
from seamless.highlevel import Context

ctx = Context()
def plot(period, npoints):
    import matplotlib.pyplot as plt
    import numpy as np
    points = np.arange(npoints)
    phase = points/period*np.pi*2
    fig, ax = plt.subplots()
    ax.plot(np.sin(phase))
    from io import BytesIO
    png = BytesIO()
    plt.savefig(png)
    return png.getvalue()

ctx.plot = plot
ctx.plot.period = 100
ctx.plot.npoints = 1000
ctx.png = ctx.plot
ctx.png.celltype = "bytes"
ctx.png.mimetype = "png"
ctx.png.share("plot.png")
ctx.compute()
print("open http://localhost:5813/ctx/plot.png in the browser")


text:
#
#  NAME
#    problem_set4.py
#
#  DESCRIPTION
#    In Problem Set 4, you will classify EEG data into NREM sleep stages and
#    create spectrograms and hypnograms.
#
from __future__ import division
import numpy as np
import matplotlib.pylab as plt
import matplotlib.mlab as m
from sklearn.linear_model import LinearRegression, LogisticRegression

def load_examples(filename):
    """
    load_examples takes the file name and reads in the data.  It returns an
    array containing the 4 examples of the 4 stages in its rows (row 0 = REM;
    1 = stage 1 NREM; 2 = stage 2; 3 = stage 3 and 4) and the sampling rate for
    the data in Hz (samples per second).
    """
    data = np.load(filename)
    return data['examples'], int(data['srate'])

def load_eeg(filename):
    """
    load_eeg takes the file name and reads in the data.  It returns an
    array containing EEG data and the sampling rate for
    the data in Hz (samples per second).
    """
    data = np.load(filename)
    return data['eeg'], int(data['srate'])

def load_stages(filename):
    """
    load_stages takes the file name and reads in the stages data.  It returns an
    array containing the correct stages (one for each 30s epoch)
    """
    data = np.load(filename)
    return data['stages']

def plot_example_psds(example, rate):
    """
    This function creates a figure with 4 lines to show the overall psd for
    the four sleep examples. (Recall row 0 is REM, rows 1-3 are NREM stages 1,
    2 and 3/4)
    """
    NFFT = 512
    rem = example[0]
    nrem_stage1 = example[1]
    nrem_stage2 = example[2]
    nrem_stage34 = example[3]

    plt.figure()

    ##YOUR CODE HERE
    Pxx, freqs = m.psd(rem, NFFT=NFFT, Fs=rate)
    Pxx = Pxx / np.sum(Pxx)  # normalize
    plt.plot(freqs, Pxx, label='rem')
    plt.hold(True)

    Pxx, freqs = m.psd(nrem_stage1, NFFT=NFFT, Fs=rate)
    Pxx = Pxx / np.sum(Pxx)  # normalize
    plt.plot(freqs, Pxx, label='nrem stage 1')

    Pxx, freqs = m.psd(nrem_stage2, NFFT=NFFT, Fs=rate)
    Pxx = Pxx / np.sum(Pxx)  # normalize
    plt.plot(freqs, Pxx, label='nrem stage 2')

    Pxx, freqs = m.psd(nrem_stage34, NFFT=NFFT, Fs=rate)
    Pxx = Pxx / np.sum(Pxx)  # normalize
    plt.plot(freqs, Pxx, label='nrem stage 3/4')

    # Plot details
    plt.xlim([0, 20])
    plt.legend()
    plt.show()

    return

def plot_example_spectrograms(example,rate):
    """
    This function creates a figure with spectrogram sublpots to of the four
    sleep examples. (Recall row 0 is REM, rows 1-3 are NREM stages 1,
    2 and 3/4)
    """

    NFFT = 2**8
    rem = example[0]
    nrem_stage1 = example[1]
    nrem_stage2 = example[2]
    nrem_stage34 = example[3]

    plt.figure()
    plt.hold(True)

    ###YOUR CODE HERE
    plt.subplot(2, 2, 1)
    Pxx, freqs, bins, im  = plt.specgram(rem, NFFT=NFFT, Fs=rate)
    plt.title('Rem Sleep')
    plt.ylim((0,30))
    plt.xlabel('Time')
    plt.ylabel('Frequency')

    plt.subplot(2, 2, 2)
    Pxx, freqs, bins, im  = plt.specgram(nrem_stage1, NFFT=NFFT, Fs=rate)
    plt.title('Non-rem stage 1')
    plt.ylim((0,30))
    plt.xlabel('Time')
    plt.ylabel('Frequency')

    plt.subplot(2, 2, 3)
    Pxx, freqs, bins, im  = plt.specgram(nrem_stage2, NFFT=NFFT, Fs=rate)
    plt.title('Non-rem stage 2')
    plt.ylim((0,30))
    plt.xlabel('Time')
    plt.ylabel('Frequency')

    plt.subplot(2, 2, 4)
    Pxx, freqs, bins, im  = plt.specgram(nrem_stage34, NFFT=NFFT, Fs=rate)
    plt.title('Non-rem stage 3/4')
    plt.ylim((0,30))
    plt.xlabel('Time')
    plt.ylabel('Frequency')

    plt.show()
#    return Pxx, freqs

def predictor():

    example_filename = 'example_stages.npz'
    examples, srate = load_examples(example_filename)

    NFFT = 2**8
    rate = srate
    number_of_features = 45
    c = 1e5

    nrem1 = examples[1]
    nrem2 = examples[2]
    nrem3 = examples[3]

    Pxx1, freqs, bins, im  = plt.specgram(nrem1, NFFT=NFFT, Fs=rate)
    Pxx2, freqs, bins, im  = plt.specgram(nrem2, NFFT=NFFT, Fs=rate)
    Pxx3, freqs, bins, im  = plt.specgram(nrem3, NFFT=NFFT, Fs=rate)
    plt.show()

    Normalize = False
    # Normalize
    if Normalize:
        sum = np.sum(Pxx1, axis=1)
        Pxx1 = Pxx1 / sum[:,np.newaxis]

        sum = np.sum(Pxx2, axis=1)
        Pxx2 = Pxx2 / sum[:,np.newaxis]

        sum = np.sum(Pxx3, axis=1)
        Pxx3 = Pxx3 / sum[:,np.newaxis]

    x1 = np.arange(0,8)
    x2 = np.arange(10,15)
    x4 = np.concatenate((x1,x2))

    x4 = np.arange(0,15)

    if True:
        Pxx1 = Pxx1[0:number_of_features, :]
        Pxx2 = Pxx2[0:number_of_features, :]
        Pxx3 = Pxx3[0:number_of_features, :]

    if False:
        Pxx1 = Pxx1[x4, :]
        Pxx2 = Pxx2[x4, :]
        Pxx3 = Pxx3[x4, :]

    value1 = 1
    value2 = 2
    value3 = 3

    y1 = np.ones(Pxx1.shape[1]) * value1
    y2 = np.ones(Pxx2.shape[1]) * value2
    y3 = np.ones(Pxx3.shape[1]) * value3

    x = np.hstack([Pxx1, Pxx2, Pxx3])
    x = x.transpose()
    y = np.hstack([y1, y2, y3])

    lr = LogisticRegression(C=c, penalty='l1')
    lr.fit(x, y)

    return lr

def classify_epoch(epoch , rate, lr):
    """
    This function returns a sleep stage classification (integers: 1 for NREM
    stage 1, 2 for NREM stage 2, and 3 for NREM stage 3/4) given an epoch of
    EEG and a sampling rate.
    """

    NFFT = 2**8
    Pxx, freqs = m.psd(epoch, NFFT=NFFT, Fs=rate)

    p = map(lr.predict, Pxx[np.newaxis,0:45])
    p = np.array(p)
    p = p[:,0]

    stage = int(p)
    ###YOUR CODE HERE
    return stage

def plot_hypnogram(eeg, stages, srate, title):
    """
    This function takes the eeg, the stages and sampling rate and draws a
    hypnogram over the spectrogram of the data.
    """
    NFFT = 2**8

    fig,ax1 = plt.subplots()  #Needed for the multiple y-axes

    #Use the specgram function to draw the spectrogram as usual
    Pxx, freqs, bins, im  = plt.specgram(eeg, NFFT=NFFT, Fs=srate)
    #Label your x and y axes and set the y limits for the spectrogram
    plt.ylim((0,30))
    plt.xlim((0,3600))
    plt.xlabel('Time (seconds)')
    plt.ylabel('Frequency (Hz)')

    ax2 = ax1.twinx() #Necessary for multiple y-axes

    #Use ax2.plot to draw the hypnogram.  Be sure your x values are in seconds
    #HINT: Use drawstyle='steps' to allow step functions in your plot
    t = np.arange(0, stages.size) * 30
    ax2.plot(t, stages, drawstyle='steps')
    #Label your right y-axis and change the text color to match your plot
    ax2.set_ylabel('NREM Sleep Stage',color='b')

    plt.xlim((0,3600))

    #Set the limits for the y-axis
    plt.ylim((0.5,3.5))

    #Only display the possible values for the stages
    ax2.set_yticks(np.arange(1,4))

    #Change the left axis tick color to match your plot
    for t1 in ax2.get_yticklabels():
        t1.set_color('b')

    #Title your plot
    plt.title(title)

def classifier_tester(classifiedEEG, actualEEG):
    """
    returns percent of 30s epochs correctly classified
    """
    epochs = len(classifiedEEG)
    incorrect = np.nonzero(classifiedEEG-actualEEG)[0]
    percorrect = (epochs - len(incorrect))/epochs*100

    print 'EEG Classifier Performance: '
    print '     Correct Epochs = ' + str(epochs-len(incorrect))
    print '     Incorrect Epochs = ' + str(len(incorrect))
    print '     Percent Correct= ' + str(percorrect)
    print
    return percorrect

def test_examples(examples, srate, lr):
    """
    This is one example of how you might write the code to test the provided
    examples.
    """
    i = 0
    bin_size = int(30*srate)
    c = np.zeros((4,int(len(examples[1,:])/bin_size)))
    while i + bin_size < len(examples[1,:]):
        for j in range(1,4):
            c[j,int(i/bin_size)] = classify_epoch(examples[j, range(i,i+bin_size)],srate, lr)
        i = i + bin_size

    totalcorrect = 0
    num_examples = 0
    for j in range(1,4):
        canswers = np.ones(len(c[j,:]))*j
        correct = classifier_tester(c[j,:],canswers)
        totalcorrect = totalcorrect + correct
        num_examples = num_examples + 1

    average_percent_correct = totalcorrect/num_examples
    print 'Average Percent Correct= ' + str(average_percent_correct)
    return average_percent_correct

def classify_eeg(eeg,srate, lr):
    """
    DO NOT MODIFY THIS FUNCTION
    classify_eeg takes an array of eeg amplitude values and a sampling rate and
    breaks it into 30s epochs for classification with the classify_epoch function.
    It returns an array of the classified stages.
    """
    bin_size_sec = 30
    bin_size_samp = int(bin_size_sec*srate)
    t = 0
    classified = np.zeros(int(len(eeg)/bin_size_samp))
    while t + bin_size_samp < len(eeg):
       classified[int(t/bin_size_samp)] = classify_epoch(eeg[range(t,t+bin_size_samp)],srate, lr)
       t = t + bin_size_samp
    return classified

##########################
#You can put the code that calls the above functions down here
if __name__ == "__main__":
    #YOUR CODE HERE

    plt.close('all') #Closes old plots
    ##PART 1
    #Load the example data
    example_filename = 'example_stages.npz'
    examples, srate = load_examples(example_filename)

    #Plot the psds
    #plot_example_psds(examples, srate)

    #Plot the spectrograms
    #plot_example_spectrograms(examples, srate)

    # predictor
    lr = predictor()

    #Test the examples
    #test_examples(examples, srate, lr)

    #Load the practice data
    practice_eeg, srate = load_eeg('practice_eeg.npz')
    #Load the practice answersp
    practice_stages = load_stages('practice_answers.npz')
    #Classify the practice data
    #classified = classify_eeg(practice_eeg ,srate, lr)
    #Check your performance
    #classifier_tester(classified, practice_stages)
    #Generate the hypnogram plots
    eeg, srate = load_eeg('test_eeg.npz')
    stages = classify_eeg(eeg, srate, lr)
    #plot hynporgram
    #plot_hypnogram(eeg, stages, srate, 'Hypnogram - Test Data Set')
    plot_hypnogram(practice_eeg, practice_stages, srate, 'Hynpogram - Practice Data Set')
    plt.show()

    if False:
        plot_hypnogram(practice_eeg, classified, srate, 'mine')
        plt.show()
        plot_hypnogram(practice_eeg, practice_stages, srate, 'correct')
        plt.show()

max_stars_repo_name: h-mayorquin/coursera

max_issues_repo_name: h-mayorquin/coursera

max_forks_repo_name: h-mayorquin/coursera


text:
import numpy as np
import sys
import pickle
from openpyxl import load_workbook
from main_cloud_ranger import test_cloud_ranger
import concurrent.futures
from concurrent.futures import as_completed
from tqdm import tqdm

# pymicro test suite
# dataset_name = 'pymicro'
# entry_point_list = [16]
# true_root_cause = [1]

# ibm_micro_service test suite
dataset_name = 'ibm_micro_service'
entry_point_list = [14]
true_root_cause = [6, 28, 30, 31]

params_list = []
for ela in range(1, 21):
    for alpha in [0.1, 0.2, 0.3, 0.4, 0.5]:
        for testround in [5]:
            for beta in [0.1]:
                for rho in [0.2]:
                    params_list.append({'ela': ela, 'alpha': alpha, 'testround': testround,
                                    'beta': beta, 'rho': rho})

def worker_process(ind, params_dict):
    prks, acc = test_cloud_ranger(
        data_source=dataset_name,
        pc_aggregate=params_dict['ela'],
        pc_alpha=params_dict['alpha'],
        testrun_round=params_dict['testround'],
        frontend=entry_point_list[0],
        true_root_cause=true_root_cause,
        beta=params_dict['beta'],
        rho=params_dict['rho'],
        save_data_fig=False,
        verbose=False,
        disable_print=True,
    )
    return prks, acc, ind

def main():
    # print("\n{:!^80}\n".format(" Pymicro Test Suite "))
    print("\n{:!^80}\n".format(" IBM Micro Service Test Suite "))
    print('Number of parameter sets: {}'.format(len(params_list)))
    processbar = tqdm(total=len(params_list), ascii=True)
    futures = []
    executor = concurrent.futures.ProcessPoolExecutor(max_workers=6)
    for i, params_dict in enumerate(params_list):
        futures.append(executor.submit(worker_process, i, params_dict))
    failed_task = 0
    result_list = []
    for future in as_completed(futures):
        processbar.update(1)
        try:
            prks, acc, ind = future.result()
            result_list.append(params_list[ind])
            result_list[-1]['prks'] = prks
            result_list[-1]['acc'] = acc
        except Exception as e:
            failed_task += 1
            print('Exception:{}, 1 task failed! Total failed: {}'
                    .format(e, failed_task))
    executor.shutdown()
    if len(result_list) > 0:
        with open('cloudranger_parameter_tune_ibm.pkl', 'wb') as f:
            pickle.dump(result_list, f)

if __name__ == '__main__':
    main()


text:
import argparse
import os
import random
import time

import models
import numpy as np
import torch
import torchvision
from PIL import Image
from dataset_generator import DatasetGenerator
from sklearn.metrics import average_precision_score
from sklearn.metrics import roc_auc_score, roc_curve, auc
from torch import nn
from torch import optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from torchvision import transforms

mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
image_size = 224
image_resize = 256
class_names = ['Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass', 'Nodule', 'Pneumonia',
               'Pneumothorax', 'Consolidation', 'Edema', 'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia']

def get_input_args():
    """
    Retrieves and parses the 3 command line arguments provided by the user when
    they run the program from a terminal window. This function uses Python's
    argparse module to created and defined these 3 command line arguments. If
    the user fails to provide some or all of the 3 arguments, then the default
    values are used for the missing arguments.
    """
    # create Parse using Argument Parser
    parser = argparse.ArgumentParser()

    # create command line arguments using add argument from the Argument Parser method
    available_models = [m for m in dir(models) if 'Net' in m and 'Model' not in m]
    parser.add_argument('--arch', choices=available_models, type=str, default='ResNet50',
                        help='string containing used architecture')

    parser.add_argument('--epoch', type=int, default=2, help='number of epochs')

    parser.add_argument('--gpu', action='store_true', help='use gpu if available')

    parser.add_argument('--batch', type=int, default=64, help='number of images per batch')

    parser.add_argument('--image_dir', type=str, default='database', help='image directory')

    parser.add_argument('--dataset_dir', type=str, default='dataset', help='csv label file directory')

    parser.add_argument('--checkpoint_dir', type=str, default='checkpoints', help='used in save checkpoint path')

    parser.add_argument('--prior_checkpoint', type=str, required=False,
                        help='existing checkpoint from prior training to bootstrap from')

    parser.add_argument('--learning_rate', type=float, required=True, help='step size in optimizer')

    return parser.parse_args()

print(torch.__version__)
print(torchvision.__version__)

SEED = 1234

random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

# load the pretrained model corresponding to the specified architecture
def load_pretrained_model(arch):
    model_func = getattr(models, arch)
    model = model_func()
    model.arch = arch

    return model

# used during training: disable gradients, feedforward, calculate test loss
def validate(model, valid_dataloader, loss_func, device):
    # track accuracy and loss
    test_loss = 0

    with torch.no_grad():  # deactivates requires_grad flag, disables tracking of gradients
        for images, labels in valid_dataloader:  # iterate over images and labels in valid dataset
            images, labels = images.to(device), labels.to(device)  # move a tensor to a device
            ps = model.forward(images)  # probabilities for each label
            test_loss += loss_func(ps, labels)  # take all values

    return test_loss

# used at the end of each epoch: get true labels and predicted probabilities, call calc_roc_metrics
def test_model(model, test_loader, device):
    # track accuracy, move to device, switch on eval mode
    model.to(device)
    model.eval()
    pred_out = torch.FloatTensor().to(device)
    labels_out = torch.FloatTensor().to(device)

    with torch.no_grad():
        for images, labels in test_loader:
            images, labels = images.to(device), labels.to(device)  # move a tensor to a device
            labels_out = torch.cat((labels_out, labels), 0)
            ps = model.forward(images)
            pred_out = torch.cat((pred_out, ps), 0)

    metrics = calc_roc_metrics(pred_out, labels_out)
    return metrics

# evaluation metric: roc and auc metrics
def calc_roc_metrics(pred_out, labels_out):
    fpr = dict()
    tpr = dict()
    roc_auc = dict()

    # https://github.com/rachellea/glassboxmedicine/blob/master/2020-07-14-AUROC-AP/main.py
    # http://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html

    y_true = labels_out.cpu().numpy()
    y_score = pred_out.cpu().numpy()

    for idx, clz in enumerate(class_names):
        fpr[clz], tpr[clz], _ = roc_curve(y_true[:, idx], y_score[:, idx])
        roc_auc[clz] = auc(fpr[clz], tpr[clz])

    # Compute micro-average ROC curve and ROC area
    fpr["micro"], tpr["micro"], _ = roc_curve(y_true.ravel(), y_score.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

    # Compute macro-average ROC curve and ROC area
    all_fpr = np.unique(np.concatenate([fpr[clz] for clz in class_names]))

    # Then interpolate all ROC curves at these points
    mean_tpr = np.zeros_like(all_fpr)
    for clz in class_names:
        mean_tpr += np.interp(all_fpr, fpr[clz], tpr[clz])

    # Finally average it and compute AUC
    mean_tpr /= len(class_names)

    fpr["macro"] = all_fpr
    tpr["macro"] = mean_tpr
    roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

    average_precision = average_precision_score(y_true, y_score)
    roc_auc_mean = roc_auc_score(y_true, y_score)

    return {
        'average_precision_score': average_precision_score(y_true, y_score),
        'roc_auc_score': roc_auc_score(y_true, y_score),
        'fpr': fpr,
        'tpr': tpr,
        'roc_auc': roc_auc
    }

# save the checkpoint
def save_checkpoint(checkpoint_path, model, optimizer, scheduler, epoch):
    checkpoint = {
        'arch': model.arch,
        'metrics': model.metrics,
        'state_dict': model.state_dict(),
        'optimizer': optimizer.state_dict(),
        'scheduler': scheduler.state_dict(),
        'epoch': epoch
    }
    torch.save(checkpoint, checkpoint_path)
    print('Saved the trained model: %s' % checkpoint_path)

def load_checkpoint(ckp_path, pretrained_model, optimizer, scheduler, device, arch):
    model_checkpoint = torch.load(ckp_path, map_location=torch.device(device))

    if model_checkpoint['arch'] != arch:
        print('Mismatched arch: Model Checkpoint=%s vs Arch=%s' % (model_checkpoint['arch'], arch))
        return False

    state_dict = model_checkpoint['state_dict']
    saved_optimizer = model_checkpoint['optimizer']
    saved_scheduler = model_checkpoint['scheduler']

    # check the state dictionary matches
    old_state_dict = pretrained_model.state_dict()
    for k in state_dict:
        if k not in old_state_dict:
            print('Unexpected key %s in state_dict' % k)
    for k in old_state_dict:
        if k not in state_dict:
            print('Missing key %s in state_dict' % k)

    # TBD: load checkpoint into pretrained_model
    pretrained_model.load_state_dict(state_dict)
    optimizer.load_state_dict(saved_optimizer)
    scheduler.load_state_dict(saved_scheduler)

    print('Model checkpoint successfully loaded from %s' % ckp_path)

    return True

# train the model
def train(model, loss_func, optimizer, scheduler, dataloaders, device, epochs, checkpoint_prefix, print_every=100):
    device = torch.device(device)
    model.to(device)

    train_loader = dataloaders['train']
    valid_loader = dataloaders['valid']
    test_loader = dataloaders['test']

    epoch_start = time.time()
    max_acc = 0.0

    # loop to train for number of epochs
    for e in range(epochs):
        running_loss = 0
        batch_start = time.time()
        steps = 0

        for images, labels in train_loader:
            # within each loop, iterate train_loader, and print loss
            images, labels = images.to(device), labels.to(device)
            optimizer.zero_grad()
            ps = model.forward(images)
            loss = loss_func(ps, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()

            steps += 1

            if steps % print_every == 0:
                model.eval()
                valid_loss = validate(model, valid_loader, loss_func, device)
                model.train()
                batch_time = time.time() - batch_start
                print(
                    "Epoch: {}/{}..".format(e + 1, epochs),
                    "Step: {}..".format(steps),
                    "Training Loss: {:.5f}..".format(running_loss / len(train_loader)),
                    "Test Loss: {:.5f}..".format(valid_loss / len(valid_loader)),
                    "Batch Time: {:.1f}, avg: {:.3f}".format(batch_time, batch_time / steps)
                )

        model.eval()
        valid_loss = validate(model, valid_loader, loss_func, device)
        model.train()

        scheduler.step(valid_loss / len(valid_loader))

        model.eval()
        metrics = test_model(model, test_loader, device)
        roc_aoc_mean = metrics['roc_auc_score']
        average_precision_mean = metrics['average_precision_score']
        model.train()
        epoch_time = time.time() - epoch_start

        if roc_aoc_mean > max_acc:
            max_acc = roc_aoc_mean
            model.metrics = metrics
            save_checkpoint('%s.pth.tar' % checkpoint_prefix, model, optimizer, scheduler, e + 1)
            print(
                "Epoch: {}/{} [save] Epoch Time={:.1f}..".format(e + 1, epochs, epoch_time),
                "Average Precision: {:.3f}..".format(average_precision_mean),
                "ROC: {:.3f}..".format(roc_aoc_mean),
                "Batch Time: {:.1f}, avg: {:.3f}".format(batch_time, batch_time / steps)
            )
        else:
            print(
                "Epoch: {}/{} [----] Epoch Time={:.1f}..".format(e + 1, epochs, epoch_time),
                "Average Precision: {:.3f}..".format(average_precision_mean),
                "ROC: {:.3f}..".format(roc_aoc_mean),
                "Batch Time: {:.1f}, avg: {:.3f}".format(batch_time, batch_time / steps)
            )

    return model

def main():
    in_arg = get_input_args()

    training_start = time.time()
    checkpoint_prefix = os.path.join(in_arg.checkpoint_dir, '%s-%d' % (in_arg.arch, training_start))
    print('{} --> {}'.format(in_arg, checkpoint_prefix))
    batch_size = in_arg.batch

    image_dir = in_arg.image_dir
    train_csv_file = in_arg.dataset_dir + '/train.txt'
    test_csv_file = in_arg.dataset_dir + '/test.txt'
    valid_csv_file = in_arg.dataset_dir + '/valid.txt'

    # TODO: Define your transforms for the training, validation, and testing sets
    train_transforms = transforms.Compose([
        transforms.RandomOrder([
            transforms.ColorJitter(hue=.05, saturation=.05),
            transforms.RandomHorizontalFlip(),
            transforms.RandomRotation(15, resample=Image.BILINEAR),
            transforms.RandomResizedCrop(image_size, scale=(0.9, 1.0)),
        ]),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
    ])

    valid_transforms = transforms.Compose([
        transforms.Resize(image_resize),
        transforms.CenterCrop(image_size),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
    ])

    test_transforms = valid_transforms

    # TODO: Load the Datasets using Dataset Generator
    train_dataset = DatasetGenerator(train_csv_file, image_dir, transform=train_transforms)
    test_dataset = DatasetGenerator(test_csv_file, image_dir, transform=test_transforms)
    valid_dataset = DatasetGenerator(valid_csv_file, image_dir, transform=valid_transforms)

    # TODO: Using the image datasets and the transforms, define the dataloaders
    dataloaders = {"train": DataLoader(train_dataset, batch_size=batch_size, num_workers=24, shuffle=True),
                   "test": DataLoader(test_dataset, batch_size=batch_size, num_workers=24, shuffle=False),
                   "valid": DataLoader(valid_dataset, batch_size=batch_size, num_workers=24, shuffle=False)}

    device = 'cpu'
    if in_arg.gpu:
        cuda = torch.cuda.is_available()
        if cuda:
            # make sure if GPU is available, we can use it by model.to(device) in train
            device = 'cuda'

    pretrained_model = load_pretrained_model(in_arg.arch)
    pretrained_model.to(device)
    optimizer = optim.Adam(pretrained_model.parameters(),
                           lr=in_arg.learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)
    criterion = nn.BCELoss(reduction='mean')
    scheduler = ReduceLROnPlateau(optimizer, factor=0.1, patience=5, mode='min')

    if in_arg.prior_checkpoint:
        checkpoint_path = os.path.join(in_arg.checkpoint_dir, in_arg.prior_checkpoint)
        checkpoint_status = load_checkpoint(checkpoint_path, pretrained_model, optimizer, scheduler, device,
                                            in_arg.arch)
        if not checkpoint_status:
            return

    trained_model = train(pretrained_model, criterion, optimizer, scheduler, dataloaders, device, in_arg.epoch,
                          checkpoint_prefix, print_every=300)
    metrics = trained_model.metrics
    print('%.2f seconds taken for model training' % (time.time() - training_start))
    print('Metrics: average_precision_score={}, roc_auc_score={}'.format(metrics['average_precision_score'],
                                                                         metrics['roc_auc_score']))

if __name__ == "__main__":
    main()

max_stars_repo_name: joycebyang/covidx

max_issues_repo_name: joycebyang/covidx

max_forks_repo_name: joycebyang/covidx


text:
import argparse

import cv2
import torch
import numpy as np

import config
import data_util
from g_net import VideoGANGenerator

MODEL_FILEPATH = 'generator_net.pth'
NUM_RECURSIONS = 64
HIST_LEN = 4
CROP_HEIGHT = 32
CROP_WIDTH = 32
DTYPE = config.dtype

def crop_batch(images, fw, fh, cw, ch):
    images = images[0]
    batch = []
    for i in range(int(np.ceil(fw/cw))):
        for j in range(int(np.ceil(fh/ch))):
            img = np.zeros([cw, ch, images.shape[2]])
            x = cw
            if ((i+1) * cw) > fw:
                x = fw - (i * cw)
            y = ch
            if ((j+1) * ch) > fh:
                y = fh - (j * ch)
            img[:x, :y, :] = images[i * cw: min(fw, (i + 1) * cw),
                                    j * ch: min(fh, (j + 1) * ch),
                                    :]
            batch.append(img)
    batch = np.stack(batch, axis=0)
    return batch

def reconstruct_frame(image_batch, out_w, out_h):
    generated_images = image_batch

    num_images, channels, cell_h, cell_w = generated_images.shape
    nrows = int(np.ceil(out_w/cell_w))
    ncols = int(np.ceil(out_h/cell_h))
    result = np.zeros((cell_w * nrows, cell_h * ncols, channels), dtype=generated_images.dtype)
    for i in range(nrows):
        for j in range(ncols):
            img_patch = generated_images[i * ncols + j].transpose(1, 2, 0)
            result[i * cell_w: (i + 1) * cell_w,
                   j * cell_h: (j + 1) * cell_h,
                   :] = img_patch

    return result[:out_w, :out_h, :]

def save_to_video(frames, video_filename):
#    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
#    video = cv2.VideoWriter(video_filename, fourcc,
#                            20.0, (frames[0].shape[1], frames[0].shape[0]))
    for i, frame in enumerate(frames):
#        video.write(frame)
        print(frame.shape)
        cv2.imwrite(video_filename + '_{}.jpg'.format(i), frame)
#    video.release()
#    cv2.destroyAllWindows()

def main():
    parser = argparse.ArgumentParser(
        description="""
            Script to run inference on a random input seeding
            of videoGAN
        """)
    parser.add_argument(
        'video_filename',
        help='output filename of video')
    parser.add_argument(
        '-i', '--input_dir',
        default='Ms_Pacman/Test',
        help='input dir of training to take random images from')

    args = parser.parse_args()

    # Load generator.
    generator = VideoGANGenerator()
    generator.load_state_dict(torch.load(MODEL_FILEPATH))
    generator.eval()

    # Load input seed.
    frames = data_util.get_full_clips(args.input_dir, HIST_LEN-1, 1)
    frames = frames[:, 0:200, :, :];
    print("frames.shape is", frames.shape)
    frame_w, frame_h = frames[0].shape[0:2]

    # Set initial frames.
    input_frames = data_util.denormalize_frames(frames)
    print("input_frames.shape is", input_frames.shape)
    # input_batched = crop_batch(frames, frame_w, frame_h,
    #                            CROP_WIDTH, CROP_HEIGHT)
    input_batched = input_frames
    # input_batched = np.array([input_frames[0, :, :, :3], input_frames[0, :, :, 3:6],
    #                           input_frames[0, :, :, 6:9], input_frames[0, :, :, 9:]])

    # Initialize output frames.
    output_frames = [input_frames[0, :, :, :3], input_frames[0, :, :, 3:6],
                     input_frames[0, :, :, 6:9], input_frames[0, :, :, 9:]]

    # Run inference for length of recursions.
    for i in range(NUM_RECURSIONS):
        print('{} of {} frames'.format(i + 1, NUM_RECURSIONS))

        # Run inference and reconstruct frame.
        input_batched_tensor = torch.tensor(np.rollaxis(input_batched, 3, 1)).type(DTYPE)
        result = generator(input_batched_tensor).detach().cpu().numpy()
        print("generated image shape is", result.shape)

        # Post-process frame
        # result_reconst = reconstruct_frame(result, frame_w, frame_h)
        result_denorm = data_util.denormalize_frames(result)
        print(result_denorm.shape)
        output_frames.append(result_denorm.transpose(0, 2, 3, 1)[0])
        print(type(output_frames[-1]))

        # Setup next batch.
        input_batched = np.concatenate((input_batched, result.transpose(0, 2, 3, 1)), axis=3)
        input_batched = input_batched[:, :, :, 3:]

    # Save frames
    save_to_video(output_frames, args.video_filename)

if __name__ == '__main__':
    main()

max_stars_repo_name: wileyw/VideoGAN


text:
# coding: utf-8

import numpy as np
import pandas as pd
import xtools as xt
import xtools.simulation as xs
from matplotlib import pyplot as plt
import gym
import xaircraft
from xaircraft.envs.lvaircraft_pitch import LVAircraftPitch, LVAircraftPitchV0

def test_LVAircraft_Pitch_GymEnv():
    xt.info("test for LVAir Pitch gym")

    dt = 0.1
    due = 60

    env = gym.make("LVAircraftPitch-v1", dt=dt)
    xt.info("env", env)

    Kp = -2.5

    buf = xs.ReplayBuffer({
        "time": 1,
        "act": 2,
        "obs": env.observation_size,
        "reward": 1
    }, capacity=int(due / dt + 1))

    # reset env
    obs = env.reset()
    act = Kp * (obs[env.IX_C] - obs[env.IX_T])
    act = np.array([0, act]).astype(np.float32)
    buf.add(time=0, obs=obs, act=act, reward=env.calc_reward(obs))

    for time in xs.generate_step_time(due, dt):
        error = obs[env.IX_C] - obs[env.IX_T]
        act = Kp * error
        act = np.array([0, act]).astype(np.float32)
        next_obs, reward, done, _ = env.step(act)

        buf.add(time=time, act=act, obs=next_obs, reward=reward)
        obs = next_obs

    result = xs.Retriever(buf.buffer())
    result = pd.DataFrame({
        "time": result("time"),
        "pitch": result("obs", idx=env.IX_T),
        "target": result("obs", idx=env.IX_C),
        "elevator": result("act", idx=1),
        "reward": result("reward")
    })
    fig, axes = plt.subplots(nrows=3, sharex=True)
    result.plot(x="time", y=["target", "pitch"], ax=axes[0])
    result.plot(x="time", y="elevator", ax=axes[1])
    result.plot(x="time", y="reward", ax=axes[2])
    plt.show()

def test_LVAircraftPitch():
    xt.info("test for LVAircraftPitch")

    dt = 0.02
    due = 300.0
    air = LVAircraftPitch(dt)
    xt.debug("LVAircraftPitch", air)
    action = np.array([1, xt.d2r(1)])

    # logger
    log = xs.ReplayBuffer({"time": 1, "command": 2, "state": 2}, capacity=int(due / dt + 1))
    log.add(time=0, command=action, state=air.get_obs())
    xt.debug("log", log)

    for t in xs.generate_step_time(due, dt):
        print("[time]", t)
        next_Tq = air.step(action)
        log.add(time=t, command=action, state=next_Tq)
        xt.debug("[T, q]", next_Tq)
        print("-"*30)

    results = log.buffer()
    results = pd.DataFrame({
        "time": np.squeeze(results["time"]),
        "theta": np.squeeze(results["state"][:, 0]),
        "q": np.squeeze(results["state"][:, 1])
    })

    fig, axes = plt.subplots(nrows=2, sharex=True)
    plt.xlim([0, due])
    axes[0].hlines(0, 0, due)
    axes[1].hlines(0, 0, due)
    results.plot(x="time", y="theta", ax=axes[0])
    results.plot(x="time", y="q",     ax=axes[1])
    plt.show()

if __name__ == '__main__':
    test_LVAircraft_Pitch_GymEnv()

max_stars_repo_name: xikasan/xaircraft

max_issues_repo_name: xikasan/xaircraft

max_forks_repo_name: xikasan/xaircraft


text:
"""
Creates Model Parameters
TensorFlow 1.4
"""
from __future__ import print_function

import time

import numpy as np
import tensorflow as tf

import Config_Params

class BotModel(object):
    def __init__(self, forward_only, batch_size):
        """forward_only: no backward pass
        """
        print('Initialize new model')
        self.fw_only = forward_only
        self.batch_size = batch_size

    def _create_placeholders(self):
        # Feeds : inputs(placeholders)
        print('Create placeholders')
        self.encoder_inputs = [tf.placeholder(tf.int32, shape=[None], name='encoder{}'.format(i))
                               for i in range(Config_Params.BUCKETS[-1][0])]
        self.decoder_inputs = [tf.placeholder(tf.int32, shape=[None], name='decoder{}'.format(i))
                               for i in range(Config_Params.BUCKETS[-1][1] + 1)]
        self.decoder_masks = [tf.placeholder(tf.float32, shape=[None], name='mask{}'.format(i))
                              for i in range(Config_Params.BUCKETS[-1][1] + 1)]

        # Targets : decoder inputs
        self.targets = self.decoder_inputs[1:]

    def _inference(self):
        print('Inference, SampleSoftmax')

        if Config_Params.NUM_SAMPLES > 0 and Config_Params.NUM_SAMPLES < Config_Params.DEC_VOCAB:
            w = tf.get_variable('proj_w', [Config_Params.HIDDEN_SIZE, Config_Params.DEC_VOCAB])
            b = tf.get_variable('proj_b', [Config_Params.DEC_VOCAB])
            self.output_projection = (w, b)

        def sampled_loss(logits, labels):  # labels, inputs
            labels = tf.reshape(labels, [-1, 1])
            local_w_t = tf.cast(tf.transpose(w), tf.float32)
            local_b = tf.cast(b, tf.float32)
            local_inputs = tf.cast(logits, tf.float32)
            return tf.cast(
                tf.nn.sampled_softmax_loss(
                    weights=tf.transpose(w),
                    biases=local_b,
                    labels=labels,
                    inputs=local_inputs,
                    num_sampled=Config_Params.NUM_SAMPLES,
                    num_classes=Config_Params.DEC_VOCAB), tf.float32)

        self.softmax_loss_function = sampled_loss

    def _create_loss(self):
        print('Creating loss...')
        start = time.time()

        def _seq2seq_f(encoder_inputs, decoder_inputs, do_decode):
            def single_cell():
                return tf.contrib.rnn.GRUCell(Config_Params.HIDDEN_SIZE)

            cell = single_cell()
            if Config_Params.NUM_LAYERS > 1:
                cell = tf.contrib.rnn.MultiRNNCell([single_cell() for _ in range(Config_Params.NUM_LAYERS)])
            return tf.contrib.legacy_seq2seq.embedding_attention_seq2seq(
                encoder_inputs, decoder_inputs, cell,
                num_encoder_symbols=Config_Params.ENC_VOCAB,
                num_decoder_symbols=Config_Params.DEC_VOCAB,
                embedding_size=Config_Params.HIDDEN_SIZE,
                output_projection=self.output_projection,
                feed_previous=do_decode)

        if self.fw_only:
            self.outputs, self.losses = tf.contrib.legacy_seq2seq.model_with_buckets(
                self.encoder_inputs,
                self.decoder_inputs,
                self.targets,
                self.decoder_masks,
                Config_Params.BUCKETS,
                lambda x, y: _seq2seq_f(x, y, True),
                softmax_loss_function=self.softmax_loss_function)
            # If we use output projection, we need to project outputs for decoding.
            if self.output_projection:
                for bucket in range(len(Config_Params.BUCKETS)):
                    self.outputs[bucket] = [tf.matmul(output,
                                                      self.output_projection[0]) + self.output_projection[1]
                                            for output in self.outputs[bucket]]
        else:
            self.outputs, self.losses = tf.contrib.legacy_seq2seq.model_with_buckets(
                self.encoder_inputs,
                self.decoder_inputs,
                self.targets,
                self.decoder_masks,
                Config_Params.BUCKETS,
                lambda x, y: _seq2seq_f(x, y, False),
                softmax_loss_function=self.softmax_loss_function)
        print('Time:', time.time() - start)

    def _creat_optimizer(self):
        print('Create optimizer...')
        with tf.variable_scope('training') as scope:
            self.global_step = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')

            if not self.fw_only:
                self.optimizer = tf.train.GradientDescentOptimizer(Config_Params.LR)
                trainables = tf.trainable_variables()
                self.gradient_norms = []
                self.train_ops = []
                start = time.time()
                for bucket in range(len(Config_Params.BUCKETS)):
                    clipped_grads, norm = tf.clip_by_global_norm(tf.gradients(self.losses[bucket],
                                                                              trainables),
                                                                 Config_Params.MAX_GRAD_NORM)
                    self.gradient_norms.append(norm)
                    self.train_ops.append(self.optimizer.apply_gradients(zip(clipped_grads, trainables),
                                                                         global_step=self.global_step))
                    print('Creating opt for bucket {} took {} seconds'.format(bucket, time.time() - start))
                    start = time.time()

    def _create_summary(self):
        pass

    def build_graph(self):
        self._create_placeholders()
        self._inference()
        self._create_loss()
        self._creat_optimizer()
        self._create_summary()


text:
from scipy.ndimage import gaussian_filter1d
import numpy as np
from sklearn.utils import check_random_state
from .networks import BaseEvolvingComponentsGenerator
from subclass_register import SubclassRegister

parametrisation_register = SubclassRegister("Parametrisation")

@parametrisation_register.link_base
class BaseParametrisation:
    pass

class Constant(BaseParametrisation):
    def __init__(self, value):
        self.value = value

    def __call__(self, time):
        return self.value

class Sinusodial(BaseParametrisation):
    def __init__(self, offset, amplitude, angular_frequency=1, phase_angle=0):
        self.offset = offset
        self.amplitude = amplitude
        self.angular_frequency = angular_frequency
        self.phase_angle = phase_angle

    def __call__(self, time):
        return (
            self.offset
             + self.amplitude*np.sin(time*self.angular_frequency + self.phase_angle)
        )

def get_component_parametrisation(component_params):
    component_parametrisation = {}

    radius_type = component_params['radius_type']
    radius_params = component_params['radius_params']
    RadiusType = parametrisation_register[radius_type]
    component_parametrisation['radius'] = RadiusType(**radius_params)

    position_type = component_params['position_type']
    position_params = component_params['position_params']
    PositionType = parametrisation_register[position_type]
    component_parametrisation['position'] = PositionType(**position_params)

    return component_parametrisation

class PlateausNetworksGenerator(BaseEvolvingComponentsGenerator):
    def __init__(
        self,
        num_components,
        num_timesteps,
        num_nodes,
        component_params,
        smoothing_factor=0,
        offset=0,
        random_state=None
    ):
        self.num_components = num_components
        self.num_timesteps = num_timesteps
        self.num_nodes = num_nodes
        self.component_params = component_params
        self.smoothing_factor= smoothing_factor
        self.offset = offset
        self.random_state = random_state

    def init_components(self, rng):
        self.component_parametrisations = [
            get_component_parametrisation(component_params) for component_params in self.component_params
        ]

    def generate_factors(self):
        rng = check_random_state(self.random_state)
        self.init_components(rng)
        B = np.zeros(shape=(self.num_timesteps, self.num_nodes, self.num_components))

        for k, B_k in enumerate(B):
            for r, _ in enumerate(self.component_parametrisations):
                b_kr = self.make_component_timestep(r, k)

                if self.smoothing_factor > 0:
                    b_kr = gaussian_filter1d(b_kr, sigma=self.smoothing_factor, mode="nearest")

                B_k[:, r] = b_kr

        return B

    def make_component_timestep(self, component, timestep):
        radius = self.component_parametrisations[component]['radius'](timestep)
        radius = abs(radius)
        position = self.component_parametrisations[component]['position'](timestep)
        component_vector = np.zeros(self.num_nodes)

        min_pos = max(0, int(position - radius))
        max_pos = int(position + radius)

        component_vector[min_pos:max_pos] = 1
        return component_vector

max_stars_repo_name: MarieRoald/synten

max_issues_repo_name: MarieRoald/synten

max_forks_repo_name: MarieRoald/synten

reason: import numpy,from scipy