# Path: deep_verify/src/bounds/layer_bounds.py
# class BoundPropagation(object):
#   def propagate_bounds(self, network, in_bounds):


from deep_verify.src.bounds import layer_bounds
import interval_bound_propagation as ibp
import tensorflow as tf
from __future__ import print_function



def input_bounds(inputs, delta, lower_bound=0., upper_bound=1.,
                 preprocess_fn=None):
  """Calculates interval bounds on the network inputs.

  Args:
    inputs: 2D tensor of shape (batch_size, input_size), or 4D tensor of
      shape (batch_size, height, width, channels), of input examples.
    delta: Permitted perturbation on each input.
    lower_bound: Scalar - smallest permissible input (pixel) value.
    upper_bound: Scalar - largest permissible input (pixel) value.
    preprocess_fn: Optional function mapping tensor to tensor
      performing pre-processing on the raw inputs.

  Returns:
    `IntervalBounds` for the inputs, relative to `inputs`.
  """
  # Input range, according to permitted perturbation radius.
  if preprocess_fn:
    lb = preprocess_fn(tf.maximum(inputs - delta, lower_bound)) - inputs
    ub = preprocess_fn(tf.minimum(inputs + delta, upper_bound)) - inputs
  else:
    lb = tf.maximum(-delta, lower_bound - inputs)
    ub = tf.minimum(delta, upper_bound - inputs)
  return ibp.RelativeIntervalBounds(lb, ub, inputs)



# Path: deep_verify/src/bounds/layer_bounds.py
# class BoundPropagation(object):
#   def propagate_bounds(self, network, in_bounds):


# Path: deep_verify/src/bounds/naive_bounds.py
from deep_verify.src.bounds import layer_bounds
import interval_bound_propagation as ibp
import tensorflow as tf

from __future__ import print_function



def input_bounds(inputs, delta, lower_bound=0., upper_bound=1.,
                 preprocess_fn=None):
  """Calculates interval bounds on the network inputs.

  Args:
    inputs: 2D tensor of shape (batch_size, input_size), or 4D tensor of
      shape (batch_size, height, width, channels), of input examples.
    delta: Permitted perturbation on each input.
    lower_bound: Scalar - smallest permissible input (pixel) value.
    upper_bound: Scalar - largest permissible input (pixel) value.
    preprocess_fn: Optional function mapping tensor to tensor
      performing pre-processing on the raw inputs.

  Returns:
    `IntervalBounds` for the inputs, relative to `inputs`.
  """
  # Input range, according to permitted perturbation radius.
  if preprocess_fn:
    lb = preprocess_fn(tf.maximum(inputs - delta, lower_bound)) - inputs
    ub = preprocess_fn(tf.minimum(inputs + delta, upper_bound)) - inputs
  else:
    lb = tf.maximum(-delta, lower_bound - inputs)
    ub = tf.minimum(delta, upper_bound - inputs)
  return ibp.RelativeIntervalBounds(lb, ub, inputs)



class NaiveBoundPropagation(layer_bounds.BoundPropagation):