"""
train merge models with SVM liblinear
"""
from liblinear.liblinearutil import *
import pandas as pd
import smote_variants as sv
import imbalanced_databases as imbd
import argparse, os
from pathlib import Path

def convert_model_format(model_in, model_out, features="tfidf_time"):

    if features == "tfidf":
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'ZZINVCLUSTER_SIZE']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'ZZINVCLUSTER_SIZE']
    elif features == "tfidf_time":
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE']
    elif features in set(['tfidf_time_sbert', 'tfidf_time_esbert', 'tfidf_time_sinpe_esbert']):
        if "tdt4" in model_in:
            feature_list = ['Entities_all','Lemmas_all', 'Tokens_all', 'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title',
                            'Lemmas_all', 'Lemmas_body', 'Lemmas_title',
                            'Tokens_all', 'Tokens_body', 'Tokens_title',
                            'NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
    elif features == "tfidf_sinpe_esbert":
        if "tdt4" in model_in:
            feature_list = ['Entities_all', 'Lemmas_all', 'Tokens_all', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
        else:
            feature_list = ['Entities_all', 'Entities_body', 'Entities_title', 'Lemmas_all', 'Lemmas_body', 'Lemmas_title', 'Tokens_all', 'Tokens_body', 'Tokens_title', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']
    elif features == "time_sinpe_esbert":
        feature_list = ['NEWEST_TS', 'OLDEST_TS', 'RELEVANCE_TS', 'ZZINVCLUSTER_SIZE', 'bert_sent_embeds']

    elif features == "sinpe_esbert":
        feature_list = ['ZZINVCLUSTER_SIZE', 'bert_sent_embeds']

    with open(model_in) as f:
        num_lines = len(f.readlines())-1 # excluding the extra one in the end

    with open(model_in) as f, open(model_out, "w") as out:
        featIdx = 0
        # bias
        lines = f.readlines()
        out.write(lines[-1].strip())
        out.write("\n")

        # weights
        # print(feature_list)
        for line in lines[6:-1]:
            line = feature_list[featIdx] + "\t" + line
            featIdx +=1
            out.write(line)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--data_path", type=str, default="./output/exp_time_esbert_ep2_mgn2.0_btch8_norm1.0_max_seq_128/tfidf_time", help="input_folder")
    parser.add_argument("--features", type=str, default="tfidf_time", help="input_folder")
    parser.add_argument("--use_smote", type=int, default=0, help="input_folder")
    parser.add_argument("-f")
    args = parser.parse_args()

    y, X = svm_read_problem(args.data_path)
    # y, X = svm_read_problem(os.path.join(args.input_folder, 'train_svmlib_balanced.dat'))
    args.input_folder = os.path.dirname(args.data_path)
    X = pd.DataFrame(X).values
    if args.use_smote:
        print("using oversampled data")
        oversampler= sv.SVM_balance()
        X_samp, y_samp = oversampler.sample(X, y)
        args.output_folder = os.path.join(args.input_folder, "svm_merge_models_smote")
    else:
        print("using original data")
        X_samp, y_samp = X, y
        args.output_folder = os.path.join(args.input_folder, "svm_merge_models")

    Path(args.output_folder).mkdir(parents=True, exist_ok=True)

    # for c in [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]:
    for c in [0.0000001, 0.000001, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
    # for c in [0.0001]:
        m = train(y_samp, X_samp, '-c {} -B 0.0'.format(c))
        model_in = os.path.join(args.output_folder, 'libSVM_c{}_b0.model'.format(c))
        model_out = os.path.join(args.output_folder, 'libSVM_c{}_b0.md'.format(c))
        save_model(model_in, m)
        print("saving model {}".format(model_out))
        convert_model_format(model_in, model_out, features=args.features)

    # for c in [0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]:
    # for c in [0.0000001, 0.000001, 0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 10, 100, 1000, 10000, 100000, 1000000, 10000000]:
    #     m = train(y_samp, X_samp, '-c {} -B 1.0'.format(c))
    #     model_in = os.path.join(args.output_folder, 'libSVM_c{}_b1.model'.format(c))
    #     model_out = os.path.join(args.output_folder, 'libSVM_c{}_b1.md'.format(c))
    #     save_model(model_in, m)
    #     print("saving model {}".format(model_out))
    #     convert_model_format(model_in, model_out, features=args.features)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
##########################################################################
# NSAp - Copyright (C) CEA, 2019 - 2020
# Distributed under the terms of the CeCILL-B license, as published by
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
# for details.
##########################################################################

# Third party import
import logging
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as func
from torch.autograd import Variable
from pynet.utils import Losses

# Global parameters
logger = logging.getLogger("pynet")

@Losses.register
class FocalLoss(object):
    """ Define a Focal Loss.

    Loss(pt) = −αt mt (1−pt)γ log(pt)

    where pt is the model's estimated probability for each class.

    When an example is misclassified and pt is small, the modulating factor
    is near 1 and the loss is unaffected. As pt goes to 1, the factor goes to
    0 and the loss for well-classified examples is down-weighted.
    The focusing parameter γ smoothly adjusts the rate at which easy examples
    are down-weighted. When γ= 0, the loss is equivalent to cross entropy, and
    as γ isincreased the effect of the modulating factor is likewise increased.
    For instance, with γ= 2, an example classified with pt= 0.9 would have
    100×lower loss compared with cross entropy and with pt≈0.968 it would have
    1000×lower loss.
    Then we use an α-balanced variant of the focal loss for addressing class
    imbalance with a weighting factor α ∈ [0,1]. In practice α may be set by
    inverse class frequency.

    Reference: https://arxiv.org/abs/1708.02002
    """
    def __init__(self, n_classes, gamma=2, alpha=None, reduction="mean",
                 with_logit=True):
        """ Class instanciation.

        Parameters
        ----------
        n_classes: int
            the number of classes.
        gamma: float, default 2
            the focusing parameter >=0.
        alpha: float or list of float, default None
            if set use alpha-balanced variant of the focal loss.
        reduction: str, default 'mean'
            specifies the reduction to apply to the output: 'none' - no
            reduction will be applied, 'mean' - the sum of the output
            will be divided by the number of elements in the output, 'sum'
            - the output will be summed.
        with_logit: bool, default True
            apply the softmax logit function to the result.
        """
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = reduction
        self.with_logit = with_logit
        self.eps = 1e-9
        alpha = alpha or 1
        if not isinstance(alpha, list):
            alpha = [alpha] * n_classes
        if len(alpha) != n_classes:
            raise ValueError("Invalid alphas size.")
        logger.debug("  alpha: {0}".format(alpha))
        self.alpha = torch.FloatTensor(alpha).view(-1, 1)
        # self.alpha = self.alpha / self.alpha.sum()
        self.debug("alpha", self.alpha)

    def __call__(self, output, target):
        """ Compute the loss.

        Parameters
        ----------
        output: Tensor (N,C,*)
            predicted labels where C is the number of classes.
        target: Tensor (N,*)
            true labels where each value is 0≤target[i]≤C−1.
        """
        logger.debug("Focal loss...")
        self.debug("output", output)
        self.debug("target", target)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape[0] != target.shape[0]:
            raise ValueError("Expected pred & true labels same batch size.")
        if output.shape[2:] != target.shape[1:]:
            raise ValueError("Expected pred & true labels same data size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")

        n_batch, n_classes = output.shape[:2]
        device = output.device
        dim = output.dim()
        logger.debug("  n_batches: {0}".format(n_batch))
        logger.debug("  n_classes: {0}".format(n_classes))
        logger.debug("  dim: {0}".format(dim))
        if self.with_logit:
            output = func.softmax(output, dim=1)
        logit = output + self.eps
        self.debug("logit", logit)

        # Reshape data
        # N,C,d1,d2 -> N,C,m (m=d1*d2*...)
        if dim > 2:
            logit = logit.view(n_batch, n_classes, -1)
            self.debug("logit", logit)
            logit = logit.permute(0, 2, 1).contiguous()
            self.debug("logit", logit)
            logit = logit.view(-1, n_classes)
            self.debug("logit", logit)
        target = torch.squeeze(target, dim=1)
        target = target.view(-1, 1)
        self.debug("target", target)

        # Create the labels one hot encoded tensor
        idx = target.data
        one_hot = torch.zeros(target.size(0), n_classes,
                              device=device, dtype=output.dtype)
        target_one_hot = one_hot.scatter_(1, idx, 1.) + self.eps

        # Compute the focal loss
        if self.alpha.device != device:
            self.alpha = self.alpha.to(device)
        pt = torch.sum(target_one_hot * logit, dim=1)
        self.debug("pt", pt)
        logpt = torch.log(pt)
        weight = torch.pow(1 - pt, self.gamma)
        self.debug("weight", weight)
        alpha = self.alpha[idx]
        alpha = torch.squeeze(alpha)
        self.debug("alpha", alpha)
        loss = -1 * alpha * weight * logpt
        self.debug("loss", loss)

        # Reduction
        if self.reduction == "none":
            pass
        elif self.reduction == "mean":
            loss = torch.mean(loss) / self.alpha[target].mean()
        elif self.reduction == "sum":
            loss = torch.sum(loss)
        else:
            raise NotImplementedError("Invalid reduction mode.")
        logger.debug("  loss: {0}".format(loss))

        return loss

    def _forward_without_resizing(self, output, target):
        """ Compute the loss.

        Parameters
        ----------
        output: Tensor (N,C,*)
            predicted labels where C is the number of classes.
        target: Tensor (N,*)
            true labels where each value is 0≤target[i]≤C−1.
        """
        logger.debug("Focal loss...")
        self.debug("output", output)
        self.debug("target", target)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape[0] != target.shape[0]:
            raise ValueError("Expected pred & true labels same batch size.")
        if output.shape[2:] != target.shape[1:]:
            raise ValueError("Expected pred & true labels same data size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")

        n_batch, n_classes = output.shape[:2]
        device = output.device
        dim = output.dim()
        logger.debug("  n_batches: {0}".format(n_batch))
        logger.debug("  n_classes: {0}".format(n_classes))
        logger.debug("  dim: {0}".format(dim))
        if self.with_logit:
            output = func.softmax(output, dim=1)
        logit = output + self.eps
        self.debug("logit", logit)

        # Create the labels one hot encoded tensor
        one_hot = torch.zeros(n_batch, n_classes, *target.shape[1:],
                              device=device, dtype=output.dtype)
        target_one_hot = one_hot.scatter_(
            1, target.unsqueeze(1), 1.) + self.eps

        # Compute the focal loss
        if self.alpha.device != device:
            self.alpha = self.alpha.to(device)
        weight = torch.pow(1 - logit, self.gamma)
        self.debug("weight", weight)
        shape = [1, n_classes] + [1] * len(target.shape[1:])
        alpha = self.alpha.view(*shape)
        alpha = alpha.expand_as(weight)
        self.debug("alpha", alpha)
        focal = -1 * alpha * weight * torch.log(logit)
        self.debug("focal", focal)
        loss = torch.sum(target_one_hot * focal, dim=1)
        self.debug("loss", loss)

        # Reduction
        if self.reduction == "none":
            pass
        elif self.reduction == "mean":
            loss = torch.mean(loss) / self.alpha[target].mean()
        elif self.reduction == "sum":
            loss = torch.sum(loss)
        else:
            raise NotImplementedError("Invalid reduction mode.")
        logger.debug("  loss: {0}".format(loss))

        return loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class MaskLoss(object):
    """ Define a Masked Loss.

    Loss(pt) = −αt mt log(pt)

    where pt is the model's estimated probability for each class.
    """
    def __init__(self, n_classes, beta=0.2, alpha=None, reduction="mean",
                 with_logit=True):
        """ Class instanciation.

        Parameters
        ----------
        n_classes: int
            the number of classes.
        beta: float, default 0.2
            the minimum value in the mask.
        alpha: float or list of float, default None
            if set use alpha-balanced variant of the focal loss.
        reduction: str, default 'mean'
            specifies the reduction to apply to the output: 'none' - no
            reduction will be applied, 'mean' - the sum of the output
            will be divided by the number of elements in the output, 'sum'
            - the output will be summed.
        with_logit: bool, default True
            apply the log softmax logit function to the result.
        """
        self.beta = beta
        self.alpha = alpha
        self.reduction = reduction
        self.with_logit = with_logit
        self.eps = 1e-9
        alpha = alpha or 1
        if not isinstance(alpha, list):
            alpha = [alpha] * n_classes
        if len(alpha) != n_classes:
            raise ValueError("Invalid alphas size.")
        logger.debug("  alpha: {0}".format(alpha))
        self.alpha = torch.FloatTensor(alpha)
        self.debug("alpha", self.alpha)

    def __call__(self, output, target, mask):
        """ Compute the loss.

        Parameters
        ----------
        output: Tensor (N,C,*)
            predicted labels where C is the number of classes.
        target: Tensor (N,*)
            true labels where each value is 0≤ target[i] ≤C−1.
        mask: Tensor (N,*)
            the binary mask used to mask the loss.
        """
        logger.debug("Maked loss...")
        self.debug("output", output)
        self.debug("target", target)
        self.debug("mask", mask)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape[0] != target.shape[0]:
            raise ValueError("Expected pred & true labels same batch size.")
        if output.shape[2:] != target.shape[1:]:
            raise ValueError("Expected pred & true labels same data size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")
        if mask is not None and output.shape[0] != mask.shape[0]:
            raise ValueError("Expected pred & mask same batch size.")
        if mask is not None and output.shape[2:] != mask.shape[1:]:
            raise ValueError("Expected pred & mask same data size.")
        if mask is not None and output.device != mask.device:
            raise ValueError("Pred & mask must be in the same device.")

        n_batch, n_classes = output.shape[:2]
        device = output.device
        logger.debug("  n_batches: {0}".format(n_batch))
        logger.debug("  n_classes: {0}".format(n_classes))

        if self.alpha.device != device:
            self.alpha = self.alpha.to(device)
        if self.with_logit:
            output = func.log_softmax(output, dim=1)
        logit = output + self.eps
        self.debug("logit", logit)

        # Compute the focal loss
        mask[mask <= self.beta] = self.beta
        loss = func.nll_loss(logit, target, weight=self.alpha,
                             reduction="none")
        loss = loss * mask
        self.debug("loss", loss)

        # Reduction
        if self.reduction == "none":
            pass
        elif self.reduction == "mean":
            loss = torch.mean(loss) / self.alpha[target].mean()
        elif self.reduction == "sum":
            loss = torch.sum(loss)
        else:
            raise NotImplementedError("Invalid reduction mode.")
        logger.debug("  loss: {0}".format(loss))

        return loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class SoftDiceLoss(object):
    """ Define a multi class Dice Loss.

    Dice = (2 intersec Y) / (X + Y)

    Note that PyTorch optimizers minimize a loss. In this case, we would like
    to maximize the dice loss so we return 1 - Dice.
    """
    def __init__(self, with_logit=True, reduction="mean"):
        """ Class instanciation.

        Parameters
        ----------
        with_logit: bool, default True
            apply the softmax logit function to the result.
        reduction: str, default 'mean'
            specifies the reduction to apply to the output: 'none' - no
            reduction will be applied, 'mean' - the sum of the output
            will be divided by the number of elements in the output, 'sum'
            - the output will be summed.
        """
        self.with_logit = with_logit
        self.reduction = reduction
        self.smooth = 1e-6
        self.eps = 1e-6

    def __call__(self, output, target):
        """ Compute the loss.

        Parameters
        ----------
        output: Tensor (N,C,*)
            predicted labels where C is the number of classes.
        target: Tensor (N,*)
            true labels where each value is 0≤targets[i]≤C−1.
        """
        logger.debug("Dice loss...")
        self.debug("output", output)
        self.debug("target", target)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape[0] != target.shape[0]:
            raise ValueError("Expected pred & true labels same batch size.")
        if output.shape[2:] != target.shape[1:]:
            raise ValueError("Expected pred & true labels same data size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")

        n_batch, n_classes = output.shape[:2]
        device = output.device
        if self.with_logit:
            prob = func.softmax(output, dim=1)
        else:
            prob = output
        self.debug("logit", prob)

        # Create the labels one hot encoded tensor
        prob = prob.view(n_batch, -1)
        dims = list(range(len(target.shape)))
        dims.insert(1, len(target.shape))
        dims = tuple(dims)
        logger.debug("permute {0}".format(dims))
        target_one_hot = func.one_hot(target, num_classes=n_classes)
        self.debug("target_one_hot", target_one_hot)
        target_one_hot = target_one_hot.permute(dims)
        target_one_hot = target_one_hot.contiguous().view(n_batch, -1)
        if target_one_hot.device != device:
            target_one_hot = target_one_hot.to(device)
        self.debug("target_one_hot", target_one_hot)

        # Compute the dice score
        intersection = prob * target_one_hot
        self.debug("intersection", intersection)
        dice_score = (2 * intersection.sum(dim=1) + self.smooth) / (
            target_one_hot.sum(dim=1) + prob.sum(dim=1) + self.smooth)
        loss = 1. - dice_score
        self.debug("loss", loss)

        # Reduction
        if self.reduction == "none":
            pass
        elif self.reduction == "mean":
            loss = torch.mean(loss)
        elif self.reduction == "sum":
            loss = torch.sum(loss)
        else:
            raise NotImplementedError("Invalid reduction mode.")
        logger.debug("  loss: {0}".format(loss))

        return loss

    def _forward_without_resizing(self, output, target):
        """ Compute the loss.

        Parameters
        ----------
        output: Tensor (N,C,*)
            predicted labels where C is the number of classes.
        target: Tensor (N,*)
            true labels where each value is 0≤targets[i]≤C−1.
        """
        logger.debug("Dice loss...")
        self.debug("output", output)
        self.debug("target", target)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape[0] != target.shape[0]:
            raise ValueError("Expected pred & true labels same batch size.")
        if output.shape[2:] != target.shape[1:]:
            raise ValueError("Expected pred & true labels same data size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")

        n_batch, n_classes = output.shape[:2]
        device = output.device
        if self.with_logit:
            prob = func.softmax(output, dim=1)
        else:
            prob = output
        self.debug("logit", prob)

        # Create the labels one hot encoded tensor
        one_hot = torch.zeros(n_batch, n_classes, *target.shape[1:],
                              device=device, dtype=output.dtype)
        target_one_hot = one_hot.scatter_(1, target.unsqueeze(1), 1.)
        self.debug("one hot", target_one_hot)

        # Compute the dice score
        dims = tuple(range(1, len(target.shape) + 1))
        intersection = torch.sum(prob * target_one_hot, dims)
        self.debug("intersection", intersection)
        cardinality = torch.sum(prob + target_one_hot, dims)
        self.debug("cardinality", cardinality)
        dice_score = 2. * intersection / (cardinality + self.eps)
        loss = 1. - dice_score
        self.debug("loss", loss)

        # Reduction
        if self.reduction == "none":
            pass
        elif self.reduction == "mean":
            loss = torch.mean(loss)
        elif self.reduction == "sum":
            loss = torch.sum(loss)
        else:
            raise NotImplementedError("Invalid reduction mode.")
        logger.debug("  loss: {0}".format(loss))

        return loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class CustomKLLoss(object):
    """ KL Loss.
    """
    def __call__(self, mean, std):
        return (torch.mean(torch.mul(mean, mean)) +
                torch.mean(torch.mul(std, std)) -
                torch.mean(torch.log(torch.mul(std, std))) - 1)

@Losses.register
class NvNetCombinedLoss(object):
    """ Combined Loss.

    Cross Entropy loss + k1 * L2 loss + k2 * KL loss
    Since the output of the segmentation decoder has N channels (prediction
    for each tumor subregion), we simply add the N dice loss functions.
    A hyper-parameter weight of k1=0.1, k2=0.1 was found empirically in the
    paper.
    """
    def __init__(self, num_classes, k1=0.1, k2=0.1):
        super(NvNetCombinedLoss, self).__init__()
        self.layer_outputs = None
        self.num_classes = num_classes
        self.k1 = k1
        self.k2 = k2
        self.ce_loss = nn.CrossEntropyLoss(reduction="mean")
        self.l2_loss = nn.MSELoss(reduction="mean")
        self.kl_loss = CustomKLLoss()

    def __call__(self, output, target):
        logger.debug("NvNet Combined Loss...")
        self.debug("output", output)
        self.debug("target", target)
        if self.layer_outputs is not None:
            y_mid = self.layer_outputs
            self.debug("y_mid", y_mid)
        if len(output.shape) < 2:
            raise ValueError("Invalid labels shape {0}.".format(output.shape))
        if output.shape != target.shape:
            raise ValueError("Expected pred & true of same size.")
        if output.device != target.device:
            raise ValueError("Pred & true labels must be in the same device.")
        if self.layer_outputs is not None and y_mid.shape[-1] != 256:
            raise ValueError("128 means & stds expected.")

        device = output.device
        if self.layer_outputs is not None:
            est_mean, est_std = (y_mid[:, :128], y_mid[:, 128:])
            self.debug("est_mean", est_mean)
            self.debug("est_std", est_std)
            vae_pred = output[:, self.num_classes:]
            vae_truth = target[:, self.num_classes:]
            self.debug("vae_pred", vae_pred)
            self.debug("vae_truth", vae_truth)
        seg_pred = output[:, :self.num_classes]
        seg_truth = target[:, :self.num_classes]
        self.debug("seg_pred", seg_pred)
        self.debug("seg_truth", seg_truth)
        seg_truth = torch.argmax(seg_truth, dim=1).type(torch.LongTensor)
        if seg_truth.device != device:
            seg_truth = seg_truth.to(device)
        self.debug("seg_truth", seg_truth)

        ce_loss = self.ce_loss(seg_pred, seg_truth)
        if self.layer_outputs is not None:
            l2_loss = self.l2_loss(vae_pred, vae_truth)
            kl_div = self.kl_loss(est_mean, est_std)
            combined_loss = ce_loss + self.k1 * l2_loss + self.k2 * kl_div
        else:
            l2_loss, kl_div = (None, None)
            combined_loss = ce_loss
        logger.debug(
            "ce_loss: {0}, L2_loss: {1}, KL_div: {2}, combined_loss: "
            "{3}".format(ce_loss, l2_loss, kl_div, combined_loss))
        return combined_loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class MSELoss(object):
    """ Calculate the Mean Square Error loss between I and J.
    """
    def __init__(self, concat=False):
        """ Init class.

        Parameters
        ----------
        concat: bool, default False
            if set asssume that the target image J is a concatenation of the
            moving and fixed.
        """
        super(MSELoss, self).__init__()
        self.concat = concat

    def __call__(self, arr_i, arr_j):
        """ Forward method.

        Parameters
        ----------
        arr_i, arr_j: Tensor (batch_size, channels, *vol_shape)
            the input data.
        """
        logger.debug("Compute MSE loss...")
        if self.concat:
            nb_channels = arr_j.shape[1] // 2
            arr_j = arr_j[:, nb_channels:]
        self.debug("I", arr_i)
        self.debug("J", arr_j)
        loss = torch.mean((arr_i - arr_j) ** 2)
        logger.debug("  loss: {0}".format(loss))
        logger.debug("Done.")
        return loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class PCCLoss(object):
    """ Calculate the Pearson correlation coefficient between I and J.
    """
    def __init__(self, concat=False):
        """ Init class.

        Parameters
        ----------
        concat: bool, default False
            if set asssume that the target image J is a concatenation of the
            moving and fixed.
        """
        super(PCCLoss, self).__init__()
        self.concat = concat

    def __call__(self, arr_i, arr_j):
        """ Forward method.

        Parameters
        ----------
        arr_i, arr_j: Tensor (batch_size, channels, *vol_shape)
            the input data.
        """
        logger.debug("Compute PCC loss...")
        nb_channels = arr_j.shape[1]
        if self.concat:
            nb_channels = arr_j.shape[1] // 2
            arr_j = arr_j[:, nb_channels:]
        logger.debug("  channels: {0}".format(nb_channels))
        self.debug("I", arr_i)
        self.debug("J", arr_j)
        centered_arr_i = arr_i - torch.mean(arr_i)
        centered_arr_j = arr_j - torch.mean(arr_j)
        pearson_loss = torch.sum(
            centered_arr_i * centered_arr_j) / (
                torch.sqrt(torch.sum(centered_arr_i ** 2) + 1e-6) *
                torch.sqrt(torch.sum(centered_arr_j ** 2) + 1e-6))
        loss = 1. - pearson_loss
        logger.debug("  loss: {0}".format(loss))
        logger.debug("Done.")
        return loss

    def debug(self, name, tensor):
        """ Print debug message.

        Parameters
        ----------
        name: str
            the tensor name in the displayed message.
        tensor: Tensor
            a pytorch tensor.
        """
        logger.debug("  {3}: {0} - {1} - {2}".format(
            tensor.shape, tensor.get_device(), tensor.dtype, name))

@Losses.register
class NCCLoss(object):
    """ Calculate the normalize cross correlation between I and J.
    """
    def __init__(self, concat=False, win=None):
        """ Init class.

        Parameters
        ----------
        concat: bool, default False
            if set asssume that the target image J is a concatenation of the
            moving and fixed.
        win: list of in, default None
            the window size to compute the correlation, default 9.
        """
        super(NCCLoss, self).__init__()
        self.concat = concat
        self.win = win

    def __call__(self, arr_i, arr_j):
        """ Forward method.

        Parameters
        ----------
        arr_i, arr_j: Tensor (batch_size, channels, *vol_shape)
            the input data.
        """
        logger.debug("Compute NCC loss...")
        if self.concat:
            nb_channels = arr_j.shape[1] // 2
            arr_j = arr_j[:, nb_channels:]
        ndims = len(list(arr_i.size())) - 2
        if ndims not in [1, 2, 3]:
            raise ValueError("Volumes should be 1 to 3 dimensions, not "
                             "{0}.".format(ndims))
        if self.win is None:
            self.win = [9] * ndims
        device = arr_i.device
        sum_filt = torch.ones([1, 1, *self.win]).to(device)
        pad_no = math.floor(self.win[0] / 2)
        stride = tuple([1] * ndims)
        padding = tuple([pad_no] * ndims)
        logger.debug("  ndims: {0}".format(ndims))
        logger.debug("  stride: {0}".format(stride))
        logger.debug("  padding: {0}".format(padding))
        logger.debug("  filt: {0} - {1}".format(
            sum_filt.shape, sum_filt.get_device()))
        logger.debug("  win: {0}".format(self.win))
        logger.debug("  I: {0} - {1} - {2}".format(
            arr_i.shape, arr_i.get_device(), arr_i.dtype))
        logger.debug("  J: {0} - {1} - {2}".format(
            arr_j.shape, arr_j.get_device(), arr_j.dtype))

        var_arr_i, var_arr_j, cross = self._compute_local_sums(
            arr_i, arr_j, sum_filt, stride, padding)
        cc = cross * cross / (var_arr_i * var_arr_j + 1e-5)
        loss = -1 * torch.mean(cc)
        logger.debug("  loss: {0}".format(loss))
        logger.info("Done.")

        return loss

    def _compute_local_sums(self, arr_i, arr_j, filt, stride, padding):
        conv_fn = getattr(func, "conv{0}d".format(len(self.win)))
        logger.debug("  conv: {0}".format(conv_fn))

        arr_i2 = arr_i * arr_i
        arr_j2 = arr_j * arr_j
        arr_ij = arr_i * arr_j

        sum_arr_i = conv_fn(arr_i, filt, stride=stride, padding=padding)
        sum_arr_j = conv_fn(arr_j, filt, stride=stride, padding=padding)
        sum_arr_i2 = conv_fn(arr_i2, filt, stride=stride, padding=padding)
        sum_arr_j2 = conv_fn(arr_j2, filt, stride=stride, padding=padding)
        sum_arr_ij = conv_fn(arr_ij, filt, stride=stride, padding=padding)

        win_size = np.prod(self.win)
        logger.debug("  win size: {0}".format(win_size))
        u_arr_i = sum_arr_i / win_size
        u_arr_j = sum_arr_j / win_size

        cross = (sum_arr_ij - u_arr_j * sum_arr_i - u_arr_i * sum_arr_j +
                 u_arr_i * u_arr_j * win_size)
        var_arr_i = (sum_arr_i2 - 2 * u_arr_i * sum_arr_i + u_arr_i *
                     u_arr_i * win_size)
        var_arr_j = (sum_arr_j2 - 2 * u_arr_j * sum_arr_j + u_arr_j *
                     u_arr_j * win_size)

        return var_arr_i, var_arr_j, cross

@Losses.register
class RCNetLoss(object):
    """ RCNet Loss function.

    This loss needs intermediate layers outputs.
    Use a callback function to set the 'layer_outputs' class parameter before
    each evaluation of the loss function.
    If you use an interface this parameter is updated automatically?

    PCCLoss
    """
    def __init__(self):
        self.similarity_loss = PCCLoss(concat=True)
        self.layer_outputs = None

    def __call__(self, moving, fixed):
        logger.debug("Compute RCNet loss...")
        if self.layer_outputs is None:
            raise ValueError(
                "This loss needs intermediate layers outputs. Please register "
                "an appropriate callback.")
        stem_results = self.layer_outputs["stem_results"]
        for stem_result in stem_results:
            params = stem_result["stem_params"]
            if params["raw_weight"] > 0:
                stem_result["raw_loss"] = self.similarity_loss(
                    stem_result["warped"], fixed) * params["raw_weight"]
        loss = sum([
            stem_result["raw_loss"] * stem_result["stem_params"]["weight"]
            for stem_result in stem_results if "raw_loss" in stem_result])
        self.layer_outputs = None
        logger.debug("  loss: {0}".format(loss))
        logger.debug("Done.")
        return loss

@Losses.register
class VMILoss(object):
    """ Variational Mutual information loss function.

    Reference: http://bayesiandeeplearning.org/2018/papers/136.pdf -
               https://discuss.pytorch.org/t/help-with-histogram-and-loss-
               backward/44052/5
    """
    def get_positive_expectation(self, p_samples, average=True):
        log_2 = math.log(2.)
        Ep = log_2 - F.softplus(-p_samples)
        # Note JSD will be shifted
        if average:
            return Ep.mean()
        else:
            return Ep

    def get_negative_expectation(self, q_samples, average=True):
        log_2 = math.log(2.)
        Eq = F.softplus(-q_samples) + q_samples - log_2
        # Note JSD will be shifted
        if average:
            return Eq.mean()
        else:
            return Eq

    def __call__(self, lmap, gmap):
        """ The fenchel_dual_loss from the DIM code
        Reshape tensors dims to (N, Channels, chunks).

        Parameters
        ----------
        lmap: Tensor
            the moving data.
        gmap: Tensor
            the fixed data.
        """
        lmap = lmap.reshape(2, 128, -1)
        gmap = gmap.squeeze()

        N, units, n_locals = lmap.size()
        n_multis = gmap.size(2)

        # First we make the input tensors the right shape.
        l = lmap.view(N, units, n_locals)
        l = lmap.permute(0, 2, 1)
        l = lmap.reshape(-1, units)

        m = gmap.view(N, units, n_multis)
        m = gmap.permute(0, 2, 1)
        m = gmap.reshape(-1, units)

        u = torch.mm(m, l.t())
        u = u.reshape(N, n_multis, N, n_locals).permute(0, 2, 3, 1)

        mask = torch.eye(N).to(l.device)
        n_mask = 1 - mask

        E_pos = get_positive_expectation(u, average=False).mean(2).mean(2)
        E_neg = get_negative_expectation(u, average=False).mean(2).mean(2)

        E_pos = (E_pos * mask).sum() / mask.sum()
        E_neg = (E_neg * n_mask).sum() / n_mask.sum()
        loss = E_neg - E_pos

        return loss


import tkinter as tk
from PIL import Image,ImageTk
from random import randint
import time

MOVE_INCREMENT = 20
moves_per_second = 12
GAME_SPEED = 1000 // moves_per_second

class Snake(tk.Canvas):
    def __init__(self):
        super().__init__(width=600, height=620, background='black', highlightthickness=0)

        self.snake_positions = [(100, 100),(80, 100),(60, 100)]
        self.food_positions = self.set_new_food_position()
        self.score=0
        self.direction = "Right"
        self.bind_all("<Key>", self.on_key_press)

        self.load_assests()
        self.create_object()

        self.after(50, self.perform_action )

    def load_assests(self):
        try:                   #try because if this fails it wont work
            self.snake_body_image=Image.open('C:/Users/INTEL/PycharmProjects/pythonProject1/iig security/img_buttons/snake.png')
            self.snake_body=ImageTk.PhotoImage(self.snake_body_image )

            self.food_body_image = Image.open('C:/Users/INTEL/PycharmProjects/pythonProject1/iig security/img_buttons/food111.png ')
            self.food_body = ImageTk.PhotoImage(self.food_body_image)
        except IOError as error:
            print(error)
            root.destroy()

    def create_object(self):
        self.create_text(
            100 , 12, text=f"score: {self.score} (speed: {moves_per_second })", tag="score", fill='white',font=('TkDefaultFont',15)
        )
        for x_position, y_position in self.snake_positions:
            self.create_image(x_position, y_position, image=self.snake_body,tag="snake")

        self.create_image(*self.food_positions,image=self.food_body, tag="food")
        self.create_rectangle(7,27,595,613,outline='white')

    def move_snake(self):
        head_x_position, head_y_position = self.snake_positions [0]
        new_head_position = (head_x_position + MOVE_INCREMENT, head_y_position)

        if self.direction == "Left":
            new_head_position = (head_x_position - MOVE_INCREMENT, head_y_position  )
        elif self.direction == "Right":
            new_head_position = (head_x_position + MOVE_INCREMENT, head_y_position  )
        elif self.direction == "Down":
            new_head_position = (head_x_position, head_y_position + MOVE_INCREMENT)
        elif self.direction == "Up":
            new_head_position = (head_x_position,head_y_position - MOVE_INCREMENT )

        #new_head_position = (head_x_position + MOVE_INCREMENT,head_y_position)

        self.snake_positions = [new_head_position ] + self.snake_positions [:-1]

        for segment, position in zip(self.find_withtag("snake"), self.snake_positions):
            self.coords(segment, position)

    def perform_action(self):
        if self.check_collisions() :
            self.end_game()
            return

        self.check_food_collision()
        self.move_snake()
        self.after(GAME_SPEED,self.perform_action)

    def check_collisions(self):
        head_x_position, head_y_position = self.snake_positions[0]

        if head_x_position in (0, 600) or head_y_position in (20, 620):
            return True
        elif (head_x_position, head_y_position) in self.snake_positions[1:]:
            return True

    def on_key_press(self,e):
        new_direction = e.keysym
        all_directions=("Up","Down","Left","Right")
        opposites = ({"Up","Down"},{"Left","Right"})

        if (
            new_direction in all_directions
            and{new_direction, self.direction  } not in opposites
        ):
            self.direction = new_direction

    def check_food_collision(self):
        if self.snake_positions[0] == self.food_positions:
            self.score += 5
            self.snake_positions.append(self.snake_positions[-1])

            if self.score % 5 == 0:
                global moves_per_second
                moves_per_second += 1

            self.create_image(
                *self.snake_positions[-1],image=self.snake_body,tag="snake"
            )

            self.food_positions = self.set_new_food_position()
            self.coords(self.find_withtag("food"),*self.food_positions)

            score = self.find_withtag("score")
            self.itemconfigure(score,text=f"Score: {self.score} (speed: {moves_per_second })",tag="score")

    def set_new_food_position(self):
        while True :
            x_position = randint(1,29)  * MOVE_INCREMENT
            y_position = randint(3,30)  * MOVE_INCREMENT
            food_position = (x_position,y_position  )

            if food_position not in self.snake_positions:
                return food_position

    def end_game(self):
        self.delete(tk.ALL)
        self.create_text(
            self.winfo_width() / 2,
            self.winfo_height() / 2,
            text=f"Game Over ! You Scored {self.score} !",
            fill='white',
            font=('TkDefaultFont',25)
        )

root=tk.Tk()
root.title('image')
root.iconbitmap('C:/Users/INTEL/PycharmProjects/pythonProject1/pics/tk1.3.ico')

board=Snake()
board.pack()

root.mainloop()


import requests
import json

class PlanetTerp:
    url = 'https://api.planetterp.com/v1/'

    # Query Parameters
    # Name	    Type	Description
    # name	    string	Required. Show the given course.
    # reviews	boolean	Optional. Show reviews for the course (reviews for professors that taught the course and have this course listed as the one being reviewed). Default: false
    def course(self, name, reviews = False):
        request_string = self.url + 'course?name=' + name.replace(" ", "%20")
        if reviews:
            request_string += '&reviews=true'

        return requests.get(request_string).json()

    # Query Parameters
    # Name	        Type	Description
    # department	string	Optional. Only get courses in a department. Must be four characters. Default: all departments
    # reviews	    boolean	Optional. Show reviews for the course (reviews for professors that taught the course and have this course listed as the one being reviewed). Default: false
    # limit	        integer	Optional. Maximum number of records to return. Must be between 1 and 1000. Default: 100
    # offset	    integer	Optional. Number of records to skip for pagination. Default: 0
    def all_courses(self, department = None, reviews = False, limit = None, offset = None):
        request_string = self.url + 'courses?'
        if department:
            request_string += 'department=' + department + '&'
        if reviews:
            request_string += 'reviews=true' + '&'
        if limit:
            request_string += 'limit=' + str(limit) + '&'
        if offset:
            request_string += 'offset=' + str(offset) + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()

    # Query Parameters
    # Name    Type    Description
    # name    string  Required. Show the given professor.
    # reviews boolean Optional.Show reviews for the professor.Default: false
    def professor(self, name, reviews = False):
        request_string = self.url + 'professor?name=' + name.replace(" ", "%20")
        if reviews:
            request_string += '&reviews=true'

        return requests.get(request_string).json()

    # Query Parameters
    # Name	    Type	Description
    # type	    string	Optional. Show only reviews for professors or teaching assistants. Default: show both. Options: professor, ta
    # reviews	boolean	Optional. Show reviews for the professors. Default: false
    # limit	    integer	Optional. Maximum number of records to return. Must be between 1 and 1000. Default: 100
    # offset	integer	Optional. Number of records to skip for pagination. Default: 0
    def all_professors(self, type_ = None, reviews = False, limit = None, offset = None):
        request_string = self.url + 'professors?'
        if type_:
            request_string += 'type=' + type_ + '&'
        if reviews:
            request_string += 'reviews=true' + '&'
        if limit:
            request_string += 'limit=' + str(limit) + '&'
        if offset:
            request_string += 'offset=' + str(offset) + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()

    # Query Parameters
    # Name	    Type	Description
    # course	string	Optional. Show only grades for the given course.
    # professor	string	Optional. Show only grades for the given professor.
    # semester	string	Optional. Show only grades for the given semester. Semester should be provided as the year followed by the semester code. 01 means Spring and 08 means Fall. For example, 202001 means Spring 2020. Default: all semesters
    # section	string	Optional. Show only grades for the given section. Default: all sections
    def grades(self, course = None, professor = None, semester = None, section = None):
        request_string = self.url + 'grades?'
        if not course and not professor:
            print("Error! Must provide either a course or professor as input")
            return None
        if course:
            request_string += 'course=' + course + '&'
        if professor:
            request_string += 'professor=' + professor.replace(" ", "%20") + '&'
        if semester:
            request_string += 'semester=' + semester + '&'
        if section:
            request_string += 'section=' + section + '&'

        # We ignore the last character as it will be an extra '&'
        request_url = request_string[:-1]
        return requests.get(request_url).json()


from flaml import AutoML
from sklearn.datasets import load_boston
import os
import unittest
import logging
import tempfile
import io

class TestLogging(unittest.TestCase):

    def test_logging_level(self):

        from flaml import logger, logger_formatter

        with tempfile.TemporaryDirectory() as d:

            training_log = os.path.join(d, "training.log")

            # Configure logging for the FLAML logger
            # and add a handler that outputs to a buffer.
            logger.setLevel(logging.INFO)
            buf = io.StringIO()
            ch = logging.StreamHandler(buf)
            ch.setFormatter(logger_formatter)
            logger.addHandler(ch)

            # Run a simple job.
            automl = AutoML()
            automl_settings = {
                "time_budget": 1,
                "metric": 'mse',
                "task": 'regression',
                "log_file_name": training_log,
                "log_training_metric": True,
                "n_jobs": 1,
                "model_history": True,
            }
            X_train, y_train = load_boston(return_X_y=True)
            n = len(y_train) >> 1
            automl.fit(X_train=X_train[:n], y_train=y_train[:n],
                       X_val=X_train[n:], y_val=y_train[n:],
                       **automl_settings)

            # Check if the log buffer is populated.
            self.assertTrue(len(buf.getvalue()) > 0)

        import pickle
        with open('automl.pkl', 'wb') as f:
            pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL)
        print(automl.__version__)


#!/usr/bin/env python
import numpy as np
from mpmath import fp,mp
from scipy.integrate import quad,fixed_quad, simps, dblquad
from tools.config import conf
import obslib.idis.aux
#from obslib.lh.theory import LH

class STFUNCS(obslib.idis.aux.AUX):
  '''
  DIS structure functions for pion. It is a simplified
  copy from the nucleon DIS stfuncs. The main change is
  the call to pion pdfs.
  '''

  def __init__(self):

    self.mellin=conf['mellin-pion']
    #self.mellin=conf['mellin']
    if conf['order']=='LO': self.order=0
    if conf['order']=='NLO': self.order=1
    self.setup()

  # twist level unpolarized structure functions in Mellin space

  def get_T2CFX(self,stf,nucleon,Q2,evolve=True):
    """
    CF(2,L,3) = F2/x,FL/x,F3
    """
    if evolve: conf['pdf-pion'].evolve(Q2)
    #if evolve: conf['pdf'].evolve(Q2)

    g =np.copy(conf['pdf-pion'].storage[Q2]['g'])     # gluon[N] were N mellin contour
    #g =conf['pdf'].storage[Q2]['g']     # gluon[N] were N mellin contour
    Nf=conf['alphaS'].get_Nf(Q2) # num of active flav
    a=conf['alphaS'].get_a(Q2)   # a=alphaS/4pi

    if stf=='F2':
      CQ = self.C2Q[0] + a*self.order*self.C2Q[1]  # hard kernels
      CG = self.C2G[0] + a*self.order*self.C2G[1]
      q=np.copy(conf['pdf-pion'].storage[Q2]['qp'])    # (q + qb)[N]
      #q=np.copy(conf['pdf'].storage[Q2]['qp'])    # (q + qb)[N]
      aX='ap'

    elif stf=='FL':
      CQ = a*self.order*self.CLQ[1]
      CG = a*self.order*self.CLG[1]
      q=np.copy(conf['pdf-pion'].storage[Q2]['qp'])    # (q + qb)[N]
      aX='ap'

    elif stf=='F3':
      CQ = self.C3Q[0] + a*self.order*self.C3Q[1]
      CG = 0
      q=np.copy(conf['pdf-pion'].storage[Q2]['qm'])    # (q - qb)[N]
      aX='am'

    if nucleon=="neutron":
      qup=np.copy(q[1])
      qdn=np.copy(q[2])
      q[1]=qdn
      q[2]=qup

    FX  = np.zeros(self.mellin.N.size,dtype=complex) # initialize FX[N] array

    for i in range(1,Nf+1):
      aXval = self.get_aX(aX,i,Q2)
      FX+=aXval*(CQ*q[i] + 2*CG*g)

    return FX

  # unpolarized functions

  def get_FXN(self,x,Q2,stf='F2',twist=2,nucleon='proton',tmc=True,precalc=False,evolve=True):

    if   stf=='F2': FX= x*self.get_T2CFX('F2',nucleon,Q2,evolve)
    elif stf=='FL': FX= x*self.get_T2CFX('FL',nucleon,Q2,evolve)
    elif stf=='F3': FX=   self.get_T2CFX('F3',nucleon,Q2,evolve)
    FX*=x**(-self.mellin.N)

    return self.mellin.invert(1,FX) # here x=1 since the factor x**(-N) is included in the moments

# we define global parameters here
# for easy connection with parman.py

#L_p2pin=0.68  # UV regulator

#initial=True  # use this flag to cp the params to conf for the initial eval
              # see parman.set_p_to_pi_n_params to see how it works

#F={}          # dictionary to store f(y) values

class LN:

  def __init__(self):
    aux=conf['aux']

    mN     = aux.M
    mN2    = aux.M2
    mPi2   = aux.Mpi2
    mDel   = aux.Mdelta
    mDel2  = aux.Mdelta2
    Mbar2  = (mDel+mN)**2
    Del2   = (mDel-mN)**2
    theta  = lambda x: 0.5*(np.sign(x)+1)
    self.mN2=mN2
    self.mDel2=mDel2

    #g_PiNN = np.sqrt(14.5*4*np.pi)
    g_PiNN = np.sqrt(13.7*4*np.pi)
    gg     = g_PiNN**2/(16*np.pi**2)
    sPiN   = lambda kT2,y:(kT2+mPi2)/y+(kT2+mN2)/(1-y)
    pointN = lambda y,t,kT2: 2*gg*y/(1-y)*(-t)/(t-mPi2)**2

    g_PiND = 11.8  # in units of 1/GeV
    C      = g_PiND**2/(16*np.pi**2 * 18*mDel2)
    sPiD   = lambda kT2,y: (kT2+mPi2)/y + (kT2+mDel2)/(1-y)
    pointD = lambda y,t,kT2: -2*C*y/(1-y)*(Mbar2-mPi2)\
                             * ( (Mbar2-mPi2)*(Del2-mPi2)-(3*(Del2-mPi2)+4*mN*mDel)*(t-mPi2) )\
                             / (t-mPi2)**2

    self.L_p2pin=0.68

    self.model=conf['SPLFUNC model']
    if   self.model=='kT2 cutoff':   self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) #* theta(L_p2pin**2-kT2)
    elif self.model=='IMF exp':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * np.exp(2*(mN2-sPiN(kT2,y))/self.L_p2pin**2)
    elif self.model=='cov exp':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='cov mon':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**2
    elif self.model=='cov dip':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**4
    elif self.model=='Pauli-Villars':self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * (1 - (t-mPi2)**2/(t-self.L_p2pin**2)**2)
    elif self.model=='Regge cov exp':self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * y**(-2*t) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='Bishari':      self.tgrandN = lambda y,t,kT2: pointN(y,t,kT2) * y**(-2*t)

    # factor -2 multiplying "C" from isospin:  f_{pi+ Del0} - f_{pi- Del++} = -2 f_{pi+ Del0} => -2 f_Del^(on)
    if   self.model=='kT2 cutoff':   self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) #* theta(L_p2pin**2-kT2)
    elif self.model=='IMF exp':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * np.exp(2*(mN2-sPiD(kT2,y))/self.L_p2pin**2)
    elif self.model=='cov exp':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='cov mon':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * ((self.L_p2pin**2-mPi2)/(self.L_p2pin**2-t))**2
    elif self.model=='Pauli-Villars':self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * (1 - (t-mPi2)**2/(t-self.L_p2pin**2)**2)
    elif self.model=='Regge cov exp':self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * y**(-2*t)*np.exp(2*(t-mPi2)/self.L_p2pin**2)
    elif self.model=='Bishari':      self.tgrandD = lambda y,t,kT2: pointD(y,t,kT2) * y**(-2*t)

    self.N=1
    self.a0=1
    self.a1=1
    LQCD2=0.4**2
    Q20=1.0
    self.eta=lambda Q2: np.log(np.log(Q2/LQCD2)/np.log(Q20/LQCD2))

    # storage for F2p
    self.storage={}

  def integrand(self,y,kT2):
    tN = -kT2/(1-y)-y**2*self.mN2/(1-y)
    tD = -kT2/(1-y)+y*self.mN2-y*self.mDel2/(1-y)
    return self.tgrandN(y,tN,kT2)+self.tgrandD(y,tD,kT2)

  def integrandN(self,y,kT2):
    tN = -kT2/(1-y)-y**2*self.mN2/(1-y)
    return self.tgrandN(y,tN,kT2)

  def integrandD(self,y,kT2):
    tD = -kT2/(1-y)+y*self.mN2-y*self.mDel2/(1-y)
    return self.tgrandD(y,tD,kT2)

  def get_f(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrand(y,kT2),0,kT2max)[0]

  def get_fN(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrandN(y,kT2),0,kT2max)[0]

  def get_fD(self,y,kT2max):
    if self.model=='kT2 cutoff': kT2max=np.amin([self.L_p2pin**2,kT2max])
    return quad(lambda kT2: self.integrandD(y,kT2),0,kT2max)[0]

  def get_kinfact(self,Q2,x,ye):
      eweak=conf['eweak']
      aEM=eweak.get_alpha(Q2)
      return 4.0*np.pi*aEM**2/x/Q2**2*(1.0-ye+ye**2/2.0)

  def get_dsigdxdQ2dxL(self,x,xpi,y,Q2,kT2max,ye):
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      f=self.get_fN(y,kT2max)
      K=self.get_kinfact(Q2,x,ye)
      return F2pi*f*K

  def get_dsigdxdQ2dxLdt(self,x,Q2,xL,t,s):
      xpi=x/(1-xL)
      if xpi>1: return 0
      ye=Q2/x/s
      if ye>1: return 0
      K=self.get_kinfact(Q2,x,ye)
      kT2=-(t*xL+(1.0-xL)**2*self.mN2)
      kT2max=0.005*xL*135
      if kT2>kT2max: return 0
      f=self.tgrandN(1.0-xL,t,kT2)
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      Jac=xL
      return F2pi*f*K*Jac

  def get_dsigdxdQ2dxLdkT2(self,x,Q2,xL,kT2,s):
      xpi=x/(1.-xL)
      if xpi>1: return 0
      ye=Q2/x/s
      K=self.get_kinfact(Q2,x,ye)
      f=self.integrandN(1.0-xL,kT2)
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      return F2pi*f*K

  def get_dsigdxdQ2dxLKaon(self,x,xpi,y,Q2,kT2max,ye):
      F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
      #f=self.get_fN(y,kT2max)
      fpikT=lambda kT2: self.integrandN(y,kT2)
      #lh=LH()
      lh=conf['lh']
      xL=1.0-np.array(y)
      fKkT=lambda kT2: lh.get_fLkT2(xL,kT2)
      frat=quad(lambda kT2: fKkT(kT2)/fpikT(kT2),0,kT2max)[0]
      K=self.get_kinfact(Q2,x,ye)
      return F2pi*frat*K

  def get_F2LN(self,xpi,y,Q2,kT2max):
    F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
    #F2pi=self.N*xpi**(self.a0+self.a1*self.eta(Q2))*(1-xpi)
    if conf['ln mode']=='model':
      f=self.get_fN(y,kT2max)
    elif conf['ln mode']=='piecewise':
      if y not in F: F[y]=self.get_fN(y,kT2max)
      f=F[y]
    return F2pi * f

  def get_R(self,x,xpi,deltaxL,y,Q2,kT2max):
    F2pi=conf['pion-stfuncs'].get_FXN(xpi,Q2)
    key='%f,%f'%(x,Q2)
    if key not in self.storage:
      #self.storage[key]=conf['stfuncs'].get_FXN(x,Q2,stf='F2',tmc=False)
      self.storage[key]=conf['idis stfuncs'].get_FXN(x,Q2,stf='F2',tmc=False)
    F2p= self.storage[key]
    if conf['ln mode']=='model':
      f=self.get_fN(y,kT2max)
    elif conf['ln mode']=='piecewise':
      if y not in F: F[y]=self.get_f(y,kT2max)
      f=F[y]
    return deltaxL * F2pi / F2p * f

  # moments of spliting functions

  def get_fmom(self):
    return quad(lambda y: self.get_f(y,np.inf),0,1)[0]

  def get_fNmom(self):
    return quad(lambda y: self.get_fN(y,np.inf),0,1)[0]

  def get_fDmom(self):
    return quad(lambda y: self.get_fD(y,np.inf),0,1)[0]

  #-> not sure what is the purpose of the next 2 functions

  def get_multiplicities(L_p2pin):
    L_p2pin = L
    return (3.0/2.0)*quad(lambda y: self.get_f(y,np.inf),0,1)[0]

  def get_f_tdis(self,y,kT2min,kT2max):
    return quad(lambda kT2: self.integrand(y,kT2),kT2min,kT2max)[0]

  def get_tmom(self,model,L,ymax,kT2max):
    return quad(lambda y: self.get_f(y,model,kT2max(y)),0,ymax)[0]

  # db-ub

  def get_qv(self,x,Q2):
    xqv = conf['pdf-pion'].get_xF(x,Q2,'ub')-conf['pdf-pion'].get_xF(x,Q2,'u')
    return xqv/x

  def get_dbub(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_f(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  def get_dbubN(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_fN(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  def get_dbubD(self,x,Q2):
    return fixed_quad(np.vectorize(lambda y: 1.0/y*self.get_fD(y,np.inf)*self.get_qv(x/y,Q2)),x,1.0,n=5)[0]

  # get, set state

  def get_state(self):
      return (self.L_p2pin)

  def set_state(self,state):
      self.L_p2pin=state

if __name__=='__main__':

  conf['Q20'] = 1.0
  conf['alphaSmode']='backward'
  conf['dglap mode']='truncated'
  conf['order']='NLO'
  conf['scheme']='ZMVFS'
  conf['ln mode']='model'
  conf['SPLFUNC model']='cov exp'

  conf['aux']=qcdlib.aux.AUX()
  conf['eweak']=qcdlib.eweak.EWEAK()
  conf['alphaS']=qcdlib.alphaS.ALPHAS()
  conf['mellin-ext']=qcdlib.mellin.MELLIN(extended=True)
  conf['mellin']=qcdlib.mellin.MELLIN()
  conf['pdf-pion']=qcdlib.pdfpion0.PDF()
  conf['pion-stfuncs']=STFUNCS()
  conf['pdf']=qcdlib.pdf0.PDF()
  conf['stfuncs']=obslib.dis.stfuncs.STFUNCS()
  ln=LN()

  x=0.000224
  Q2=7.3
  y=0.635
  xpi=x/y
  kT2max=0.4
  deltaxL=0.2
  print(ln.get_F2LN(xpi,y,Q2,kT2max))
  print(ln.get_R(x,xpi,deltaxL,y,Q2,kT2max))
  print(ln.get_dbub(x,Q2))


# -*- coding: utf-8 -*-
"""
Created on Sat Mar 05 23:39:47 2016

@author: xzk
"""
import collections
class Solution(object):
    def ladderLength(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: Set[str]
        :rtype: int
        #BFS
        """
        queue = collections.deque([(beginWord, 1)])
        visited = set([])
        while queue:
            word, dist = queue.popleft()
            if word == endWord:
                return dist
            for i in xrange(len(word)):
                for j in 'abcdefghijklmnopqrstuvwxyz':
                    new_word = word[0:i] + j + word[i+1:]
                    if new_word not in visited and new_word in wordList:
                        queue.append((new_word, dist+1))
                        visited.add(new_word)
        return 0
    def ladderLength2(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: Set[str]
        :rtype: int
        """
        front, back = set([beginWord]), set([endWord])
        wordList.discard(beginWord)
        dist = 2
        while front:
            front = wordList & set(word[0:i] + char + word[i+1:] for word in front \
                                    for i in xrange(len(beginWord)) for char in 'abcdefghijklmnopqrstuvwxyz')
            if front & back:
                return dist
            dist += 1
            if len(back) < len(front):
                front, back = back, front #Trick.
            wordList -= front
        return 0


import numpy as np
from dask.multiprocessing import get

from torchy.utils import timeit

import torch
from torch.nn.functional import conv2d
from torch.autograd import Variable

def conv(input, kernel):
    print("[>] [conv]")
    if isinstance(input, np.ndarray):
        input = Variable(torch.from_numpy(input))
    if isinstance(kernel, np.ndarray):
        kernel = Variable(torch.from_numpy(kernel))
    out = conv2d(input=input, weight=kernel).data
    print("[<] [conv]")
    return out

def make_zeros(fid, shape):
    print("[>] [make_zeros::fid_{}]".format(fid))
    print("[<] [make_zeros::fid_{}]".format(fid))
    return np.zeros(shape=shape)

if __name__ == '__main__':
    dsk = {'input': (np.zeros, (1, 1, 100, 100)),
           'weights': (np.zeros, (10, 1, 5, 5)),
           'conv1': (conv, 'input', 'weights')}

    with timeit() as time_info:
        output = get(dsk, 'conv1')

    print("Time Elapsed: {}".format(time_info.elapsed_time))


# -*- coding: utf-8 -*-
"""
Created on Tue Dec 19 10:08:20 2017

@author: user# -*- coding: utf-8 -*-
"""
#####update sensor data every time#

import serial
import numpy as np
import time
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from pandas import Series,DataFrame
import xlsxwriter
#from pylab import rcParams

M=np.zeros((50,50))
T0=np.zeros((50,50))
T1=np.zeros((50,50))
T2=np.zeros((50,50))
T3=np.zeros((50,50))
T4=np.zeros((50,50))
T5=np.zeros((50,50))
T6=np.zeros((50,50))
T7=np.zeros((50,50))
a=np.zeros((50,50))
b=np.zeros((50,50))
c=np.zeros((50,50))
empty=[None]*2500

tcal=[None]*10
tvel=[None]*2500

port="COM5"
port1="COM7"
port2="COM8"
baud2=9800
baud1=57600
baud = 500000
temp = 0b101111
data = 0
header = 0
PressureArray=[]
X=[x for x in range(50)]
Y=[y for y in range(50)]
x,y=np.meshgrid(X, Y)
#fig=plt.figure()
#plt.axes().set_aspect('equal')
row=0
maxi=0
mini=0

row=0
excelrow=-1
workbook  = xlsxwriter.Workbook(r"C:\Users\user\Desktop\SensorData7.csv")
worksheet = workbook.add_worksheet()

def AutoVel(h):
    global tvel
    re1="0"
    re2='0'
    while(re1 != b'!\r'):
        T= 23+5*h
        ss="SS0"+str(T)+".00"+"\n\r"
        print(ss)
        ll=[ord(c) for c in ss]
        ser1.write(ll)
        re1=ser1.read(2)
        print(re1)

    for number in range(3):
        time.sleep(3600)
        print("waiting for temperature to stable")

    ser.flushInput()
    ser.flushOutput()
    ser2.flushInput()
    ser2.flushOutput()
    ser1.flushInput()
    ser1.flushOutput()
    print('successful')

    ss='MON2? 1\n\r'
    print(ss)
    ll=[ord(c) for c in ss]
    ser2.write(ll)
    time.sleep(0.5)
    re2=ser2.read(5)
        #re2.substring(0, re2.length() - 2)
    print(re2)
    tvel[h]=float(re2)
    print(tvel[h])
    SaveFile(tvel)

def ReadCal():
    global T0,T1,T2,T3,T4,T5,T6,T7,a,b,c,maxi,mini,tcal

    s='read data point 0'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T0[row,j]=data2
    print(T0)

    s='read data point 1'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T1[row,j]=data2
    print(T1)

    s='read data point 2'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T2[row,j]=data2
    print(T2)

    s='read data point 3'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T3[row,j]=data2
    print(T3)

    s='read data point 4'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T4[row,j]=data2
    print(T4)

    s='read data point 5'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T5[row,j]=data2
    print(T5)

    s='read data point 6'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T6[row,j]=data2
    print(T6)

    s='read data point 7'+'\n'
    l=[ord(c) for c in s]
    ser.write(l)
    for i in range(50):
        header=0
        data1=0
        row=0
        while(header != b'\xff'):
            header = ser.read(1)
        header=0
        data1=ser.read(2)
        if(data1==b'\xff\xff'):
            row=int.from_bytes(ser.read(1),'little')
            if row>50:
                break;
            for j in range(50):
                data=int.from_bytes(ser.read(2),byteorder='big')
                if data==0:
                    data2=0;
                else:
                    data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                T7[row,j]=data2
    print(T7)

    y=np.array([24.707, 29.598, 34.504, 39.429, 44.365, 49.33, 54.277, 59.242])
    print('tcal:',24.707, 29.598, 34.504, 39.429, 44.365, 49.33, 54.277, 59.242)
    for i in range(50):
        for j in range(50):
            x=np.array([T0[i,j],T1[i,j],T2[i,j],T3[i,j],T4[i,j],T5[i,j],T6[i,j],T7[i,j]])
            if(T0[i,j]>0 and T1[i,j]>0 and T2[i,j]>0 and T3[i,j]>0 and T4[i,j]>0
               and T5[i,j]>0 and T6[i,j]>0 and T7[i,j]>0):
                f = np.polyfit(x,y,2)
                p= np.poly1d(f)
                a[i,j]=p[2]
                b[i,j]=p[1]
                c[i,j]=p[0]

def DoCal(row,j,data2):
    data=a[row,j]*data2*data2+b[row,j]*data2+c[row,j]
    #data=b[row,j]*data2+c[row,j]
    return data

def SaveFile(list):
    global excelrow,worksheet,workbook
    col=0
    excelrow= excelrow+1
    for col in range(2500):
        worksheet.write(excelrow, col, list[col])

def animate():
    global maxi,mini
    TemData = np.zeros((50,50))
    while 1:
        for i in range(50):
            header=0
            data1=0
            row=0
            maxi=0
            mini=45
            while(header != b'\xff'):
                header = ser.read(1)
            header=0
            data1=ser.read(2)
            if(data1==b'\xff\xff'):
                row=int.from_bytes(ser.read(1),'little')
                if row>50:
                    break;
                for j in range(50):
                    data=int.from_bytes(ser.read(2),byteorder='big')
                    #data2=((330 / (0.244141 * data/ 1000 + 2) * 10 - 100 - 800) / 8.7298)
                    if data==0:
                        data3=0
                    else:
                        data2=((3003 / ((0.292969 * data) / 1000 + 1.8)) - 834.66) / 8.7295
                        data3=DoCal(row,j,data2)
                    TemData[row,49-j] = data3
                row=0
                data1=0
        yield TemData

            #plt.contourf(x,y,df, 50, cmap=plt.cm.jet,vmin=0, vmax=700)
            #plt.show()

ser = serial.Serial(port, baud, timeout=1)
if ser.isOpen():
     print(ser.name + ' is open...')

ser1 = serial.Serial(port1, baud1, timeout=1)
if ser1.isOpen():
     print(ser1.name + ' is open...')

ser2 = serial.Serial(port2, baud2, timeout=1)
if ser1.isOpen():
     print(ser2.name + ' is open...')

ss='idle'+'\n'
ll=[ord(c) for c in ss]
ser.write(ll)
time.sleep(0.5)
ser.flushInput()
ReadCal()

rw=animate()

for n in range(8):
    ss='idle'+'\n'
    ll=[ord(c) for c in ss]
    ser.write(ll)
    time.sleep(0.5)
    ser.flushInput()
    AutoVel(n)
    ss='run'+'\n'
    ll=[ord(c) for c in ss]
    ser.write(ll)
    for i in range(3):
        #plt.clf()
        M=next(rw)
        CS=plt.contourf(X, Y, M, 100,origin='upper', cmap=plt.cm.jet,vmin=25, vmax=50)
        ListData=np.reshape(M,2500)
        SaveFile(ListData)
    SaveFile(empty)
    plt.title('Max: '+str(maxi)+'\n Min:'+str(mini))
    plt.xticks(())
    plt.yticks(())
    plt.figure("Nano and Advanced Materials Institue")
    plt.axes().set_aspect('equal')
    #plt.colorbar(CS)
    plt.pause(0.03)

print('Finished')
SaveFile(tvel)
workbook.close()
ser.close()
ser1.close()
ser2.close()


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

def input_data():
    N = 100 # number of points per class
    D = 2 # dimensionality
    K = 3 # number of classes
    X = np.zeros((N*K,D)) # data matrix (each row = single example)
    y = np.zeros((N*K,K), dtype='uint8') # class labels
    for j in range(K):
        ix = range(N*j,N*(j+1))
        r = np.linspace(0.0,1,N) # radius
        t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.5 # theta
        X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
        y[ix,j] = 1 # one-hot
    return X, y

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def model(X, W, b, W2, b2):
    hidden_layer = tf.nn.relu(tf.matmul(X, W) + b)
    scores = tf.matmul(hidden_layer, W2) + b2
    return scores

x_data, y_data = input_data()

D, h, K = 2, 100, 3

# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, D])
Y = tf.placeholder(tf.float32, shape=[None, K])

W  = init_weights([D,h])
b  = init_weights([1,h])
W2 = init_weights([h,K])
b2 = init_weights([1,K])

py_x = model(X, W, b, W2, b2)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                        logits=py_x, labels=Y))
train_op = tf.train.AdamOptimizer().minimize(cost)
predict_op = tf.argmax(py_x, axis=1)

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    for i in range(20000):
        sess.run(train_op, feed_dict={X: x_data, Y: y_data})
        if i % 1000 == 0:
            cost_val, pred_val = sess.run([cost, predict_op],
                 feed_dict={X: x_data, Y: y_data})
            accuracy = np.mean(np.argmax(y_data, axis=1) == pred_val)
            print("iteration %5d : loss %.5f : accuracy %.5f" % (i, cost_val, accuracy))

    # plot the resulting classifier
    h = 0.02
    x_min, x_max = x_data[:, 0].min() - 1, x_data[:, 0].max() + 1
    y_min, y_max = x_data[:, 1].min() - 1, x_data[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                      np.arange(y_min, y_max, h))
    xgrid_data = np.c_[xx.ravel(), yy.ravel()]
    Z = sess.run(predict_op, feed_dict={X: xgrid_data})
    Z = Z.reshape(xx.shape)
    fig = plt.figure()
    plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
    plt.scatter(x_data[:, 0], x_data[:, 1])
    # , c=np.int(np.argmax(y_data)), s=40, cmap=plt.cm.Spectral)
    plt.xlim(x_min, x_max)
    plt.ylim(y_min, y_max)
    plt.show()


from nltk import pos_tag
from nltk import word_tokenize, sent_tokenize

''' POS Tag list:

    CC coordinating conjunction
    CD cardinal digit
    DT determiner
    EX existential there (like: "there is" ... think of it like "there exists")
    FW foreign word
    IN preposition/subordinating conjunction
    JJ adjective 'big'
    JJR adjective, comparative 'bigger'
    JJS adjective, superlative 'biggest'
    LS list marker 1)
    MD modal could, will
    NN noun, singular 'desk'
    NNS noun plural 'desks'
    NNP proper noun, singular 'Harrison'
    NNPS proper noun, plural 'Americans'
    PDT predeterminer 'all the kids'
    POS possessive ending parent's
    PRP personal pronoun I, he, she
    PRP$ possessive pronoun my, his, hers
    RB adverb very, silently,
    RBR adverb, comparative better
    RBS adverb, superlative best
    RP particle give up
    TO to go 'to' the store.
    UH interjection errrrrrrrm
    VB verb, base form take
    VBD verb, past tense took
    VBG verb, gerund/present participle taking
    VBN verb, past participle taken
    VBP verb, sing. present, non-3d take
    VBZ verb, 3rd person sing. present takes
    WDT wh-determiner which
    WP wh-pronoun who, what
    WP$ possessive wh-pronoun whose
    WRB wh-abverb where, when
'''

# sample_text = "I was walking by the road when I saw the cutest puppy on the other side"
#
# tokenized_words = word_tokenize(sample_text)
# tag = pos_tag(tokenized_words)
# print(tag)

with open("2001aspaceodyssey.txt", "r") as corpora:
    sample_text = corpora.read()

# print(sample_text)

tokenized_sentences = sent_tokenize(sample_text)
for sentence in tokenized_sentences:
    tokenized_words = word_tokenize(sentence)
    tag = pos_tag(tokenized_words)
    print(tag)


import pygame
from tela import Tela

class Texto:
    def __init__(self,
                 texto,
                 font,
                 tamanho,
                 cor,
                 posicao):

        self.__texto = texto
        self.__font = font
        self.__tamanho = tamanho
        self.__cor = cor
        self.__tela = Tela()
        self.__posicao = posicao

        self.__fonteCompleta = pygame.font.Font(self.__font, self.__tamanho)

    @property
    def texto(self):
        return self.__texto

    @texto.setter
    def texto(self, texto):
        self.__texto = texto

    @property
    def posicao(self):
        return self.__posicao

    @posicao.setter
    def posicao(self, posicao):
        self.__posicao = posicao

    def draw(self):
        temp = self.__fonteCompleta.render(self.__texto, False, self.__cor).convert_alpha(self.__tela.display)
        self.__tela.display.blit(temp, self.__posicao)


from functools import reduce

try:
    import configparser
except ImportError:
    import ConfigParser as configparser

import os
import re

from vit import util

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
BRACKETS_REGEX = re.compile("[<>]")
DEFAULT_KEYBINDINGS_SECTIONS = ('global', 'navigation', 'command', 'report')
CONFIG_SPECIAL_KEY_SUBSTITUTIONS = {
    'colon': ':',
    'equals': '=',
    'space': ' ',
}

class KeybindingError(Exception):
    pass

class KeybindingParser(object):
    def __init__(self, loader, config, action_registry):
        self.loader = loader
        self.config = config
        self.action_registry = action_registry
        self.actions = self.action_registry.get_actions()
        self.noop_action_name = self.action_registry.make_action_name(self.action_registry.noop_action_name)
        self.default_keybinding_name = self.config.get('vit', 'default_keybindings')
        self.default_keybindings = configparser.SafeConfigParser()
        self.default_keybindings.optionxform=str
        self.sections = DEFAULT_KEYBINDINGS_SECTIONS
        self.keybindings = {}
        self.multi_key_cache = {}

    def is_keybinding(self, keys):
        return keys in self.keybindings

    def items(self, section):
        try:
            return self.default_keybindings.items(section)
        except configparser.NoSectionError:
            return []

    def load_default_keybindings(self):
        name = self.default_keybinding_name
        template = '%s/keybinding/%s.ini'
        user_keybinding_file = template % (self.loader.user_config_dir, name)
        keybinding_file = template % (BASE_DIR, name)
        if util.file_readable(user_keybinding_file):
            self.default_keybindings.read(user_keybinding_file)
        elif util.file_readable(keybinding_file):
            self.default_keybindings.read(keybinding_file)
        else:
            raise KeybindingError("default_keybindings setting '%s' invalid, file not found" % name)
        for section in self.sections:
            bindings = self.items(section)
            self.add_keybindings(bindings)

    def keybinding_special_keys_substitutions(self, value):
        if value in CONFIG_SPECIAL_KEY_SUBSTITUTIONS:
            value = CONFIG_SPECIAL_KEY_SUBSTITUTIONS[value]
        return value

    def parse_keybinding_keys(self, keys):
        has_modifier = bool(re.match(BRACKETS_REGEX, keys))
        parsed_keys = ' '.join(re.sub(BRACKETS_REGEX, ' ', keys).strip().split()).lower() if has_modifier else keys
        return self.keybinding_special_keys_substitutions(parsed_keys), has_modifier

    def parse_keybinding_value(self, value, replacements={}):
        def reducer(accum, char):
            if char == '<':
                accum['in_brackets'] = True
                accum['bracket_string'] = ''
            elif char == '>':
                accum['in_brackets'] = False
                accum['keybinding'].append(self.keybinding_special_keys_substitutions(accum['bracket_string'].lower()))
            elif char == '{':
                accum['in_variable'] = True
                accum['variable_string'] = ''
            elif char == '}':
                accum['in_variable'] = False
                if accum['variable_string'] in self.actions:
                    accum['action_name'] = accum['variable_string']
                else:
                    for replacement in replacements:
                        args = replacement['match_callback'](accum['variable_string'])
                        if isinstance(args, list):
                            accum['keybinding'].append((replacement['replacement_callback'], args))
                            return accum
                    raise ValueError("unknown config variable '%s'" % accum['variable_string'])
            else:
                if accum['in_brackets']:
                    accum['bracket_string'] += char
                elif accum['in_variable']:
                    accum['variable_string'] += char
                else:
                    accum['keybinding'].append(char)
            return accum
        accum = reduce(reducer, value, {
            'keybinding': [],
            'action_name': None,
            'in_brackets': False,
            'bracket_string': '',
            'in_variable': False,
            'variable_string': '',
        })
        return accum['keybinding'], accum['action_name']

    def validate_parsed_value(self, key_groups, bound_keys, action_name):
        if bound_keys and action_name:
            raise KeybindingError("keybindings '%s' unsupported configuration: ACTION_ variables must be used alone." % key_groups)

    def is_noop_action(self, keybinding):
        return True if 'action_name' in keybinding and keybinding['action_name'] == self.noop_action_name else False

    def filter_noop_actions(self, keybindings):
        return {keys:value for (keys, value) in keybindings.items() if not self.is_noop_action(keybindings[keys])}

    def get_keybindings(self):
        return self.keybindings

    def add_keybindings(self, bindings=[], replacements={}):
        for key_groups, value in bindings:
            bound_keys, action_name = self.parse_keybinding_value(value, replacements)
            self.validate_parsed_value(key_groups, bound_keys, action_name)
            for keys in key_groups.strip().split(','):
                parsed_keys, has_modifier = self.parse_keybinding_keys(keys)
                self.keybindings[parsed_keys] = {
                    'label': keys,
                    'has_modifier': has_modifier,
                }
                if action_name:
                    self.keybindings[parsed_keys]['action_name'] = action_name
                else:
                    self.keybindings[parsed_keys]['keys'] = bound_keys
        self.keybindings = self.filter_noop_actions(self.keybindings)
        return self.get_keybindings()


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

# In[73]:

import numpy as np
import pandas as pd
from scipy.spatial import distance
import os

# In[79]:

# Import data

filename_1 = "pre_outdoor_embedding.csv "
filename_2 = "pre_outdoor_mfcc.csv "
filename_3 = "pre_TV_embedding.csv"
filename_4 = "pre_TV_mfcc.csv"
data_1 = np.array(pd.read_csv(filename_1, header = None))
data_2 = np.array(pd.read_csv(filename_2, header = None))
data_3 = np.array(pd.read_csv(filename_3, header = None))
data_4 = np.array(pd.read_csv(filename_4, header = None))
X_1 = data_1[:, :-1]
Y_1 = data_1[:, -1]
X_2 = data_2[:, :-1]
Y_2 = data_2[:, -1]
X_3 = data_3[:, :-1]
Y_3 = data_3[:, -1]
X_4 = data_4[:, :-1]
Y_4 = data_4[:, -1]

# In[80]:

def getDistance(seg_1, seg_2):
#    return np.linalg.norm((seg_1 - seg_2), ord = 1)
    return scipy.spatial.distance.cosine(seg_1, seg_2)

def unsupervisedalgo(mfcc, Constants_mfcc_dist_diff_un):

    new_mfcc = np.zeros((mfcc.shape[0], mfcc.shape[1]))
    new_in_count = np.zeros((mfcc.shape[0], ))
    new_mfcc[0, :] = mfcc[0, :]
    speaker_count = 1
    new_in_count[0] = 1

    for i in range(1, mfcc.shape[0], 1):
        diff_count = 0
        for j in range(speaker_count):
            mfcc_dist = getDistance(mfcc[i], new_mfcc[j])
            if (mfcc_dist >= Constants_mfcc_dist_diff_un):
                diff_count = diff_count + 1;
            else:
                new_mfcc[j] = (new_mfcc[j]*new_in_count[j]+mfcc[i])/(new_in_count[j]+1)
                new_in_count[j] += 1
                break
        if (diff_count == speaker_count):
            new_mfcc[speaker_count, :] = mfcc[i, :]
            new_in_count[speaker_count] += 1
            speaker_count = speaker_count + 1
    return speaker_count, new_in_count

# In[81]:

print(unsupervisedalgo(X_1, 0.5))
print(unsupervisedalgo(X_2, 0.05))

print(unsupervisedalgo(X_3, 0.5))
print(unsupervisedalgo(X_4, 0.05))

# In[ ]:


## Author: Xinwu Qian
## Last Update: 2019-2-5

#utility functions that will be used in the taxi_env
from collections import deque
#update the waiting time of passengers in Q

def waiting_time_update(waiting_time,expect_waiting_time):
    try:
        (new_wait, new_expect_wait) = map(deque, zip(*[(i+1,j) for i,j in zip(waiting_time,expect_waiting_time) if i<j]))
    except ValueError:
        new_wait=deque([])
        new_expect_wait=deque([])

    return new_wait,new_expect_wait


from django.core.management.base import BaseCommand, CommandError
from uwsgi_log_parser.utils import load_line

class Command(BaseCommand):
    args = "<log_path log_path ...>"
    help = "Load uwsgi log from raw log file"

    def handle(self, *args, **options):
        for log_path in args:
            self.stdout.write('Processing path: %s ...' % log_path)
            self.load_single_log_file(log_path)

    def load_single_log_file(self, log_path):
        try:
            with open(log_path, 'r') as log_file:
                for line in log_file:
                    load_line(line)
        except IOError:
            raise CommandError('%s does not exist.' % log_path)


from lib.H3Kernels import kernel
import utils.plot_utils as pltools
import numpy as np
import matplotlib.pyplot as plt

__author__ = 'morgan'

def obj_f(x):
    # 1d function
    return np.array([np.sin(x0)+x0*np.cos(x0) for x0 in x])

X = np.linspace(-5, 5, 100).reshape(100, 1)
y = obj_f(X)
given_idx = np.random.choice(y.size, 3)
x_cov = kernel(X, metric='rbf')
m = np.zeros(X.shape[0])
f = np.random.multivariate_normal(m, x_cov, size=3)
# posterior
k_tt = kernel(X, metric='rbf')
k_tr = kernel(X[given_idx], metric='rbf')
k_ttr = kernel(X, X[given_idx], metric='rbf')
m_posterior = k_ttr.dot(np.linalg.inv(k_tr)).dot(y[given_idx].reshape(given_idx.size, 1))
cov_posterior = k_tt-k_ttr.dot(np.linalg.inv(k_tr)).dot(k_ttr.T)
f_posterior = np.random.multivariate_normal(m_posterior.ravel(), cov_posterior, size=3)
plt.subplot(121)
plt.plot(X, f[0], 'r-')
plt.plot(X, f[1], 'b-')
plt.plot(X, f[2], 'g-')
plt.plot(X, y, 'k-.', label='groundtruth f')
plt.legend()
plt.subplot(122)
plt.plot(X, f_posterior[0], 'r-')
plt.plot(X, f_posterior[1], 'b-')
plt.plot(X, f_posterior[2], 'g-')
plt.plot(X, y, 'k-.', label='groundtruth f')
plt.plot(X[given_idx], y[given_idx], 'ko', ms=6)
plt.fill_between(X.ravel(), m_posterior.ravel()-4*cov_posterior.diagonal(),
                 m_posterior.ravel()+4*cov_posterior.diagonal(),
                 facecolor='gray', alpha=.25, edgecolor='none')
plt.gca().set(xlim=[-5, 5])
plt.legend()
plt.show()


# -*- coding: utf-8 -*-
"""
    hexonet.apiconnector.apiclient
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    This module covers all necessary functionality for http communicatiton
    with our Backend System.
    :copyright: © 2018 by HEXONET GmbH.
    :license: MIT, see LICENSE for more details.
"""

from hexonet.apiconnector.response import Response
from hexonet.apiconnector.responsetemplatemanager import ResponseTemplateManager as RTM
from hexonet.apiconnector.socketconfig import SocketConfig
from six.moves.urllib.parse import quote, unquote
from six import string_types
import re
from six.moves.urllib.request import urlopen, Request
from six.moves.urllib.parse import urlparse, urlencode
import copy

rtm = RTM()

class APIClient(object):

    def __init__(self):
        # API connection url
        self.setURL("https://coreapi.1api.net/api/call.cgi")
        # Object covering API connection data
        self.__socketConfig = SocketConfig()
        # activity flag for debug mode
        self.__debugMode = False
        # API connection timeout setting
        self.__socketTimeout = 300000
        self.useLIVESystem()

    def enableDebugMode(self):
        """
        Enable Debug Output to STDOUT
        """
        self.__debugMode = True
        return self

    def disableDebugMode(self):
        """
        Disable Debug Output
        """
        self.__debugMode = False
        return self

    def getPOSTData(self, cmd):
        """
        Serialize given command for POST request including connection configuration data
        """
        data = self.__socketConfig.getPOSTData()
        tmp = ""
        if not isinstance(cmd, string_types):
            for key in cmd.keys():
                if (cmd[key] is not None):
                    tmp += ("{0}={1}\n").format(key, re.sub('[\r\n]', '', str(cmd[key])))
        return ("{0}{1}={2}").format(data, quote('s_command'), quote(re.sub('\n$', '', tmp)))

    def getSession(self):
        """
        Get the API Session that is currently set
        """
        return self.__socketConfig.getSession()

    def getURL(self):
        """
        Get the API connection url that is currently set
        """
        return self.__socketURL

    def getVersion(self):
        """
        Get the current module version
        """
        return "2.0.1"

    def saveSession(self, session):
        """
        Apply session data (session id and system entity) to given client request session
        """
        session["socketcfg"] = {
            "entity": self.__socketConfig.getSystemEntity(),
            "session": self.__socketConfig.getSession()
        }
        return self

    def reuseSession(self, session):
        """
        Use existing configuration out of session
        to rebuild and reuse connection settings
        """
        self.__socketConfig.setSystemEntity(session["socketcfg"]["entity"])
        self.setSession(session["socketcfg"]["session"])
        return self

    def setURL(self, value):
        """
        Set another connection url to be used for API communication
        """
        self.__socketURL = value
        return self

    def setOTP(self, value):
        """
        Set one time password to be used for API communication
        """
        self.__socketConfig.setOTP(value)
        return self

    def setSession(self, value):
        """
        Set an API session id to be used for API communication
        """
        self.__socketConfig.setSession(value)
        return self

    def setRemoteIPAddress(self, value):
        """
        Set an Remote IP Address to be used for API communication.
        To be used in case you have an active ip filter setting.
        """
        self.__socketConfig.setRemoteAddress(value)
        return self

    def setCredentials(self, uid, pw):
        """
        Set Credentials to be used for API communication
        """
        self.__socketConfig.setLogin(uid)
        self.__socketConfig.setPassword(pw)
        return self

    def setRoleCredentials(self, uid, role, pw):
        """
        Set Credentials to be used for API communication
        """
        if (role == ''):
            return self.setCredentials(uid, pw)
        return self.setCredentials(("{0}!{1}").format(uid, role), pw)

    def login(self, otp=""):
        """
        Perform API login to start session-based communication
        """
        self.setOTP(otp)
        rr = self.request({"COMMAND": "StartSession"})
        if (rr.isSuccess()):
            col = rr.getColumn("SESSION")
            self.setSession(col.getData()[0] if (col is not None) else None)
        return rr

    def loginExtended(self, params, otp=""):
        """
        Perform API login to start session-based communication.
        Use given specific command parameters.
        """
        self.setOTP(otp)
        cmd = {
            "COMMAND": "StartSession"
        }
        cmd.update(params)
        rr = self.request(cmd)
        if (rr.isSuccess()):
            col = rr.getColumn("SESSION")
            self.setSession(col.getData()[0] if (col is not None) else None)
        return rr

    def logout(self):
        """
        Perform API logout to close API session in use
        """
        rr = self.request({
            "COMMAND": "EndSession",
        })
        if (rr.isSuccess()):
            self.setSession(None)
        return rr

    def request(self, cmd):
        """
        Perform API request using the given command
        """
        data = self.getPOSTData(cmd).encode('UTF-8')
        # TODO: 300s (to be sure to get an API response)
        try:
            req = Request(self.__socketURL, data, {
                'User-Agent': 'python-sdk::' + self.getVersion()
            })
            body = urlopen(req, timeout=self.__socketTimeout).read()
            if (self.__debugMode):
                print(self.__socketURL, data, body, '\n', '\n')
        except Exception:
            body = rtm.getTemplate("httperror").getPlain()
            if (self.__debugMode):
                print(self.__socketURL, data, "HTTP communication failed", body, '\n', '\n')
        return Response(body, cmd)

    def requestNextResponsePage(self, rr):
        """
        Request the next page of list entries for the current list query
        Useful for tables
        """
        mycmd = self.__toUpperCaseKeys(rr.getCommand())
        if ("LAST" in mycmd):
            raise Exception("Parameter LAST in use. Please remove it to avoid issues in requestNextPage.")
        first = 0
        if ("FIRST" in mycmd):
            first = mycmd["FIRST"]
        total = rr.getRecordsTotalCount()
        limit = rr.getRecordsLimitation()
        first += limit
        if (first < total):
            mycmd["FIRST"] = first
            mycmd["LIMIT"] = limit
            return self.request(mycmd)
        else:
            return None

    def requestAllResponsePages(self, cmd):
        """
        Request all pages/entries for the given query command
        """
        responses = []
        mycmd = copy.deepcopy(cmd)
        mycmd["FIRST"] = 0
        rr = self.request(mycmd)
        tmp = rr
        while tmp is not None:
            responses.append(tmp)
            tmp = self.requestNextResponsePage(tmp)
            if tmp is None:
                break
        return responses

    def setUserView(self, uid):
        """
        Set a data view to a given subuser
        """
        self.__socketConfig.setUser(uid)
        return self

    def resetUserView(self):
        """
        Reset data view back from subuser to user
        """
        self.__socketConfig.setUser(None)
        return self

    def useOTESystem(self):
        """
        Set OT&E System for API communication
        """
        self.__socketConfig.setSystemEntity("1234")
        return self

    def useLIVESystem(self):
        """
        Set LIVE System for API communication (this is the default setting)
        """
        self.__socketConfig.setSystemEntity("54cd")
        return self

    def __toUpperCaseKeys(self, cmd):
        """
        Translate all command parameter names to uppercase
        """
        newcmd = {}
        for k in cmd.keys():
            newcmd[k.upper()] = cmd[k]
        return newcmd


def resolve():
    # n = int(input())
    # al = list(map(int, input().split()))
    # al_cnt = [0] * (n + 1)
    #
    # for i in range(1, n + 1):
    #     al_cnt[i] = al.count(i)
    # max_cnt = 0
    #
    # for j in range(1, n + 1):
    #     if al_cnt[j] >= 2:
    #         max_cnt += al_cnt[j] * (al_cnt[j] - 1) / 2
    #
    # for l in range(n):
    #     if al_cnt[al[l]] >= 3:
    #         ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2) + ((al_cnt[al[l]] - 1) * (al_cnt[al[l]] - 2) / 2)
    #         print(int(ans) if ans > 0 else 0)
    #     else:
    #         ans = max_cnt - (al_cnt[al[l]] * (al_cnt[al[l]] - 1) / 2)
    #         print(int(ans) if ans > 0 else 0)

    from collections import Counter

    n = int(input())
    al = list(map(int, input().split()))
    al_cnt = Counter(al)
    ans = sum([i * (i - 1) // 2 for i in al_cnt.values()])
    for j in al:
        print(ans - al_cnt[j] + 1)

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
1 1 2 1 2"""
        output = """2
2
3
2
3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
1 2 3 4"""
        output = """0
0
0
0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5
3 3 3 3 3"""
        output = """6
6
6
6
6"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """8
1 2 1 4 2 1 4 1"""
        output = """5
7
5
7
7
5
7
5"""
        self.assertIO(input, output)

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