jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def bin_power_table(self):
        """
        Displays a table with the signal power contained in every frequency bin
        
        The power is computed as the time integral of the signal, such as :

            $ P = \int_0^{t_{max}} sig(t) dt $
        
        __ Multiple SoundPack Method __
        The sounds are always normalized before computing the power. Because 
        the signal amplitude is normalized between -1 and 1, the power value 
        is adimentional, and can only be used to compare two sounds between  
        eachother.
        """
        # Bin power distribution table
        bin_strings = self.bin_strings
        integrals = []

        # for every sound in the SoundPack
        for sound in self.sounds:

            integral = []
            # for every frequency bin in the sound
            for f_bin in bin_strings:
                log_envelope, log_time = sound.bins[f_bin].normalize().log_envelope()
                integral.append(scipy.integrate.trapezoid(log_envelope, log_time))

            # a list of dict for every sound
            integrals.append(integral)

        # make the table
        table_data = np.array([list(bin_strings), *integrals]).transpose()
        sound_names = [sound.name for sound in self.sounds]

        print('___ Signal Power Frequency Bin Distribution ___ \n')
        print(tabulate(table_data, headers=['bin', *sound_names]))
//  [python]
def self_diffusivity_gk(self, start=0, stop=0, step=1):
        """
        Returns a self-diffusivity value using ``scipy.integrate.trapezoid``.
        Analysis must be run prior to computing self-diffusivity.

        Parameters
        ----------
        start : Optional[int]
            The first frame of ``self.results.timeseries``
            used for the calculation.
        stop : Optional[int]
            The frame of ``self.results.timeseries`` to stop at
            for the calculation, non-inclusive.
        step : Optional[int]
            Number of frames to skip between each frame used
            for the calculation.

        Returns
        -------
        `numpy.float64`
            The calculated self-diffusivity value for the analysis.
        """
        if not self._run_called:
            raise RuntimeError(
                "Analysis must be run prior to computing self-diffusivity"
            )

        stop = self.n_frames if stop == 0 else stop

        return (
            integrate.trapz(
                self.results.timeseries[start:stop:step],
                self.times[start:stop:step],
            )
            / self.dim_fac
        )
//  [python]
def self_diffusivity_gk(self, start=0, stop=0, step=1):
        """
        Returns a self-diffusivity value using ``scipy.integrate.trapezoid``.
        Analysis must be run prior to computing self-diffusivity.

        Parameters
        ----------
        start : Optional[int]
            The first frame of ``self.results.timeseries``
            used for the calculation.
        stop : Optional[int]
            The frame of ``self.results.timeseries`` to stop at
            for the calculation, non-inclusive.
        step : Optional[int]
            Number of frames to skip between each frame used
            for the calculation.

        Returns
        -------
        `numpy.float64`
            The calculated self-diffusivity value for the analysis.
        """
        if not self._run_called:
            raise RuntimeError(
                "Analysis must be run prior to computing self-diffusivity"
            )

        stop = self.n_frames if stop == 0 else stop

        return (
            integrate.trapezoid(
                self.results.timeseries[start:stop:step],
                self.times[start:stop:step],
            )
            / self.dim_fac
        )
//  [python]
def bin_power_hist(self):
        """
        Histogram of the frequency bin power for multiple sounds

        The power is computed as the time integral of the signal, such as :

            $ P = \int_0^{t_{max}} sig(t) dt $
        
        __ Multiple SoundPack Method __
        The sounds are always normalized before computing the power. Because 
        the signal amplitude is normalized between -1 and 1, the power value 
        is adimentional, and can only be used to compare two sounds between  
        eachother.
        """
        # Compute the bin powers
        bin_strings = self.bin_strings
        integrals = []

        # for every sound in the SoundPack
        for sound in self.sounds:

            integral = []
            # for every frequency bin in the sound
            for f_bin in bin_strings:
                log_envelope, log_time = sound.bins[f_bin].normalize().log_envelope()
                integral.append(np.trapz(log_envelope, log_time))

            # a list of dict for every sound
            integral = np.array(integral)
            integral /= np.max(integral)
            integrals.append(integral)

        # create the bar plotting vectors
        fig, ax = plt.subplots(figsize=(6, 6))

        # make the bar plot
        n = len(self.sounds)
        width = 0.8 / n
        # get nice colors
        cmap = matplotlib.cm.get_cmap('Set2')
        for i, sound in enumerate(self.sounds):
            x = np.arange(i * width, len(bin_strings) + i * width)
            y = integrals[i]
            if n < 8:
                color = cmap(i)
            else:
                color = None

            if i == n // 2:
                ax.bar(x, y, width=width, tick_label=list(bin_strings), label=sound.name, color=color)
            else:
                ax.bar(x, y, width=width, label=sound.name, color=color)
        ax.set_xlabel('frequency bin name')
        ax.set_ylabel('normalized power')
        plt.legend()
//  [python]
def bin_power_hist(self):
        """
        Histogram of the frequency bin power for multiple sounds

        The power is computed as the time integral of the signal, such as :

            $ P = \int_0^{t_{max}} sig(t) dt $
        
        __ Multiple SoundPack Method __
        The sounds are always normalized before computing the power. Because 
        the signal amplitude is normalized between -1 and 1, the power value 
        is adimentional, and can only be used to compare two sounds between  
        eachother.
        """
        # Compute the bin powers
        bin_strings = self.bin_strings
        integrals = []

        # for every sound in the SoundPack
        for sound in self.sounds:

            integral = []
            # for every frequency bin in the sound
            for f_bin in bin_strings:
                log_envelope, log_time = sound.bins[f_bin].normalize().log_envelope()
                integral.append(trapezoid(log_envelope, log_time))

            # a list of dict for every sound
            integral = np.array(integral)
            integral /= np.max(integral)
            integrals.append(integral)

        # create the bar plotting vectors
        fig, ax = plt.subplots(figsize=(6, 6))

        # make the bar plot
        n = len(self.sounds)
        width = 0.8 / n
        # get nice colors
        cmap = matplotlib.cm.get_cmap('Set2')
        for i, sound in enumerate(self.sounds):
            x = np.arange(i * width, len(bin_strings) + i * width)
            y = integrals[i]
            if n < 8:
                color = cmap(i)
            else:
                color = None

            if i == n // 2:
                ax.bar(x, y, width=width, tick_label=list(bin_strings), label=sound.name, color=color)
            else:
                ax.bar(x, y, width=width, label=sound.name, color=color)
        ax.set_xlabel('frequency bin name')
        ax.set_ylabel('normalized power')
        plt.legend()
//  [python]
def set_lambda_eff(self):
        """Set effective wavelength

        Notes
        -----

        Sets attribute lambda_eff
"""
        # Just use original grid; good enough.
        wave = self.wave
        response = self.response

        # Perform integration for numerator
        integrand_numer = np.log(wave) * response / wave
        numer = integrate.trapz(wave, integrand_numer)

        # Perform integration for denominator
        integrand_denom = response / wave
        denom = integrate.trapz(wave, integrand_denom)

        # Set effective wavelength
        self.lambda_eff = np.exp(numer / denom)

        return
//  [python]
def set_lambda_eff(self):
        """Set effective wavelength

        Notes
        -----

        Sets attribute lambda_eff
"""
        # Just use original grid; good enough.
        wave = self.wave
        response = self.response

        # Perform integration for numerator
        integrand_numer = np.log(wave) * response / wave
        numer = integrate.trapezoid(wave, integrand_numer)

        # Perform integration for denominator
        integrand_denom = response / wave
        denom = integrate.trapezoid(wave, integrand_denom)

        # Set effective wavelength
        self.lambda_eff = np.exp(numer / denom)

        return
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")

###############################################################################
try:
    from scipy import misc
    face = misc.face(gray=True)
except AttributeError:
    # Old versions of scipy have face in the top level package
    face = sp.face(gray=True)
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")

###############################################################################
try:
    from scipy import datasets
    face = datasets.face(gray=True)
except AttributeError:
    # Old versions of scipy have face in the top level package
    face = sp.face(gray=True)
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")
try:
    from scipy import misc
    face = misc.face(gray=True)
except AttributeError:
    # Old versions of scipy have face in the top level package
    face = sp.face(gray=True)
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")
try:
    from scipy import datasets
    face = datasets.face(gray=True)
except AttributeError:
    # Old versions of scipy have face in the top level package
    face = sp.face(gray=True)
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")

try:
    face = sp.face(gray=True)
except AttributeError:
    # Newer versions of scipy have face in misc
    from scipy import misc
    face = misc.face(gray=True)
//  [python]
if sp_version < (0, 12):
    raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
                   "thus does not include the scipy.misc.face() image.")

try:
    face = sp.face(gray=True)
except AttributeError:
    # Newer versions of scipy have face in misc
    from scipy import datasets
    face = datasets.face(gray=True)
//  [python]
def test_face():
    assert_equal(face().shape, (768, 1024, 3))
//  [python]
def test_face():
    from scipy.datasets import face
    assert_equal(face().shape, (768, 1024, 3))
//  [python]
def calc_sigma2p_Sigma(R, r, sigma2_nu, beta):
    """ Calculate the projected Jeans integration:
    .. math::
    sigma2_p(R) Sigma(R) = 2 * int_R^\intfy (1 - beta * R^2 /r^2) (nu(r) sigma2(r) r) / sqrt(r^2 - R^2) dr

    where:
    - R is the projected radius
    - Sigma(R) is the 3D light profile

    Parameters
    ----------
    R: array of M float
        The 2D radius in kpc
    r: array of N float
        The 3D radius in kpc
    sigma2_nu: array of N float
        The 3D velocity dispersion
    beta: array of N float
        The velocity anisotropy

    Returns
    -------
    sigma2p_Sigma: array of M float
        The projected velocity dispersion
    """
    dr = r[1] - r[0]
    R = R[:, None]
    r = r[None, :]
    sigma2_nu = sigma2_nu[None, :]
    rminR2 = r**2 - R**2
    beta = beta[None, :]

    with np.errstate(divide='ignore', invalid='ignore'):
        inte = (1 - beta * R**2 / r**2) * sigma2_nu * r
        inte = np.where(rminR2 > 0,  inte / np.sqrt(rminR2), 0)

    sigma2p_Sigma = 2 * integrate.simps(inte, axis=1, dx=dr)
    return sigma2p_Sigma
//  [python]
def calc_sigma2p_Sigma(R, r, sigma2_nu, beta):
    """ Calculate the projected Jeans integration:
    .. math::
    sigma2_p(R) Sigma(R) = 2 * int_R^\intfy (1 - beta * R^2 /r^2) (nu(r) sigma2(r) r) / sqrt(r^2 - R^2) dr

    where:
    - R is the projected radius
    - Sigma(R) is the 3D light profile

    Parameters
    ----------
    R: array of M float
        The 2D radius in kpc
    r: array of N float
        The 3D radius in kpc
    sigma2_nu: array of N float
        The 3D velocity dispersion
    beta: array of N float
        The velocity anisotropy

    Returns
    -------
    sigma2p_Sigma: array of M float
        The projected velocity dispersion
    """
    dr = r[1] - r[0]
    R = R[:, None]
    r = r[None, :]
    sigma2_nu = sigma2_nu[None, :]
    rminR2 = r**2 - R**2
    beta = beta[None, :]

    with np.errstate(divide='ignore', invalid='ignore'):
        inte = (1 - beta * R**2 / r**2) * sigma2_nu * r
        inte = np.where(rminR2 > 0,  inte / np.sqrt(rminR2), 0)

    sigma2p_Sigma = 2 * integrate.trapezoid(inte, axis=1, dx=dr)
    return sigma2p_Sigma
//  [python]
def __init__(self, frequencies, whitened_amplitude, par_dic_0, snr,
                 smooth=True):
        """
        Parameters
        ----------
        frequencies: 1d float array
            Frequencies in Hz.

        whitened_amplitude: array like frequencies
            |h| / sqrt(PSD), with arbitrary normalization, evaluated at
            `frequencies`. If there are multiple detectors, pass the
            root mean square over detectors.

        smooth: bool
            If True, will smooth the whitened amplitude as a function of
            frequency so it can be interpolated meaningfully (useful for
            estimating the amplitude at merger frequency).
        """
        if smooth:
            whitened_amplitude = scipy.signal.savgol_filter(
                whitened_amplitude, len(whitened_amplitude) // 100, 1)

        whitened_amplitude /= np.sqrt(
            scipy.integrate.trapz(whitened_amplitude**2, frequencies))

        mtot0 = par_dic_0['m1'] + par_dic_0['m2']
        chieff0 = gw_utils.chieff(par_dic_0['m1'],  par_dic_0['m2'],
                                   par_dic_0['s1z'],  par_dic_0['s2z'])

        self._fmerger_0 = get_f_merger(mtot0, chieff0)
        wht_amp_f_merger = np.interp(self._fmerger_0, frequencies,
                                     whitened_amplitude)

        self._fmerger_scale_0 = 2 * (snr * wht_amp_f_merger)**-2
//  [python]
def __init__(self, frequencies, whitened_amplitude, par_dic_0, snr,
                 smooth=True):
        """
        Parameters
        ----------
        frequencies: 1d float array
            Frequencies in Hz.

        whitened_amplitude: array like frequencies
            |h| / sqrt(PSD), with arbitrary normalization, evaluated at
            `frequencies`. If there are multiple detectors, pass the
            root mean square over detectors.

        smooth: bool
            If True, will smooth the whitened amplitude as a function of
            frequency so it can be interpolated meaningfully (useful for
            estimating the amplitude at merger frequency).
        """
        if smooth:
            whitened_amplitude = scipy.signal.savgol_filter(
                whitened_amplitude, len(whitened_amplitude) // 100, 1)

        whitened_amplitude /= np.sqrt(
            scipy.integrate.trapezoid(whitened_amplitude**2, frequencies))

        mtot0 = par_dic_0['m1'] + par_dic_0['m2']
        chieff0 = gw_utils.chieff(par_dic_0['m1'],  par_dic_0['m2'],
                                   par_dic_0['s1z'],  par_dic_0['s2z'])

        self._fmerger_0 = get_f_merger(mtot0, chieff0)
        wht_amp_f_merger = np.interp(self._fmerger_0, frequencies,
                                     whitened_amplitude)

        self._fmerger_scale_0 = 2 * (snr * wht_amp_f_merger)**-2
//  [python]
def __init__(
        self,
        longitudinal_curve: LongitudinalCurve,
        lateral_curve: LateralCurve,
        t_array: np.array,
        cost_coefficients: dict,
        s_dot_desired: float,
    ):
        """
        :param longitudinal_curve: longitudinal curve of trajectory
        :type longitudinal_curve: LongitudinalCurve
        :param lateral_curve: lateral curve of trajectory
        :type lateral_curve: LateralCurve
        :param t_array: time array of trajectory
        :type t_array: np.array
        :param cost_coefficients: cost coefficients to calculate total cost of trajectory
        :type cost_coefficients: dict
        :param s_dot_desired: desired longitudinal end velocity
        :type s_dot_desired: dict"""

        self.__t = t_array

        self.__s = longitudinal_curve.s
        self.__s_dot = longitudinal_curve.s_dot
        self.__s_ddot = longitudinal_curve.s_ddot
        self.__s_dddot = longitudinal_curve.s_dddot

        self.__d = lateral_curve.d
        self.__d_dot = lateral_curve.d_dot
        self.__d_ddot = lateral_curve.d_ddot
        self.__d_dddot = lateral_curve.d_dddot

        t_end = self.__t[-1]
        s_dot_end = self.__s_dot[-1]
        d_end = self.__d[-1]

        # calculate integral of jerk using trapezoidal rule
        jerk_integral_lateral_trajectory = integrate.trapz(
            y=self.__d_dddot ** 2, x=self.__t
        )
        jerk_integral_longitudinal_trajectory = integrate.trapz(
            y=self.__s_dddot ** 2, x=self.__t
        )

        # calculate lateral cost
        lateral_cost = (
            cost_coefficients["k_jerk"] * jerk_integral_lateral_trajectory
            + cost_coefficients["k_time"] * t_end
            + cost_coefficients["k_d"] * d_end ** 2
        )

        # calculate longitudinal cost
        longitudinal_cost = (
            cost_coefficients["k_jerk"] * jerk_integral_longitudinal_trajectory
            + cost_coefficients["k_time"] * t_end
            + cost_coefficients["k_s_dot"] * (s_dot_end - s_dot_desired) ** 2
        )

        # calculate total cost
        self.__cost = (
            cost_coefficients["k_lateral"] * lateral_cost
            + cost_coefficients["k_longitudinal"] * longitudinal_cost
        )
//  [python]
def __init__(
        self,
        longitudinal_curve: LongitudinalCurve,
        lateral_curve: LateralCurve,
        t_array: np.array,
        cost_coefficients: dict,
        s_dot_desired: float,
    ):
        """
        :param longitudinal_curve: longitudinal curve of trajectory
        :type longitudinal_curve: LongitudinalCurve
        :param lateral_curve: lateral curve of trajectory
        :type lateral_curve: LateralCurve
        :param t_array: time array of trajectory
        :type t_array: np.array
        :param cost_coefficients: cost coefficients to calculate total cost of trajectory
        :type cost_coefficients: dict
        :param s_dot_desired: desired longitudinal end velocity
        :type s_dot_desired: dict"""

        self.__t = t_array

        self.__s = longitudinal_curve.s
        self.__s_dot = longitudinal_curve.s_dot
        self.__s_ddot = longitudinal_curve.s_ddot
        self.__s_dddot = longitudinal_curve.s_dddot

        self.__d = lateral_curve.d
        self.__d_dot = lateral_curve.d_dot
        self.__d_ddot = lateral_curve.d_ddot
        self.__d_dddot = lateral_curve.d_dddot

        t_end = self.__t[-1]
        s_dot_end = self.__s_dot[-1]
        d_end = self.__d[-1]

        # calculate integral of jerk using trapezoidal rule
        jerk_integral_lateral_trajectory = integrate.trapezoid(
            y=self.__d_dddot ** 2, x=self.__t
        )
        jerk_integral_longitudinal_trajectory = integrate.trapezoid(
            y=self.__s_dddot ** 2, x=self.__t
        )

        # calculate lateral cost
        lateral_cost = (
            cost_coefficients["k_jerk"] * jerk_integral_lateral_trajectory
            + cost_coefficients["k_time"] * t_end
            + cost_coefficients["k_d"] * d_end ** 2
        )

        # calculate longitudinal cost
        longitudinal_cost = (
            cost_coefficients["k_jerk"] * jerk_integral_longitudinal_trajectory
            + cost_coefficients["k_time"] * t_end
            + cost_coefficients["k_s_dot"] * (s_dot_end - s_dot_desired) ** 2
        )

        # calculate total cost
        self.__cost = (
            cost_coefficients["k_lateral"] * lateral_cost
            + cost_coefficients["k_longitudinal"] * longitudinal_cost
        )
//  [python]
def test_probmap(self):
        image_tensor = tf.constant(scipy.misc.face())
        attention_tensor = np.eye(5)
        op = tfplot.contrib.probmap(attention_tensor, figsize=(4, 3))
        self._execute_plot_op(op, print_image=True)
//  [python]
def test_probmap(self):
        image_tensor = tf.constant(scipy.datasets.face())
        attention_tensor = np.eye(5)
        op = tfplot.contrib.probmap(attention_tensor, figsize=(4, 3))
        self._execute_plot_op(op, print_image=True)
//  [python]
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
//  [python]
def test_connect_regions():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        from scipy import datasets
        face = datasets.face(gray=True)
    for thr in (50, 150):
        mask = face > thr
        graph = img_to_graph(face, mask)
        assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
//  [python]
def test_connect_regions_with_grid():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    mask = face > 50
    graph = grid_to_graph(*face.shape, mask=mask)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])

    mask = face > 150
    graph = grid_to_graph(*face.shape, mask=mask, dtype=None)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
//  [python]
def test_connect_regions_with_grid():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import datasets
        face = datasets.face(gray=True)
    mask = face > 50
    graph = grid_to_graph(*face.shape, mask=mask)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])

    mask = face > 150
    graph = grid_to_graph(*face.shape, mask=mask, dtype=None)
    assert_equal(ndimage.label(mask)[1], connected_components(graph)[0])
//  [python]
def _downsampled_face():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        # Newer versions of scipy have face in misc
        from scipy import misc
        face = misc.face(gray=True)
    face = face.astype(np.float32)
    face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2]
            + face[1::2, 1::2])
    face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2]
            + face[1::2, 1::2])
    face = face.astype(np.float32)
    face /= 16.0
    return face
//  [python]
def _downsampled_face():
    try:
        face = sp.face(gray=True)
    except AttributeError:
        from scipy import datasets
        face = datasets.face(gray=True)
    face = face.astype(np.float32)
    face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2]
            + face[1::2, 1::2])
    face = (face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2]
            + face[1::2, 1::2])
    face = face.astype(np.float32)
    face /= 16.0
    return face
//  [python]
gray = scipy.misc.ascent()
color = scipy.misc.face()
//  [python]
gray = scipy.misc.ascent()
color = scipy.datasets.face()
//  [python]
def main():
    args = parse_args()

    if not os.path.exists(args.out_dir):
        os.makedirs(args.out_dir)

    target_layer_names = ['35']
    target_index = None

    # Prepare input image
    if args.img:
        img = cv2.imread(args.img, 1)
    else:
        img = misc.face()
    img = np.float32(cv2.resize(img, (224, 224))) / 255
    preprocessed_img = preprocess_image(img, args.cuda)

    model = vgg19(pretrained=True)
    if args.cuda:
        model.cuda()

    # Prediction
    output = model(preprocessed_img)
    pred_index = np.argmax(output.data.cpu().numpy())
    print('Prediction: {}'.format(IMAGENET_LABELS[pred_index]))

    # Compute vanilla gradient
    vanilla_grad = VanillaGrad(
        pretrained_model=model, cuda=args.cuda)
    vanilla_saliency = vanilla_grad(preprocessed_img, index=target_index)
    save_as_gray_image(vanilla_saliency, os.path.join(args.out_dir, 'vanilla_grad.jpg'))
    print('Saved vanilla gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided gradient
    guided_grad = GuidedBackpropGrad(
        pretrained_model=model, cuda=args.cuda)
    guided_saliency = guided_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_saliency, os.path.join(args.out_dir, 'guided_grad.jpg'))
    print('Saved guided backprop gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute smooth gradient
    smooth_grad = SmoothGrad(
        pretrained_model=model,
        cuda=args.cuda,
        n_samples=args.n_samples,
        magnitude=True)
    smooth_saliency = smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(smooth_saliency, os.path.join(args.out_dir, 'smooth_grad.jpg'))
    print('Saved smooth gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided smooth gradient
    guided_smooth_grad = GuidedBackpropSmoothGrad(
        pretrained_model=model,
        cuda=args.cuda,
        n_samples=args.n_samples,
        magnitude=True)
    guided_smooth_saliency = guided_smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_smooth_saliency, os.path.join(args.out_dir, 'guided_smooth_grad.jpg'))
    print('Saved guided backprop smooth gradient image')
//  [python]
def main():
    args = parse_args()

    if not os.path.exists(args.out_dir):
        os.makedirs(args.out_dir)

    target_layer_names = ['35']
    target_index = None

    # Prepare input image
    if args.img:
        img = cv2.imread(args.img, 1)
    else:
        img = scipy.datasets.face()
    img = np.float32(cv2.resize(img, (224, 224))) / 255
    preprocessed_img = preprocess_image(img, args.cuda)

    model = vgg19(pretrained=True)
    if args.cuda:
        model.cuda()

    # Prediction
    output = model(preprocessed_img)
    pred_index = np.argmax(output.data.cpu().numpy())
    print('Prediction: {}'.format(IMAGENET_LABELS[pred_index]))

    # Compute vanilla gradient
    vanilla_grad = VanillaGrad(
        pretrained_model=model, cuda=args.cuda)
    vanilla_saliency = vanilla_grad(preprocessed_img, index=target_index)
    save_as_gray_image(vanilla_saliency, os.path.join(args.out_dir, 'vanilla_grad.jpg'))
    print('Saved vanilla gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided gradient
    guided_grad = GuidedBackpropGrad(
        pretrained_model=model, cuda=args.cuda)
    guided_saliency = guided_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_saliency, os.path.join(args.out_dir, 'guided_grad.jpg'))
    print('Saved guided backprop gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute smooth gradient
    smooth_grad = SmoothGrad(
        pretrained_model=model,
        cuda=args.cuda,
        n_samples=args.n_samples,
        magnitude=True)
    smooth_saliency = smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(smooth_saliency, os.path.join(args.out_dir, 'smooth_grad.jpg'))
    print('Saved smooth gradient image')

    # Reload preprocessed image
    preprocessed_img = preprocess_image(img, args.cuda)

    # Compute guided smooth gradient
    guided_smooth_grad = GuidedBackpropSmoothGrad(
        pretrained_model=model,
        cuda=args.cuda,
        n_samples=args.n_samples,
        magnitude=True)
    guided_smooth_saliency = guided_smooth_grad(preprocessed_img, index=target_index)
    save_as_gray_image(guided_smooth_saliency, os.path.join(args.out_dir, 'guided_smooth_grad.jpg'))
    print('Saved guided backprop smooth gradient image')
//  [python]
def test_plot_with_kwargs(self):
        '''1.3 with kwargs'''

        attention_tensor = fake_attention()
        image_tensor = tf.constant(scipy.misc.face())

        # (a) default execution
        plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor])
        r = self._execute_plot_op(plot_op, print_image=True)
        self.assertEqual(test_util.hash_image(r), 'c2d64dedd4aa54218e6df95bfeb03bbc17bd17fa')

        # (b) override cmap and alpha
        plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor],
                              cmap='gray', alpha=0.8)
        r = self._execute_plot_op(plot_op, print_image=True)
        self.assertEqual(test_util.hash_image(r), '31c8029aed7bbafe37bb8c451a3220d573d2d0e0')
//  [python]
def test_plot_with_kwargs(self):
        '''1.3 with kwargs'''

        attention_tensor = fake_attention()
        image_tensor = tf.constant(scipy.datasets.face())

        # (a) default execution
        plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor])
        r = self._execute_plot_op(plot_op, print_image=True)
        self.assertEqual(test_util.hash_image(r), 'c2d64dedd4aa54218e6df95bfeb03bbc17bd17fa')

        # (b) override cmap and alpha
        plot_op = tfplot.plot(_overlay_attention, [attention_tensor, image_tensor],
                              cmap='gray', alpha=0.8)
        r = self._execute_plot_op(plot_op, print_image=True)
        self.assertEqual(test_util.hash_image(r), '31c8029aed7bbafe37bb8c451a3220d573d2d0e0')
//  [python]
getcontext().prec = 444

# Read an image.
im = scipy.misc.face(gray=True)
//  [python]
getcontext().prec = 444

# Read an image.
im = scipy.datasets.face(gray=True)
//  [python]
def test_plot_many(self):
        '''1.4 plot_many'''
        # make a fake batch
        batch_size = 3
        image_tensor = tf.constant(scipy.misc.face())
        try:
            attention_batch = tf.random.gamma([batch_size, 7, 7], alpha=0.3, seed=42)
        except AttributeError:  # legacy TF versions
            attention_batch = tf.random_gamma([batch_size, 7, 7], alpha=0.3, seed=42)

        image_batch = tf.tile(tf.expand_dims(image_tensor, 0),
                              [batch_size, 1, 1, 1], name='image_batch') # copy

        plot_op = tfplot.plot_many(_overlay_attention, [attention_batch, image_batch])
        r = self._execute_plot_op(plot_op, print_image=False)
        #for i in range(3): imgcat(r[i])
        self.assertEqual(r.shape, (3, 400, 400, 4))
//  [python]
def test_plot_many(self):
        '''1.4 plot_many'''
        # make a fake batch
        batch_size = 3
        image_tensor = tf.constant(scipy.datasets.face())
        try:
            attention_batch = tf.random.gamma([batch_size, 7, 7], alpha=0.3, seed=42)
        except AttributeError:  # legacy TF versions
            attention_batch = tf.random_gamma([batch_size, 7, 7], alpha=0.3, seed=42)

        image_batch = tf.tile(tf.expand_dims(image_tensor, 0),
                              [batch_size, 1, 1, 1], name='image_batch') # copy

        plot_op = tfplot.plot_many(_overlay_attention, [attention_batch, image_batch])
        r = self._execute_plot_op(plot_op, print_image=False)
        #for i in range(3): imgcat(r[i])
        self.assertEqual(r.shape, (3, 400, 400, 4))
//  [python]
def synthetic_ingestor():
    timestamp = time.time()
    run_bundle = event_model.compose_run()
    data = scipy.misc.face(True)
    field = "some_data"
    source = "synthetic_ingestor"
    frame_data_keys = {field: {"source": source, "dtype": "number", "shape": data.shape}}
    frame_stream_name = "primary"
    frame_stream_bundle = run_bundle.compose_descriptor(data_keys=frame_data_keys, name=frame_stream_name)
    yield "start", run_bundle.start_doc
    yield "descriptor", frame_stream_bundle.descriptor_doc
    yield "event", frame_stream_bundle.compose_event(data={field: data}, timestamps={field: timestamp})
    yield "stop", run_bundle.compose_stop()
//  [python]
def synthetic_ingestor():
    timestamp = time.time()
    run_bundle = event_model.compose_run()
    data = scipy.datasets.face()
    field = "some_data"
    source = "synthetic_ingestor"
    frame_data_keys = {field: {"source": source, "dtype": "number", "shape": data.shape}}
    frame_stream_name = "primary"
    frame_stream_bundle = run_bundle.compose_descriptor(data_keys=frame_data_keys, name=frame_stream_name)
    yield "start", run_bundle.start_doc
    yield "descriptor", frame_stream_bundle.descriptor_doc
    yield "event", frame_stream_bundle.compose_event(data={field: data}, timestamps={field: timestamp})
    yield "stop", run_bundle.compose_stop()
//  [python]
try:
    from skimage.restoration import denoise_tv_chambolle
except ImportError:
    # skimage < 0.12
    from skimage.filters import denoise_tv_chambolle

f = scipy.misc.face(gray=True)
//  [python]
try:
    from skimage.restoration import denoise_tv_chambolle
except ImportError:
    # skimage < 0.12
    from skimage.filters import denoise_tv_chambolle

f = scipy.datasets.face(gray=True)
//  [python]
plt.imshow(img)
plt.show()
print(img[0:3,0:7])
print(img)
img=scipy.misc.face()
//  [python]
plt.imshow(img)
plt.show()
print(img[0:3,0:7])
print(img)
img=scipy.datasets.face()
//  [python]
def test_ifft_basic(alg_func, *args, **kwargs):
    """Basic testing for IFFT algorithms to see if the final phase truly reproduces an initial target

    Creates an image target (the center of the scipy.misc.face() image), runs the alg_func on it, and plots the results
    for comparison by eye

    :param alg_func:
    :param args:
    :param kwargs:
    :return:
    """
    if 'mixing_ratio' in kwargs:
        intensity_correction = kwargs['mixing_ratio']
    else:
        intensity_correction = 1
    target = np.asarray(misc.face()[:, :, 0], np.float)
    x, y = _get_coordinate_arrays(target)
    shp = target.shape
    # x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
    # x, y = np.meshgrid(x, y)
    mask_size = 0.2
    mask = (x**2 + y**2) > (mask_size * np.min(shp))**2
    target[mask] = 0
    target /= np.sum(target)  # the target intensity is normalised to 1

    init_phase = np.zeros(target.shape)
    # Making an input field that focuses light on the target pattern reduces vortex creation and improves pattern
    input_field = np.ones(shp) * np.exp(1j * 2*mask_size*np.min(shp) * ((x/np.max(x))**2+(y/np.max(y))**2))
    kwargs['input_field'] = input_field
    phase = alg_func(init_phase, target, *args, **kwargs)
    output = old_div(np.fft.fftshift(np.fft.fft2(np.exp(1j * phase))), (np.prod(shp)))
    print(np.sum(np.abs(output)**2), np.sum(np.abs(output[~mask])**2), np.sum(np.abs(output[mask])**2))
    _errors = (target - np.abs(output)**2) / target
    errors = _errors[np.abs(_errors) != np.inf]
    avg = np.sqrt(np.mean(errors**2))

    fig, axs = plt.subplots(2, 2, sharey=True, sharex=True, gridspec_kw=dict(wspace=0.01))
    vmin, vmax = (np.min(target), np.max(target))
    axs[0, 0].imshow(target, vmin=vmin, vmax=vmax)
    axs[0, 0].set_title('Target')
    axs[1, 0].imshow(phase)
    axs[1, 0].set_title('Input Phase')
    vmin *= intensity_correction
    vmax *= intensity_correction
    axs[0, 1].imshow(np.abs(output)**2, vmin=vmin, vmax=vmax)
    axs[0, 1].set_title('Output')
    axs[1, 1].imshow(np.angle(output))
    axs[1, 1].set_title('Output Phase')
    fig.suptitle(r'$\sqrt{\sum\left(\frac{target-output}{target}\right)^2}=$%g' % avg)
    plt.show()

    return output, target
//  [python]
def test_ifft_basic(alg_func, *args, **kwargs):
    """Basic testing for IFFT algorithms to see if the final phase truly reproduces an initial target

    Creates an image target (the center of the scipy.misc.face() image), runs the alg_func on it, and plots the results
    for comparison by eye

    :param alg_func:
    :param args:
    :param kwargs:
    :return:
    """
    if 'mixing_ratio' in kwargs:
        intensity_correction = kwargs['mixing_ratio']
    else:
        intensity_correction = 1
    target = np.asarray(datasets.face()[:, :, 0], np.float)
    x, y = _get_coordinate_arrays(target)
    shp = target.shape
    # x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
    # x, y = np.meshgrid(x, y)
    mask_size = 0.2
    mask = (x**2 + y**2) > (mask_size * np.min(shp))**2
    target[mask] = 0
    target /= np.sum(target)  # the target intensity is normalised to 1

    init_phase = np.zeros(target.shape)
    # Making an input field that focuses light on the target pattern reduces vortex creation and improves pattern
    input_field = np.ones(shp) * np.exp(1j * 2*mask_size*np.min(shp) * ((x/np.max(x))**2+(y/np.max(y))**2))
    kwargs['input_field'] = input_field
    phase = alg_func(init_phase, target, *args, **kwargs)
    output = old_div(np.fft.fftshift(np.fft.fft2(np.exp(1j * phase))), (np.prod(shp)))
    print(np.sum(np.abs(output)**2), np.sum(np.abs(output[~mask])**2), np.sum(np.abs(output[mask])**2))
    _errors = (target - np.abs(output)**2) / target
    errors = _errors[np.abs(_errors) != np.inf]
    avg = np.sqrt(np.mean(errors**2))

    fig, axs = plt.subplots(2, 2, sharey=True, sharex=True, gridspec_kw=dict(wspace=0.01))
    vmin, vmax = (np.min(target), np.max(target))
    axs[0, 0].imshow(target, vmin=vmin, vmax=vmax)
    axs[0, 0].set_title('Target')
    axs[1, 0].imshow(phase)
    axs[1, 0].set_title('Input Phase')
    vmin *= intensity_correction
    vmax *= intensity_correction
    axs[0, 1].imshow(np.abs(output)**2, vmin=vmin, vmax=vmax)
    axs[0, 1].set_title('Output')
    axs[1, 1].imshow(np.angle(output))
    axs[1, 1].set_title('Output Phase')
    fig.suptitle(r'$\sqrt{\sum\left(\frac{target-output}{target}\right)^2}=$%g' % avg)
    plt.show()

    return output, target
//  [python]
logger.configure(os.path.realpath('.'))
face = scipy.misc.face()
//  [python]
logger.configure(os.path.realpath('.'))
face = scipy.datasets.face()
//  [python]
def test_ifft_smoothness(alg_func, *args, **kwargs):
    """Evaluates smoothness of calculated vs target pattern as a function of iteration in an IFFT algorithm

    Smoothness is defined as the sum of absolute difference over the area of interest. For most algorithms the area of
    interest is the whole plane, while for MRAF the area of interest is only the signal region

    :param alg_func:
    :param args:
    :param kwargs:
    :return:
    """
    target = np.asarray(misc.face()[:, :, 0], np.float)
    x, y = _get_coordinate_arrays(target)
    shp = target.shape
    # x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
    # x, y = np.meshgrid(x, y)
    mask = (x**2 + y**2) > (0.2 * np.min(shp))**2
    target[mask] = 0
    target /= np.sum(target)

    iterations = 60
    if 'iterations' in kwargs:
        iterations = kwargs['iterations']
    # The algorithms only return the final phase, so to evaluate the smoothness at each iteration, need to set the
    # algorithm to only run one step at a time
    kwargs['iterations'] = 1

    # Defining a mask and a mixing_ratio for calculating the smoothness later
    if alg_func == gerchberg_saxton:
        mask = np.ones(shp, dtype=np.bool)
        mixing_ratio = 1
    elif alg_func == mraf:
        x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
        x, y = np.meshgrid(x, y)
        signal_region_size = 0.5
        if 'signal_region_size' in kwargs:
            signal_region_size = kwargs['signal_region_size']
        mask = (x**2 + y**2) < (signal_region_size * np.min(shp))**2
        mixing_ratio = 0.4
        if 'mixing_ratio' in kwargs:
            mixing_ratio = kwargs['mixing_ratio']
    else:
        raise ValueError('Unrecognised algorithm')

    smth = []
    outputs = []
    for indx in range(iterations):
        init_phase = alg_func(0, target, *args, **kwargs)
        input_field = np.exp(1j * init_phase)
        kwargs['input_field'] = input_field
        output = old_div(np.fft.fftshift(np.fft.fft2(np.exp(1j * init_phase))), (np.prod(shp)))
        output_int = np.abs(output) ** 2
        # print(np.sum(np.abs(output_int)), np.sum(np.abs(output_int)[mask]))
        smth += [old_div(np.sum(np.abs(output_int - mixing_ratio*target)[mask]), np.sum(mask))]
        outputs += [output]

    fig = plt.figure(figsize=(old_div(8*shp[1],shp[0])*2, 8))
    gs = gridspec.GridSpec(1, 2)
    gs2 = gridspec.GridSpecFromSubplotSpec(5, 6, gs[0], 0.001, 0.001)
    reindex = np.linspace(0, iterations-1, 30)
    ax = None
    for indx, _gs in zip(reindex, gs2):
        indx = int(indx)
        ax = plt.subplot(_gs, sharex=ax, sharey=ax)
        ax.imshow(np.abs(outputs[indx]))
        ax.text(shp[1]/2., 0, '%d=%.3g' % (indx, smth[indx]), ha='center', va='top', color='w')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
    ax2 = plt.subplot(gs[1])
    ax2.semilogy(smth)
    return np.array(smth)
//  [python]
def test_ifft_smoothness(alg_func, *args, **kwargs):
    """Evaluates smoothness of calculated vs target pattern as a function of iteration in an IFFT algorithm

    Smoothness is defined as the sum of absolute difference over the area of interest. For most algorithms the area of
    interest is the whole plane, while for MRAF the area of interest is only the signal region

    :param alg_func:
    :param args:
    :param kwargs:
    :return:
    """
    target = np.asarray(datasets.face()[:, :, 0], np.float)
    x, y = _get_coordinate_arrays(target)
    shp = target.shape
    # x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
    # x, y = np.meshgrid(x, y)
    mask = (x**2 + y**2) > (0.2 * np.min(shp))**2
    target[mask] = 0
    target /= np.sum(target)

    iterations = 60
    if 'iterations' in kwargs:
        iterations = kwargs['iterations']
    # The algorithms only return the final phase, so to evaluate the smoothness at each iteration, need to set the
    # algorithm to only run one step at a time
    kwargs['iterations'] = 1

    # Defining a mask and a mixing_ratio for calculating the smoothness later
    if alg_func == gerchberg_saxton:
        mask = np.ones(shp, dtype=np.bool)
        mixing_ratio = 1
    elif alg_func == mraf:
        x, y = np.ogrid[old_div(-shp[1], 2):old_div(shp[1], 2), old_div(-shp[0], 2):old_div(shp[0], 2)]
        x, y = np.meshgrid(x, y)
        signal_region_size = 0.5
        if 'signal_region_size' in kwargs:
            signal_region_size = kwargs['signal_region_size']
        mask = (x**2 + y**2) < (signal_region_size * np.min(shp))**2
        mixing_ratio = 0.4
        if 'mixing_ratio' in kwargs:
            mixing_ratio = kwargs['mixing_ratio']
    else:
        raise ValueError('Unrecognised algorithm')

    smth = []
    outputs = []
    for indx in range(iterations):
        init_phase = alg_func(0, target, *args, **kwargs)
        input_field = np.exp(1j * init_phase)
        kwargs['input_field'] = input_field
        output = old_div(np.fft.fftshift(np.fft.fft2(np.exp(1j * init_phase))), (np.prod(shp)))
        output_int = np.abs(output) ** 2
        # print(np.sum(np.abs(output_int)), np.sum(np.abs(output_int)[mask]))
        smth += [old_div(np.sum(np.abs(output_int - mixing_ratio*target)[mask]), np.sum(mask))]
        outputs += [output]

    fig = plt.figure(figsize=(old_div(8*shp[1],shp[0])*2, 8))
    gs = gridspec.GridSpec(1, 2)
    gs2 = gridspec.GridSpecFromSubplotSpec(5, 6, gs[0], 0.001, 0.001)
    reindex = np.linspace(0, iterations-1, 30)
    ax = None
    for indx, _gs in zip(reindex, gs2):
        indx = int(indx)
        ax = plt.subplot(_gs, sharex=ax, sharey=ax)
        ax.imshow(np.abs(outputs[indx]))
        ax.text(shp[1]/2., 0, '%d=%.3g' % (indx, smth[indx]), ha='center', va='top', color='w')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
    ax2 = plt.subplot(gs[1])
    ax2.semilogy(smth)
    return np.array(smth)
//  [python]
def raccoon_face(gray: bool = True) -> np.ndarray:
    return scipy.misc.face(gray=gray)
//  [python]
def raccoon_face(gray: bool = True) -> np.ndarray:
    return scipy.datasets.face(gray=gray)
//  [python]
def test_image(setup):
    import scipy.misc

    face_rgba = scipy.misc.face()

    logger.save_image(face_rgba / 255, 'rgb.png', normalize='grid')
    logger.save_image(face_rgba / 1000, f'rgba_face_{100}.png', normalize=False)
    path = logger.save_image(face_rgba / 1000, f'rgba_face_{100}_normalized.png', normalize="individual")
    print(f"files:/{logger.root}/{logger.prefix}/{path}")
//  [python]
def test_image(setup):
    import scipy.datasets

    face_rgba = scipy.datasets.face()

    logger.save_image(face_rgba / 255, 'rgb.png', normalize='grid')
    logger.save_image(face_rgba / 1000, f'rgba_face_{100}.png', normalize=False)
    path = logger.save_image(face_rgba / 1000, f'rgba_face_{100}_normalized.png', normalize="individual")
    print(f"files:/{logger.root}/{logger.prefix}/{path}")
//  [python]
def test_image_cmap(setup):
    import scipy.misc
    import numpy as np

    face_bw = scipy.misc.face().mean(axis=-1)
    # the image dtype can not be uint8 --- in that case no cmap is applied
    logger.save_image(face_bw, f"face_bw_cmap.png", cmap="cool", normalize=True)
    logger.save_image(face_bw.astype(np.uint8), f"face_bw_no_cmap.png")
//  [python]
def test_image_cmap(setup):
    import scipy.datasets
    import numpy as np

    face_bw = scipy.datasets.face().mean(axis=-1)
    # the image dtype can not be uint8 --- in that case no cmap is applied
    logger.save_image(face_bw, f"face_bw_cmap.png", cmap="cool", normalize=True)
    logger.save_image(face_bw.astype(np.uint8), f"face_bw_no_cmap.png")
//  [python]
def main():
    import scipy.misc
    import matplotlib.pyplot as pl
    import numpy as np
    img = scipy.misc.face()
    nz = 64
    n = img.shape[0]
    img = np.transpose(np.tile(img, [1, 1, nz]), (2,0,1))
    print(f'img.shape={img.shape}')
    pl.imshow(mosaic(img), cmap='gray')
    pl.show()
//  [python]
def main():
    import scipy.datasets
    import matplotlib.pyplot as pl
    import numpy as np
    img = scipy.datasets.face()
    nz = 64
    n = img.shape[0]
    img = np.transpose(np.tile(img, [1, 1, nz]), (2,0,1))
    print(f'img.shape={img.shape}')
    pl.imshow(mosaic(img), cmap='gray')
    pl.show()