import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.nn.modules.module import _addindent
from torch.utils.data import Dataset
import numpy as np 
from collections import Counter
import pdb
import matplotlib.pyplot as plt
import os
from torch.autograd import Variable


class FocalLoss(nn.Module):
    r"""
        This criterion is a implemenation of Focal Loss, which is proposed in 
        Focal Loss for Dense Object Detection.
            
            Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
    
        The losses are averaged across observations for each minibatch.
        Args:
            alpha(1D Tensor, Variable) : the scalar factor for this criterion
            gamma(float, double) : gamma > 0; reduces the relative loss for well-classiﬁed examples (p > .5), 
                                   putting more focus on hard, misclassiﬁed examples
            size_average(bool): size_average(bool): By default, the losses are averaged over observations for each minibatch.
                                However, if the field size_average is set to False, the losses are
                                instead summed for each minibatch.
    """
    def __init__(self, class_num, alpha=None, gamma=2, size_average=True):
        super(FocalLoss, self).__init__()
        if alpha is None:
            self.alpha = Variable(torch.ones(class_num, 1))
        else:
            if isinstance(alpha, Variable):
                self.alpha = alpha
            else:
                self.alpha = Variable(alpha)
        self.gamma = gamma
        self.class_num = class_num
        self.size_average = size_average

    def forward(self, inputs, targets):
        N = inputs.size(0)
        # print(N)
        C = inputs.size(1)
        P = F.softmax(inputs, dim=1)

        class_mask = inputs.data.new(N, C).fill_(0)
        class_mask = Variable(class_mask)
        ids = targets.view(-1, 1)
        class_mask.scatter_(1, ids.data, 1.)
        #print(class_mask)
        

        if inputs.is_cuda and not self.alpha.is_cuda:
            self.alpha = self.alpha.cuda()
        alpha = self.alpha[ids.data.view(-1)]
        
        probs = (P*class_mask).sum(1).view(-1,1)

        log_p = probs.log()
        #print('probs size= {}'.format(probs.size()))
        #print(probs)

        batch_loss = -alpha*(torch.pow((1-probs), self.gamma))*log_p 
        #print('-----bacth_loss------')
        #print(batch_loss)

        
        if self.size_average:
            loss = batch_loss.mean()
        else:
            loss = batch_loss.sum()
        return loss
# summarize Pytorch model
def summary(model, show_weights=True, show_parameters=True):
    """Summarizes torch model by showing trainable parameters and weights."""
    tmpstr = model.__class__.__name__ + ' (\n'
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params and weights
        if type(module) in [
            torch.nn.modules.container.Container,
            torch.nn.modules.container.Sequential
        ]:
            modstr = torch_summarize(module)
        else:
            modstr = module.__repr__()
        modstr = _addindent(modstr, 2)

        params = sum([np.prod(p.size()) for p in module.parameters()])
        weights = tuple([tuple(p.size()) for p in module.parameters()])

        tmpstr += '  (' + key + '): ' + modstr 
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr +=  ', parameters={}'.format(params)
        tmpstr += '\n'   

    tmpstr = tmpstr + ')'
    return tmpstr

# multi indexing of values in a dictionary combined in a result dictionary. 
def create_data_variables(dct, idx):
    result_dct = {}
    for x in dct.keys():
        result_dct[x] = dct[x][idx]
    return result_dct
        
# Quantification dataset for all data 
class HRMASDataset_Type_A(Dataset):
    def __init__(self, dct):
        super(HRMASDataset_Type_A, self).__init__()
        self.data_dct = dct
    def __len__(self):
        return self.data_dct["spectra"].shape[0]
    def __getitem__(self, idx):
        return (self.data_dct["spectra"][idx], self.data_dct["ppm_spectra"][idx], self.data_dct["quant"][idx], self.data_dct["quant_availability"][idx], self.data_dct["class_labels"][idx])

# Quantification dataset for all data 
class HRMASDataset_Type_B(Dataset):
    def __init__(self, dct):
        super(HRMASDataset_Type_B, self).__init__()
        self.data_dct = dct
    def __len__(self):
        return self.data_dct["spectra"].shape[0]
    def __getitem__(self, idx):
        return (self.data_dct["spectra"][idx], self.data_dct["ppm_spectra"][idx], self.data_dct["quant"][idx], self.data_dct["class_labels"][idx])

# changed and used from a MIT licensed repo on github
# reference: https://github.com/Bjarten/early-stopping-pytorch/blob/master/pytorchtools.py
class EarlyStopping:
    """Early stops the training if validation loss doesn't improve after a given patience."""
    def __init__(self, patience=7, verbose=False, delta=0):
        """
        Args:
            patience (int): How long to wait after last time validation loss improved.
                            Default: 7
            verbose (bool): If True, prints a message for each validation loss improvement. 
                            Default: False
            delta (float): Minimum change in the monitored quantity to qualify as an improvement.
                            Default: 0
        """
        self.patience = patience
        self.verbose = verbose
        self.counter = 0
        self.best_score = None
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.delta = delta

    def __call__(self, val_loss):

        score = -val_loss

        if self.best_score is None:
            self.best_score = score
        elif score < self.best_score + self.delta:
            self.counter += 1
            if self.counter >= self.patience:
                self.early_stop = True
        else:
            self.best_score = score
            self.counter = 0
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.nn.modules.module import _addindent
from torch.utils.data import Dataset
import numpy as np 
from collections import Counter
import pdb
import matplotlib.pyplot as plt
import os
from torch.autograd import Variable


class FocalLoss(nn.Module):
    r"""
        This criterion is a implemenation of Focal Loss, which is proposed in 
        Focal Loss for Dense Object Detection.
            
            Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
    
        The losses are averaged across observations for each minibatch.
        Args:
            alpha(1D Tensor, Variable) : the scalar factor for this criterion
            gamma(float, double) : gamma > 0; reduces the relative loss for well-classiﬁed examples (p > .5), 
                                   putting more focus on hard, misclassiﬁed examples
            size_average(bool): size_average(bool): By default, the losses are averaged over observations for each minibatch.
                                However, if the field size_average is set to False, the losses are
                                instead summed for each minibatch.
    """
    def __init__(self, class_num, alpha=None, gamma=2, size_average=True):
        super(FocalLoss, self).__init__()
        if alpha is None:
            self.alpha = Variable(torch.ones(class_num, 1))
        else:
            if isinstance(alpha, Variable):
                self.alpha = alpha
            else:
                self.alpha = Variable(alpha)
        self.gamma = gamma
        self.class_num = class_num
        self.size_average = size_average

    def forward(self, inputs, targets):
        N = inputs.size(0)
        # print(N)
        C = inputs.size(1)
        P = F.softmax(inputs, dim=1)

        class_mask = inputs.data.new(N, C).fill_(0)
        class_mask = Variable(class_mask)
        ids = targets.view(-1, 1)
        class_mask.scatter_(1, ids.data, 1.)
        #print(class_mask)
        

        if inputs.is_cuda and not self.alpha.is_cuda:
            self.alpha = self.alpha.cuda()
        alpha = self.alpha[ids.data.view(-1)]
        
        probs = (P*class_mask).sum(1).view(-1,1)

        log_p = probs.log()
        #print('probs size= {}'.format(probs.size()))
        #print(probs)

        batch_loss = -alpha*(torch.pow((1-probs), self.gamma))*log_p 
        #print('-----bacth_loss------')
        #print(batch_loss)

        
        if self.size_average:
            loss = batch_loss.mean()
        else:
            loss = batch_loss.sum()
        return loss
# summarize Pytorch model
def summary(model, show_weights=True, show_parameters=True):
    """Summarizes torch model by showing trainable parameters and weights."""
    tmpstr = model.__class__.__name__ + ' (\n'
    for key, module in model._modules.items():
        # if it contains layers let call it recursively to get params and weights
        if type(module) in [
            torch.nn.modules.container.Container,
            torch.nn.modules.container.Sequential
        ]:
            modstr = torch_summarize(module)
        else:
            modstr = module.__repr__()
        modstr = _addindent(modstr, 2)

        params = sum([np.prod(p.size()) for p in module.parameters()])
        weights = tuple([tuple(p.size()) for p in module.parameters()])

        tmpstr += '  (' + key + '): ' + modstr 
        if show_weights:
            tmpstr += ', weights={}'.format(weights)
        if show_parameters:
            tmpstr +=  ', parameters={}'.format(params)
        tmpstr += '\n'   

    tmpstr = tmpstr + ')'
    return tmpstr

# multi indexing of values in a dictionary combined in a result dictionary. 
def create_data_variables(dct, idx):
    result_dct = {}
    for x in dct.keys():
        result_dct[x] = dct[x][idx]
    return result_dct
        
# Quantification dataset for all data 
class HRMASDataset_Type_A(Dataset):
    def __init__(self, dct):
        super(HRMASDataset_Type_A, self).__init__()
        self.data_dct = dct
    def __len__(self):
        return self.data_dct["spectra"].shape[0]
    def __getitem__(self, idx):
        return (self.data_dct["spectra"][idx], self.data_dct["ppm_spectra"][idx], self.data_dct["quant"][idx], self.data_dct["quant_availability"][idx], self.data_dct["class_labels"][idx])

# Quantification dataset for all data 
class HRMASDataset_Type_B(Dataset):
    def __init__(self, dct):
        super(HRMASDataset_Type_B, self).__init__()
        self.data_dct = dct
    def __len__(self):
        return self.data_dct["spectra"].shape[0]
    def __getitem__(self, idx):
        return (self.data_dct["spectra"][idx], self.data_dct["ppm_spectra"][idx], self.data_dct["quant"][idx], self.data_dct["class_labels"][idx])

# changed and used from a MIT licensed repo on github
# reference: https://github.com/Bjarten/early-stopping-pytorch/blob/master/pytorchtools.py
class EarlyStopping:
    """Early stops the training if validation loss doesn't improve after a given patience."""
    def __init__(self, patience=7, verbose=False, delta=0):
        """
        Args:
            patience (int): How long to wait after last time validation loss improved.
                            Default: 7
            verbose (bool): If True, prints a message for each validation loss improvement. 
                            Default: False
            delta (float): Minimum change in the monitored quantity to qualify as an improvement.
                            Default: 0
        """
        self.patience = patience
        self.verbose = verbose
        self.counter = 0
        self.best_score = None
        self.early_stop = False
        self.val_loss_min = np.Inf
        self.delta = delta

    def __call__(self, val_loss):

        score = -val_loss

        if self.best_score is None:
            self.best_score = score
        elif score < self.best_score + self.delta:
            self.counter += 1
            if self.counter >= self.patience:
                self.early_stop = True
        else:
            self.best_score = score
            self.counter = 0
This criterion is a implemenation of Focal Loss, which is proposed in 
         Focal Loss for Dense Object Detection.
             
             Loss(x, class) = - \alpha (1-softmax(x)[class])^gamma \log(softmax(x)[class])
     
         The losses are averaged across observations for each minibatch.
         Args:
             alpha(1D Tensor, Variable) : the scalar factor for this criterion
             gamma(float, double) : gamma > 0; reduces the relative loss for well-classiﬁed examples (p > .5), 
                                    putting more focus on hard, misclassiﬁed examples
             size_average(bool): size_average(bool): By default, the losses are averaged over observations for each minibatch.
                                 However, if the field size_average is set to False, the losses are
                                 instead summed for each minibatch. # print(N) #print(class_mask) #print('probs size= {}'.format(probs.size())) #print(probs) #print('-----bacth_loss------') #print(batch_loss) # summarize Pytorch model Summarizes torch model by showing trainable parameters and weights. # if it contains layers let call it recursively to get params and weights # multi indexing of values in a dictionary combined in a result dictionary. # Quantification dataset for all data # Quantification dataset for all data # changed and used from a MIT licensed repo on github # reference: https://github.com/Bjarten/early-stopping-pytorch/blob/master/pytorchtools.py Early stops the training if validation loss doesn't improve after a given patience. Args:
             patience (int): How long to wait after last time validation loss improved.
                             Default: 7
             verbose (bool): If True, prints a message for each validation loss improvement. 
                             Default: False
             delta (float): Minimum change in the monitored quantity to qualify as an improvement.
                             Default: 0