| |
| from scipy.stats import pearsonr, spearmanr |
| import torch.nn.functional as F |
| import torch |
| from scipy.stats import kendalltau |
| import scipy.stats as stats |
|
|
|
|
| def nash_sutcliffe_efficiency(observed, predicted): |
| |
| observed = observed.cpu() |
| predicted = predicted.cpu() |
|
|
| |
| numerator = torch.sum((observed - predicted) ** 2) |
| denominator = torch.sum((observed - torch.mean(observed)) ** 2) |
|
|
| |
| nse = 1 - (numerator / denominator) |
| return nse.item() |
|
|
| def pearson_correlation(y_true, y_pred): |
| y_true = y_true.view(-1).cpu().numpy() |
| y_pred = y_pred.view(-1).cpu().numpy() |
| |
| return pearsonr(y_true, y_pred)[0] |
|
|
| def spearman_correlation(y_true, y_pred): |
| y_true = y_true.view(-1).cpu().numpy() |
| y_pred = y_pred.view(-1).cpu().numpy() |
| |
| return spearmanr(y_true, y_pred).correlation |
|
|
| def mse(y_true, y_pred): |
| |
| y_true = y_true.cpu() |
| y_pred = y_pred.cpu() |
| |
| return torch.mean((y_true - y_pred) ** 2).item() |
|
|
| def mae(y_true, y_pred): |
| |
| y_true = y_true.cpu() |
| y_pred = y_pred.cpu() |
| |
| return torch.mean(torch.abs(y_true - y_pred)).item() |
|
|
| def percentage_error(y_true, y_pred): |
| |
| y_true = y_true.cpu() |
| y_pred = y_pred.cpu() |
| |
| return 100 * torch.mean((y_pred - y_true) / (y_true + 1e-6)).item() |
|
|
| def percentage_bias(y_true, y_pred): |
| |
| y_true = y_true.cpu() |
| y_pred = y_pred.cpu() |
|
|
| return 100 * torch.sum(y_pred - y_true) / (torch.sum(y_true) + 1e-6) |
|
|
| def kendall_tau(y_true, y_pred): |
| y_true = y_true.view(-1).cpu().numpy() |
| y_pred = y_pred.view(-1).cpu().numpy() |
| |
| return kendalltau(y_true, y_pred).correlation |
|
|
| def r2_score(y_true, y_pred): |
| |
| y_true = y_true.cpu() |
| y_pred = y_pred.cpu() |
|
|
| ss_total = torch.sum((y_true - torch.mean(y_true)) ** 2) |
| ss_residual = torch.sum((y_true - y_pred) ** 2) |
| |
| return 1 - (ss_residual / (ss_total + 1e-6)).item() |
|
|
| def spatial_correlation(y_true, y_pred): |
| |
| y_true_flat = y_true.view(-1).cpu() |
| y_pred_flat = y_pred.view(-1).cpu() |
|
|
| |
| numerator = torch.sum(y_pred_flat * y_true_flat) |
|
|
| |
| denominator = torch.sqrt(torch.sum(y_pred_flat ** 2) * torch.sum(y_true_flat ** 2)) |
|
|
| |
| correlation = numerator / (denominator) |
|
|
| return correlation.item() |