# 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 sys, os
from utils.squeeze import get_squeezer_by_name
from utils.parameter_parser import parse_params
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


class FeatureSqueezingRC:
    def __init__(self, keras_model, rc_name):
        # Example of rc_name: FeatureSqueezing?squeezer=bit_depth_1
        self.model_predict = lambda x: keras_model.predict(x)
        subject, params = parse_params(rc_name)
        assert subject == 'FeatureSqueezing'

        if params.has_key('squeezer'):
            self.filter = get_squeezer_by_name(params['squeezer'], 'python')
        elif params.has_key('squeezers'):
            squeezer_names = params['squeezers'].split(',')

# 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: robustness/feature_squeezing.py
import sys, os
from utils.squeeze import get_squeezer_by_name
from utils.parameter_parser import parse_params

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


class FeatureSqueezingRC:
    def __init__(self, keras_model, rc_name):
        # Example of rc_name: FeatureSqueezing?squeezer=bit_depth_1
        self.model_predict = lambda x: keras_model.predict(x)
        subject, params = parse_params(rc_name)
        assert subject == 'FeatureSqueezing'

        if params.has_key('squeezer'):
            self.filter = get_squeezer_by_name(params['squeezer'], 'python')
        elif params.has_key('squeezers'):
            squeezer_names = params['squeezers'].split(',')

            self.filters = [ get_squeezer_by_name(squeezer_name, 'python') for squeezer_name in squeezer_names ]