# Path: utils/squeeze.py
# def get_squeezer_by_name(name, func_type):
#     squeezer_list = ['none',
#                      'bit_depth_random',
#                      'bit_depth',
#                      'binary_filter',
#                      'binary_random_filter',
#                      'adaptive_binarize',
#                      'otsu_binarize',
#                      'median_filter',
#                      'median_random_filter',
#                      'median_random_size_filter',
#                      'non_local_means_bw',
#                      'non_local_means_color',
#                      'adaptive_bilateral_filter',
#                      'bilateral_filter',
#                      'magnet_mnist',
#                      'magnet_cifar10',
#                     ]
# 
#     for squeezer_name in squeezer_list:
#         if name.startswith(squeezer_name):
#             func_name = "%s_py" % squeezer_name if func_type=='python' else "%s_tf" % squeezer_name
#             params_str = name[len(squeezer_name):]
# 
#             # Return a list
#             args = parse_params(params_str)
#             # print ("params_str: %s, args: %s" % (params_str, args))
# 
#             return lambda x: globals()[func_name](*([x]+args))
# 
#     raise Exception('Unknown squeezer name: %s' % name)
# 
# Path: utils/parameter_parser.py
# def parse_params(params_str):
#     if '?' not in params_str:
#         return params_str, {}
#     subject, params_str = params_str.split('?')
#     params = urlparse.parse_qs(params_str)
#     params = dict( (k, v.lower() if len(v)>1 else v[0] ) for k,v in params.items())
# 
#     # Data type conversion.
#     integer_parameter_names = ['batch_size', 'max_iterations', 'num_classes', 'max_iter', 'nb_iter', 'max_iter_df']
#     for k,v in params.items():
#         if k in integer_parameter_names:
#             params[k] = int(v)
#         elif v == 'true':
#             params[k] = True
#         elif v == 'false':
#             params[k] = False
#         elif v == 'inf':
#             params[k] = np.inf
#         elif isfloat(v):
#             params[k] = float(v)
# 
#     return subject, params


import sklearn
import numpy as np
import operator
import functools
import pdb
import random
import sys, os
from sklearn.metrics import roc_curve, auc
from scipy.stats import entropy
from keras.models import Model
from utils.squeeze import get_squeezer_by_name
from utils.parameter_parser import parse_params
from sklearn.preprocessing import normalize
        from utils.output import write_to_csv
                layer_id = np.argmax(series)
                print ("Best: Metric-%s at Layer-%d, normalized by %s" % (distance_metric_name, layer_id, normalizer))
                ret.append([layer_id, normalizer, distance_metric_name])

            draw_plot(xs, series_list, label_list, "./%s_%s.png" % (self.name_prefix, normalizer))

        return ret

    def calculate_distance_max(self, val_orig, vals_squeezed, metric_name):
        distance_func = self.get_metric_by_name(metric_name)

        dist_array = []
        for val_squeezed in vals_squeezed:
            dist = distance_func(val_orig, val_squeezed)
            dist_array.append(dist)

        dist_array = np.array(dist_array)
        return np.max(dist_array, axis=0)

    def get_distance(self, X1, X2=None):
        layer_id, normalizer_name, metric_name, squeezers_name = self.get_config()

        normalize_func = self.get_normalizer_by_name(normalizer_name)
        input_to_normalized_output = lambda x: normalize_func(reshape_2d(self.eval_layer_output(x, layer_id)))

        val_orig_norm = input_to_normalized_output(X1)

        if X2 is None:
            vals_squeezed = []
            for squeezer_name in squeezers_name:

# Path: utils/squeeze.py
# def get_squeezer_by_name(name, func_type):
#     squeezer_list = ['none',
#                      'bit_depth_random',
#                      'bit_depth',
#                      'binary_filter',
#                      'binary_random_filter',
#                      'adaptive_binarize',
#                      'otsu_binarize',
#                      'median_filter',
#                      'median_random_filter',
#                      'median_random_size_filter',
#                      'non_local_means_bw',
#                      'non_local_means_color',
#                      'adaptive_bilateral_filter',
#                      'bilateral_filter',
#                      'magnet_mnist',
#                      'magnet_cifar10',
#                     ]
# 
#     for squeezer_name in squeezer_list:
#         if name.startswith(squeezer_name):
#             func_name = "%s_py" % squeezer_name if func_type=='python' else "%s_tf" % squeezer_name
#             params_str = name[len(squeezer_name):]
# 
#             # Return a list
#             args = parse_params(params_str)
#             # print ("params_str: %s, args: %s" % (params_str, args))
# 
#             return lambda x: globals()[func_name](*([x]+args))
# 
#     raise Exception('Unknown squeezer name: %s' % name)
# 
# Path: utils/parameter_parser.py
# def parse_params(params_str):
#     if '?' not in params_str:
#         return params_str, {}
#     subject, params_str = params_str.split('?')
#     params = urlparse.parse_qs(params_str)
#     params = dict( (k, v.lower() if len(v)>1 else v[0] ) for k,v in params.items())
# 
#     # Data type conversion.
#     integer_parameter_names = ['batch_size', 'max_iterations', 'num_classes', 'max_iter', 'nb_iter', 'max_iter_df']
#     for k,v in params.items():
#         if k in integer_parameter_names:
#             params[k] = int(v)
#         elif v == 'true':
#             params[k] = True
#         elif v == 'false':
#             params[k] = False
#         elif v == 'inf':
#             params[k] = np.inf
#         elif isfloat(v):
#             params[k] = float(v)
# 
#     return subject, params


# Path: detections/feature_squeezing.py
import sklearn
import numpy as np
import operator
import functools
import pdb
import random
import sys, os
from sklearn.metrics import roc_curve, auc
from scipy.stats import entropy
from keras.models import Model
from utils.squeeze import get_squeezer_by_name
from utils.parameter_parser import parse_params
from sklearn.preprocessing import normalize
        from utils.output import write_to_csv

                layer_id = np.argmax(series)
                print ("Best: Metric-%s at Layer-%d, normalized by %s" % (distance_metric_name, layer_id, normalizer))
                ret.append([layer_id, normalizer, distance_metric_name])

            draw_plot(xs, series_list, label_list, "./%s_%s.png" % (self.name_prefix, normalizer))

        return ret

    def calculate_distance_max(self, val_orig, vals_squeezed, metric_name):
        distance_func = self.get_metric_by_name(metric_name)

        dist_array = []
        for val_squeezed in vals_squeezed:
            dist = distance_func(val_orig, val_squeezed)
            dist_array.append(dist)

        dist_array = np.array(dist_array)
        return np.max(dist_array, axis=0)

    def get_distance(self, X1, X2=None):
        layer_id, normalizer_name, metric_name, squeezers_name = self.get_config()

        normalize_func = self.get_normalizer_by_name(normalizer_name)
        input_to_normalized_output = lambda x: normalize_func(reshape_2d(self.eval_layer_output(x, layer_id)))

        val_orig_norm = input_to_normalized_output(X1)

        if X2 is None:
            vals_squeezed = []
            for squeezer_name in squeezers_name:

                squeeze_func = self.get_squeezer_by_name(squeezer_name)