def __init__(self, isfld, isxmap, isdmap, evlmap, hdrtxt, hdrmap): """Initialise configuration object. Parameters ---------- isfld : list List of field names for iteration statistics namedtuple isxmap : dict Dictionary mapping iteration statistics namedtuple field names to field names in corresponding X step object iteration statistics namedtuple isdmap : dict Dictionary mapping iteration statistics namedtuple field names to field names in corresponding D step object iteration statistics namedtuple evlmap : dict Dictionary mapping iteration statistics namedtuple field names to labels in the dict returned by :meth:`DictLearn.evaluate` hdrtxt : list List of column header titles for verbose iteration statistics display hdrmap : dict Dictionary mapping column header titles to IterationStats entries """ self.IterationStats = collections.namedtuple('IterationStats', isfld) self.isxmap = isxmap self.isdmap = isdmap self.evlmap = evlmap self.hdrtxt = hdrtxt self.hdrmap = hdrmap # Call utility function to construct status display formatting self.hdrstr, self.fmtstr, self.nsep = util.solve_status_str( hdrtxt, type(self).fwiter, type(self).fpothr)
def display_start(self): """Set up status display if option selected. NB: this method assumes that the first entry is the iteration count and the last is the rho value. """ if self.opt['Verbose']: # If AutoRho option enabled rho is included in iteration status if self.opt['AutoRho', 'Enabled']: hdrtxt = type(self).hdrtxt() else: hdrtxt = type(self).hdrtxt()[0:-1] # Call utility function to construct status display formatting hdrstr, fmtstr, nsep = util.solve_status_str( hdrtxt, type(self).fwiter, type(self).fpothr) # Print header and separator strings if self.opt['StatusHeader']: print(hdrstr) print("-" * nsep) else: fmtstr, nsep = '', 0 return fmtstr, nsep
def solve(self): """Start (or re-start) optimisation. This method implements the framework for the alternation between `X` and `D` updates in a dictionary learning algorithm. If option ``Verbose`` is ``True``, the progress of the optimisation is displayed at every iteration. At termination of this method, attribute :attr:`itstat` is a list of tuples representing statistics of each iteration. Attribute :attr:`timer` is an instance of :class:`.util.Timer` that provides the following labelled timers: ``init``: Time taken for object initialisation by :meth:`__init__` ``solve``: Total time taken by call(s) to :meth:`solve` ``solve_wo_func``: Total time taken by call(s) to :meth:`solve`, excluding time taken to compute functional value and related iteration statistics """ # Construct tuple of status display column titles and set status # display strings hdrtxt = ['Itn', 'Fnc', 'DFid', u('Regℓ1')] hdrstr, fmtstr, nsep = util.solve_status_str(hdrtxt, type(self).fwiter, type(self).fpothr) # Print header and separator strings if self.opt['Verbose']: if self.opt['StatusHeader']: print(hdrstr) print("-" * nsep) # Reset timer self.timer.start(['solve', 'solve_wo_eval']) # Create process pool if self.nproc > 0: self.pool = mp.Pool(processes=self.nproc) for self.j in range(self.j, self.j + self.opt['MaxMainIter']): # Perform a set of update steps self.step() # Evaluate functional self.timer.stop('solve_wo_eval') fnev = self.evaluate() self.timer.start('solve_wo_eval') # Record iteration stats tk = self.timer.elapsed('solve') itst = self.IterationStats(*((self.j, ) + fnev + (tk, ))) self.itstat.append(itst) # Display iteration stats if Verbose option enabled if self.opt['Verbose']: print(fmtstr % itst[:-1]) # Call callback function if defined if self.opt['Callback'] is not None: if self.opt['Callback'](self): break # Clean up process pool if self.nproc > 0: self.pool.close() self.pool.join() # Increment iteration count self.j += 1 # Record solve time self.timer.stop(['solve', 'solve_wo_eval']) # Print final separator string if Verbose option enabled if self.opt['Verbose'] and self.opt['StatusHeader']: print("-" * nsep) # Return final dictionary return self.getdict()