# Path: maxdiv/libmaxdiv_wrapper.py
# class detection_t(Structure):
# class pointwise_proposal_params_t(Structure):
# class erph_params_t(Structure):
# class embedding_params_t(Structure):
# class detrending_params_t(Structure):
# class projection_params_t(Structure):
# class preproc_params_t(Structure):
# class maxdiv_params_t(Structure):
# class _LibMaxDiv(object):
#     def __init__(self, library):
#     def _register_func(self, funcName, paramtypes, paramflags, errcheck = None):
#     def _errcheck_compile_pipeline(result, func, args):
# def _search_libmaxdiv():
# def maxdiv(X, method = 'gaussian_cov', num_intervals = 1, proposals = 'dense', **kwargs):
# def maxdiv_exec(X, params, num_intervals = 1):
# def maxdiv_score_intervals(X, params, intervals):
#         X = X.reshape((1, len(X)))
#                     X = preproc.local_linear_regression(X)
#                     X = preproc.deseasonalize_ft(X)
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])


import sys
import argparse
import numpy as np
from maxdiv.libmaxdiv_wrapper import maxdiv_exec, libmaxdiv, maxdiv_params_t, enums
""" Runs the MDI algorithm on CNN features extracted from a video an reduced to 16 dimensions using PCA. """

sys.path.append('../..')



if __name__ == '__main__':

    parser = argparse.ArgumentParser(description = 'Runs the MDI algorithm on pre-computed CNN features from a video and returns a list of detections.', formatter_class = argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('features', type = str, help = 'Path to a numpy file containing the pre-computed features as 5-dimensional array (time, x, y, 1, channels).')
    parser.add_argument('--proposals', action = 'store_true', default = False, help = 'Use interval proposals to speed up computations.')
    parser.add_argument('--divergence', type = str, choices = ['KL_UNBIASED', 'KL_I_OMEGA', 'CROSS_ENTROPY'], default = 'KL_UNBIASED', help = 'Divergence measure to be used.')
    parser.add_argument('--num', type = int, default = 5, help = 'Number of detections to be returned.')
    args = parser.parse_args()
    
    data = np.load(args.features)

    params = maxdiv_params_t()
    libmaxdiv.maxdiv_init_params(params)
    if args.divergence.startswith('KL_'):
        params.divergence = enums['MAXDIV_KL_DIVERGENCE']

# Path: maxdiv/libmaxdiv_wrapper.py
# class detection_t(Structure):
# class pointwise_proposal_params_t(Structure):
# class erph_params_t(Structure):
# class embedding_params_t(Structure):
# class detrending_params_t(Structure):
# class projection_params_t(Structure):
# class preproc_params_t(Structure):
# class maxdiv_params_t(Structure):
# class _LibMaxDiv(object):
#     def __init__(self, library):
#     def _register_func(self, funcName, paramtypes, paramflags, errcheck = None):
#     def _errcheck_compile_pipeline(result, func, args):
# def _search_libmaxdiv():
# def maxdiv(X, method = 'gaussian_cov', num_intervals = 1, proposals = 'dense', **kwargs):
# def maxdiv_exec(X, params, num_intervals = 1):
# def maxdiv_score_intervals(X, params, intervals):
#         X = X.reshape((1, len(X)))
#                     X = preproc.local_linear_regression(X)
#                     X = preproc.deseasonalize_ft(X)
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])
#         X = X.reshape((1, len(X)))
#         X = X.filled(np.nan)
#     X = np.require(X if isSpatioTemporal else X.T, np.float32 if maxdiv_scalar == c_float else np.float64, ['C_CONTIGUOUS'])


# Path: experiments/VideoDetection/visor_detect.py
import sys
import argparse
import numpy as np
from maxdiv.libmaxdiv_wrapper import maxdiv_exec, libmaxdiv, maxdiv_params_t, enums

""" Runs the MDI algorithm on CNN features extracted from a video an reduced to 16 dimensions using PCA. """

sys.path.append('../..')



if __name__ == '__main__':

    parser = argparse.ArgumentParser(description = 'Runs the MDI algorithm on pre-computed CNN features from a video and returns a list of detections.', formatter_class = argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('features', type = str, help = 'Path to a numpy file containing the pre-computed features as 5-dimensional array (time, x, y, 1, channels).')
    parser.add_argument('--proposals', action = 'store_true', default = False, help = 'Use interval proposals to speed up computations.')
    parser.add_argument('--divergence', type = str, choices = ['KL_UNBIASED', 'KL_I_OMEGA', 'CROSS_ENTROPY'], default = 'KL_UNBIASED', help = 'Divergence measure to be used.')
    parser.add_argument('--num', type = int, default = 5, help = 'Number of detections to be returned.')
    args = parser.parse_args()
    
    data = np.load(args.features)

    params = maxdiv_params_t()
    libmaxdiv.maxdiv_init_params(params)
    if args.divergence.startswith('KL_'):
        params.divergence = enums['MAXDIV_KL_DIVERGENCE']

        params.kl_mode = enums['MAXDIV_{}'.format(args.divergence)]