Skip to content

don't open new fig in every iteration

Jakob Roth requested to merge no_new_fig into NIFTy_8

Currently, opimize_kl opens a new figure, when plotting the minisanity history, in every iteration of the optimisation (See this line in the code: https://gitlab.mpcdf.mpg.de/ift/nifty/-/blob/NIFTy_8/src/minimization/optimize_kl.py#L613). After the minisanity plot the figure is cleared but not closed. Thus after an optimize_kl run with n iterations, there are n open matplotlib figures. If one calls plt.show() after optimize_kl n empty figures will open. Here is a small demo:

import nifty8 as ift
import matplotlib.pyplot as plt
import numpy as np

from mpi4py import MPI
comm = MPI.COMM_WORLD
master = comm.Get_rank() == 0

############ some nifty with optimize_kl ##############
sp = ift.RGSpace(1)
op = ift.makeOp(ift.full(sp, 1)).ducktape('blub')
d = ift.full(sp, 1.)
n = ift.ScalingOperator(sp, 0.1, np.float64)
lh = ift.GaussianEnergy(d, inverse_covariance=n.inverse) @ op
n_iterations = 3
n_samples = 2
ic_sampling = ift.AbsDeltaEnergyController(name="Sampling (linear)",
                                           deltaE=0.05, iteration_limit=100)
ic_newton = ift.AbsDeltaEnergyController(name='Newton', deltaE=0.5,
                                         convergence_level=2, iteration_limit=35)
ic_sampling_nl = ift.AbsDeltaEnergyController(name='Sampling (nonlin)',
                                              deltaE=0.5, iteration_limit=15,
                                              convergence_level=2)
minimizer = ift.NewtonCG(ic_newton)
minimizer_sampling = ift.NewtonCG(ic_sampling_nl)
samples = ift.optimize_kl(lh, n_iterations, 2,
                          minimizer, ic_sampling, minimizer_sampling,
                          output_directory="folder",
                          comm=comm, plot_energy_history=True,
                          plot_minisanity_history=True)
#######################################################


x = np.linspace(1,10,100)
plt.plot(x,x**2)
if master:
    plt.show()

Is there a reason to create a new figure every time? I guess this is a bug. I have removed the plt.figure(). Note: also in the energy history we don't have a plt.figure().

Merge request reports