code: def calc_loss(
      attack_data,
      forecasts,
      attack_idx,
      target_items,
      quantiles=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
  ):
      testset_size = sum(
          [
              attack_data[i].true_future_target.shape[0]
              for i in range(len(attack_data))
          ]
      )
      mse = {
          key: np.zeros(
              (testset_size, len(attack_idx), len(target_items))
          )
          for key in forecasts.keys()
      }
      mape = {
          key: np.zeros(
              (testset_size, len(attack_idx), len(target_items))
          )
          for key in forecasts.keys()
      }
      ql = {
          key: np.zeros(
              (len(quantiles), testset_size, 1, len(target_items))
          )
          for key in forecasts.keys()
      }
      testset_idx = 0
  
      for i in tqdm(range(len(attack_data))):
          true_future_target = attack_data[i].true_future_target
          batch_size = true_future_target.shape[0]
  
          for attack_type in forecasts.keys():
              if (
                  true_future_target[:, attack_idx][..., target_items]
                  != 0
              ).prod() == 0:
                  mape[attack_type][
                      testset_idx : testset_idx + batch_size
                  ] = np.abs(
                      forecasts[attack_type][i][:, :, attack_idx][
                          ..., target_items
                      ].mean(1)
                      - true_future_target[:, attack_idx][
                          ..., target_items
                      ]
                  )
                  mse[attack_type][
                      testset_idx : testset_idx + batch_size
                  ] = (
                      forecasts[attack_type][i][:, :, attack_idx][
                          ..., target_items
                      ].mean(1)
                      - true_future_target[:, attack_idx][
                          ..., target_items
                      ]
                  ) ** 2
                  for j, quantile in enumerate(quantiles):
                      pred = np.quantile(
                          a=forecasts[attack_type][i][
                              :, :, attack_idx, target_items
                          ],
                          q=quantile,
                          axis=1,
                      )
                      true = true_future_target[:, attack_idx][
                          ..., target_items
                      ]
                      ql[attack_type][
                          j, testset_idx : testset_idx + batch_size
                      ] = quantile_loss(true, pred, quantile)
              else:
                  mape[attack_type][
                      testset_idx : testset_idx + batch_size
                  ] = np.abs(
                      forecasts[attack_type][i][:, :, attack_idx][
                          ..., target_items
                      ].mean(1)
                      / true_future_target[:, attack_idx][
                          ..., target_items
                      ]
                      - 1
                  )
                  mse[attack_type][
                      testset_idx : testset_idx + batch_size
                  ] = (
                      mape[attack_type][
                          testset_idx : testset_idx + batch_size
                      ]
                      ** 2
                  )
                  for j, quantile in enumerate(quantiles):
                      pred = np.quantile(
                          a=forecasts[attack_type][i][:, :, attack_idx][
                              ..., target_items
                          ],
                          q=quantile,
                          axis=1,
                      )
                      true = true_future_target[:, attack_idx][
                          ..., target_items
                      ]
                      ql[attack_type][
                          j, testset_idx : testset_idx + batch_size
                      ] = quantile_loss(true, pred, quantile)
          testset_idx += batch_size
  
      return mse, mape, ql
  
imports: import json
  import pickle
  import random
  import torch
  import numpy
  import pandas
  from tqdm.auto import tqdm
  from typing import Iterator
  from gluonts.dataset.common import DataEntry, Dataset, ListDataset
label_desc: Use image node_id 9 for calling a global function with example usage: calc_loss(attack_data, forecasts, attack_idx, target_items, quantiles) and returns: mse,  mape,  ql