# Path: detection/evaluation/map.py
# def calc_map(opt, video_scores, video_names, groundtruth_dir, iou_thresholds):
#   """Get mAP (action) for IoU 0.1, 0.3 and 0.5."""
#   activity_threshold = 0.4
#   num_videos = len(video_scores)
#   video_files = [name + '.txt' for name in video_names]
# 
#   v_props = []
#   for i in range(num_videos):
#     # video_name = video_names[i]
#     scores = video_scores[i]
#     segments = get_segments(scores, activity_threshold)
# 
#     prop = []
#     for segment in segments:
#       start, end, cls, score = segment
#       # start, end are indices of clips. Transform to frame index.
#       start_index = start * opt.step_size * opt.downsample
#       end_index = (
#           (end - 1) * opt.step_size + opt.n_frames) * opt.downsample - 1
#       prop.append([cls, start_index, end_index, score, video_files[i]])
#     v_props.append(prop)
# 
#   # Run evaluation on different IoU thresholds.
#   mean_aps = []
#   for iou in iou_thresholds:
#     mean_ap = process(v_props, video_files, groundtruth_dir, iou)
#     mean_aps.append(mean_ap)
#   return mean_aps


import argparse
import os
import utils
import utils.logging as logging
from .get_dataloader import *
from .get_model import *
from .evaluation.map import calc_map
      lrs = model.lr_decay()
      logger.log('***** lr decay *****: {}'.format(lrs))


def test(opt, model, dataloader):
  # Logging
  logger = logging.Logger(opt.ckpt_path, opt.split)
  stats = logging.Statistics(opt.ckpt_path, opt.split)
  logger.log(opt)

  model.load(opt.load_ckpt_paths, opt.load_opts, opt.load_epoch)
  all_scores = []
  video_names = []
  for step, data in enumerate(dataloader, 1):
    inputs, label, vid_name = data
    info_acc, logits, scores = model.test(inputs, label, opt.timestep)

    all_scores.append(scores)
    video_names.append(vid_name[0])
    update = stats.update(logits.shape[0], info_acc)
    if utils.is_due(step, opt.print_every):
      utils.info('step {}/{}: {}'.format(step, len(dataloader), update))

  logger.log('[Summary] {}'.format(stats.summarize()))

  # Evaluate
  iou_thresholds = [0.1, 0.3, 0.5]
  groundtruth_dir = os.path.join(opt.dset_path, opt.dset, 'groundtruth',
                                 'validation/cross-subject')
  assert os.path.exists(groundtruth_dir), '{} does not exist'.format(groundtruth_dir)

# Path: detection/evaluation/map.py
# def calc_map(opt, video_scores, video_names, groundtruth_dir, iou_thresholds):
#   """Get mAP (action) for IoU 0.1, 0.3 and 0.5."""
#   activity_threshold = 0.4
#   num_videos = len(video_scores)
#   video_files = [name + '.txt' for name in video_names]
# 
#   v_props = []
#   for i in range(num_videos):
#     # video_name = video_names[i]
#     scores = video_scores[i]
#     segments = get_segments(scores, activity_threshold)
# 
#     prop = []
#     for segment in segments:
#       start, end, cls, score = segment
#       # start, end are indices of clips. Transform to frame index.
#       start_index = start * opt.step_size * opt.downsample
#       end_index = (
#           (end - 1) * opt.step_size + opt.n_frames) * opt.downsample - 1
#       prop.append([cls, start_index, end_index, score, video_files[i]])
#     v_props.append(prop)
# 
#   # Run evaluation on different IoU thresholds.
#   mean_aps = []
#   for iou in iou_thresholds:
#     mean_ap = process(v_props, video_files, groundtruth_dir, iou)
#     mean_aps.append(mean_ap)
#   return mean_aps


# Path: detection/run.py
import argparse
import os
import utils
import utils.logging as logging
from .get_dataloader import *
from .get_model import *
from .evaluation.map import calc_map

      lrs = model.lr_decay()
      logger.log('***** lr decay *****: {}'.format(lrs))


def test(opt, model, dataloader):
  # Logging
  logger = logging.Logger(opt.ckpt_path, opt.split)
  stats = logging.Statistics(opt.ckpt_path, opt.split)
  logger.log(opt)

  model.load(opt.load_ckpt_paths, opt.load_opts, opt.load_epoch)
  all_scores = []
  video_names = []
  for step, data in enumerate(dataloader, 1):
    inputs, label, vid_name = data
    info_acc, logits, scores = model.test(inputs, label, opt.timestep)

    all_scores.append(scores)
    video_names.append(vid_name[0])
    update = stats.update(logits.shape[0], info_acc)
    if utils.is_due(step, opt.print_every):
      utils.info('step {}/{}: {}'.format(step, len(dataloader), update))

  logger.log('[Summary] {}'.format(stats.summarize()))

  # Evaluate
  iou_thresholds = [0.1, 0.3, 0.5]
  groundtruth_dir = os.path.join(opt.dset_path, opt.dset, 'groundtruth',
                                 'validation/cross-subject')
  assert os.path.exists(groundtruth_dir), '{} does not exist'.format(groundtruth_dir)

  mean_aps = calc_map(opt, all_scores, video_names, groundtruth_dir, iou_thresholds)