def salt_pepper_noise(vector, fraction=0.05, salt_vs_pepper=0.5,
                      low_val=None, high_val=None, seed=None):
    """Add salt and pepper noise to vector.

    Salt and pepper noise replaces random elements in ``vector`` with
    ``low_val`` or ``high_val``.

    Parameters
    ----------
    vector : element of `TensorSpace` or `ProductSpace`
        The vector that noise should be added to.
    fraction : float, optional
        The propotion of the elements in ``vector`` that should be converted
        to noise.
    salt_vs_pepper : float, optional
        Relative abundance of salt (high) vs pepper (low) noise. A high value
        means more salt than pepper noise.
    low_val : float, optional
        The "pepper" color in the noise.
        Default: minimum value of ``vector``. For product spaces the minimum
        value per subspace is taken.
    high_val : float, optional
        The "salt" value in the noise.
        Default: maximuim value of ``vector``. For product spaces the maximum
        value per subspace is taken.
    seed : int, optional
        Random seed to use for generating the noise.
        For ``None``, use the current seed.

    Returns
    -------
    salt_pepper_noise : ``vector.space`` element
        ``vector`` with salt and pepper noise.

    See Also
    --------
    white_noise
    poisson_noise
    uniform_noise
    """
    from odl.space import ProductSpace

    # Validate input parameters
    fraction, fraction_in = float(fraction), fraction
    if not (0 <= fraction <= 1):
        raise ValueError('`fraction` ({}) should be a float in the interval '
                         '[0, 1]'.format(fraction_in))

    salt_vs_pepper, salt_vs_pepper_in = float(salt_vs_pepper), salt_vs_pepper
    if not (0 <= salt_vs_pepper <= 1):
        raise ValueError('`salt_vs_pepper` ({}) should be a float in the '
                         'interval [0, 1]'.format(salt_vs_pepper_in))

    with NumpyRandomSeed(seed):
        if isinstance(vector.space, ProductSpace):
            values = [salt_pepper_noise(subintensity, fraction, salt_vs_pepper,
                                        low_val, high_val)
                      for subintensity in vector]
        else:
            # Extract vector of values
            values = vector.asarray().flatten()

            # Determine fill-in values if not given
            if low_val is None:
                low_val = np.min(values)
            if high_val is None:
                high_val = np.max(values)

            # Create randomly selected points as a subset of image.
            a = np.arange(vector.size)
            np.random.shuffle(a)
            salt_indices = a[:int(fraction * vector.size * salt_vs_pepper)]
            pepper_indices = a[int(fraction * vector.size * salt_vs_pepper):
                               int(fraction * vector.size)]

            values[salt_indices] = high_val
            values[pepper_indices] = -low_val
            values = values.reshape(vector.space.shape)

    return vector.space.element(values)
Add salt and pepper noise to vector.

    Salt and pepper noise replaces random elements in ``vector`` with
    ``low_val`` or ``high_val``.

    Parameters
    ----------
    vector : element of `TensorSpace` or `ProductSpace`
        The vector that noise should be added to.
    fraction : float, optional
        The propotion of the elements in ``vector`` that should be converted
        to noise.
    salt_vs_pepper : float, optional
        Relative abundance of salt (high) vs pepper (low) noise. A high value
        means more salt than pepper noise.
    low_val : float, optional
        The "pepper" color in the noise.
        Default: minimum value of ``vector``. For product spaces the minimum
        value per subspace is taken.
    high_val : float, optional
        The "salt" value in the noise.
        Default: maximuim value of ``vector``. For product spaces the maximum
        value per subspace is taken.
    seed : int, optional
        Random seed to use for generating the noise.
        For ``None``, use the current seed.

    Returns
    -------
    salt_pepper_noise : ``vector.space`` element
        ``vector`` with salt and pepper noise.

    See Also
    --------
    white_noise
    poisson_noise
    uniform_noise