Exemplo n.º 1
0
 def config_itstats(self):
     """Setup config fields."""
     # NOTE: BackTrack is not implemented so always False.
     isfld = ['Iter', 'ObjFun', 'DFid', 'RegL1',
              'XPrRsdl', 'XDlRsdl', 'XRho', 'X_It',
              'D_L', 'D_Rsdl', 'D_It', 'Time']
     isxmap = {'XPrRsdl': 'PrimalRsdl', 'XDlRsdl': 'DualRsdl',
               'XRho': 'Rho', 'X_It': 'Iter'}
     isdmap = {'D_L': 'L', 'D_Rsdl': 'Rsdl', 'D_It': 'Iter'}
     hdrtxt = ['Itn', 'Fnc', 'DFid', u('ℓ1'),
               'Itn_X', 'r_X', 's_X', u('ρ_X'),
               'Itn_D', 'r_D', 'L_D', 'Time']
     hdrmap = {'Itn': 'Iter', 'Fnc': 'ObjFun', 'DFid': 'DFid',
               u('ℓ1'): 'RegL1', 'r_X': 'XPrRsdl', 's_X': 'XDlRsdl',
               u('ρ_X'): 'XRho', 'Itn_X': 'X_It',
               'r_D': 'D_Rsdl', 'L_D': 'D_L', 'Itn_D': 'D_It', 'Time': 'Time'}
     _evlmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1'}
     if self.opt['AccurateDFid']:
         evlmap = _evlmap
     else:
         evlmap = {}
         isxmap.update(_evlmap)
     return dictlrn.IterStatsConfig(
         isfld=isfld, isxmap=isxmap, isdmap=isdmap, evlmap=evlmap,
         hdrtxt=hdrtxt, hdrmap=hdrmap,
         fmtmap={'Itn_X': '%4d', 'Itn_D': '%4d'}
     )
Exemplo n.º 2
0
 def config_itstats(self):
     """Config itstats output."""
     isfld = [
         'Iter', 'ObjFun', 'DFid', 'RegL1', 'Cnstr', 'XPrRsdl', 'XDlRsdl',
         'XRho', 'DPrRsdl', 'DDlRsdl', 'DRho', 'Time'
     ]
     isxmap = {
         'XPrRsdl': 'PrimalRsdl',
         'XDlRsdl': 'DualRsdl',
         'XRho': 'Rho'
     }
     isdmap = {
         'DPrRsdl': 'PrimalRsdl',
         'DDlRsdl': 'DualRsdl',
         'DRho': 'Rho',
         'Cnstr': 'Cnstr'
     }
     hdrtxt = [
         'Itn', 'Fnc', 'DFid',
         u('ℓ1'), 'Cnstr', 'r_X', 's_X',
         u('ρ_X'), 'r_D', 's_D',
         u('ρ_D')
     ]
     hdrmap = {
         'Itn': 'Iter',
         'Fnc': 'ObjFun',
         'DFid': 'DFid',
         u('ℓ1'): 'RegL1',
         'Cnstr': 'Cnstr',
         'r_X': 'XPrRsdl',
         's_X': 'XDlRsdl',
         u('ρ_X'): 'XRho',
         'r_D': 'DPrRsdl',
         's_D': 'DDlRsdl',
         u('ρ_D'): 'DRho'
     }
     _evlmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1'}
     if self.opt['AccurateDFid']:
         evlmap = _evlmap
     else:
         evlmap = {}
         isxmap.update(_evlmap)
     return dictlrn.IterStatsConfig(isfld=isfld,
                                    isxmap=isxmap,
                                    isdmap=isdmap,
                                    evlmap=evlmap,
                                    hdrtxt=hdrtxt,
                                    hdrmap=hdrmap)
Exemplo n.º 3
0
    def __init__(self,
                 D0,
                 S,
                 lmbda,
                 W,
                 opt=None,
                 xmethod=None,
                 dmethod=None,
                 dimK=1,
                 dimN=2):
        """

        |

        **Call graph**

        .. image:: ../_static/jonga/cbpdnmddl_init.svg
           :width: 20%
           :target: ../_static/jonga/cbpdnmddl_init.svg

        |


        Parameters
        ----------
        D0 : array_like
          Initial dictionary array
        S : array_like
          Signal array
        lmbda : float
          Regularisation parameter
        W : array_like
          Mask array. The array shape must be such that the array is
          compatible for multiplication with the *internal* shape of
          input array S (see :class:`.cnvrep.CDU_ConvRepIndexing` for a
          discussion of the distinction between *external* and *internal*
          data layouts) after reshaping to the shape determined by
          :func:`.cnvrep.mskWshape`.
        opt : :class:`ConvBPDNMaskDictLearn.Options` object
          Algorithm options
        xmethod : string, optional (default 'admm')
          String selecting sparse coding solver. Valid values are
          documented in function :func:`.ConvBPDNMask`.
        dmethod : string, optional (default 'pgm')
          String selecting dictionary update solver. Valid values are
          documented in function :func:`.ConvCnstrMODMask`.
        dimK : int, optional (default 1)
          Number of signal dimensions. If there is only a single input
          signal (e.g. if `S` is a 2D array representing a single image)
          `dimK` must be set to 0.
        dimN : int, optional (default 2)
          Number of spatial/temporal dimensions
        """

        if opt is None:
            opt = ConvBPDNMaskDictLearn.Options(xmethod=xmethod,
                                                dmethod=dmethod)
        if xmethod is None:
            xmethod = opt.xmethod
        if dmethod is None:
            dmethod = opt.dmethod
        if opt.xmethod != xmethod or opt.dmethod != dmethod:
            raise ValueError('Parameters xmethod and dmethod must have the '
                             'same values used to initialise the Options '
                             'object')
        self.opt = opt
        self.xmethod = xmethod
        self.dmethod = dmethod

        # Get dictionary size
        if self.opt['DictSize'] is None:
            dsz = D0.shape
        else:
            dsz = self.opt['DictSize']

        # Construct object representing problem dimensions
        cri = cr.CDU_ConvRepIndexing(dsz, S, dimK, dimN)

        # Normalise dictionary
        D0 = cr.Pcn(D0,
                    dsz,
                    cri.Nv,
                    dimN,
                    cri.dimCd,
                    crp=True,
                    zm=opt['CCMOD', 'ZeroMean'])

        # Modify D update options to include initial values for Y
        if cri.C == cri.Cd:
            Y0b0 = np.zeros(cri.Nv + (cri.C, 1, cri.K))
        else:
            Y0b0 = np.zeros(cri.Nv + (1, 1, cri.C * cri.K))
        Y0b1 = cr.zpad(cr.stdformD(D0, cri.Cd, cri.M, dimN), cri.Nv)
        if dmethod == 'pgm':
            opt['CCMOD'].update({'X0': Y0b1})
        else:
            if dmethod == 'cns':
                Y0 = Y0b1
            else:
                Y0 = np.concatenate((Y0b0, Y0b1), axis=cri.axisM)
            opt['CCMOD'].update({'Y0': Y0})

        # Create X update object
        xstep = ConvBPDNMask(D0,
                             S,
                             lmbda,
                             W,
                             opt['CBPDN'],
                             method=xmethod,
                             dimK=dimK,
                             dimN=dimN)

        # Create D update object
        dstep = ConvCnstrMODMask(None,
                                 S,
                                 W,
                                 dsz,
                                 opt['CCMOD'],
                                 method=dmethod,
                                 dimK=dimK,
                                 dimN=dimN)

        # Configure iteration statistics reporting
        isc = dictlrn.IterStatsConfig(isfld=dc.isfld(xmethod, dmethod, opt),
                                      isxmap=dc.isxmap(xmethod, opt),
                                      isdmap=dc.isdmap(dmethod),
                                      evlmap=dc.evlmap(opt['AccurateDFid']),
                                      hdrtxt=dc.hdrtxt(xmethod, dmethod, opt),
                                      hdrmap=dc.hdrmap(xmethod, dmethod, opt),
                                      fmtmap={
                                          'It_X': '%4d',
                                          'It_D': '%4d'
                                      })

        # Call parent constructor
        super(ConvBPDNMaskDictLearn, self).__init__(xstep, dstep, opt, isc)
Exemplo n.º 4
0
    def __init__(self,
                 D0,
                 S,
                 lmbda=None,
                 opt=None,
                 xmethod=None,
                 dmethod=None,
                 dimK=1,
                 dimN=2):
        """

        |

        **Call graph**

        .. image:: ../_static/jonga/cbpdndl_init.svg
           :width: 20%
           :target: ../_static/jonga/cbpdndl_init.svg

        |


        Parameters
        ----------
        D0 : array_like
          Initial dictionary array
        S : array_like
          Signal array
        lmbda : float
          Regularisation parameter
        opt : :class:`ConvBPDNDictLearn.Options` object
          Algorithm options
        xmethod : string, optional (default 'admm')
          String selecting sparse coding solver. Valid values are
          documented in function :func:`.ConvBPDN`.
        dmethod : string, optional (default 'fista')
          String selecting dictionary update solver. Valid values are
          documented in function :func:`.ConvCnstrMOD`.
        dimK : int, optional (default 1)
          Number of signal dimensions. If there is only a single input
          signal (e.g. if `S` is a 2D array representing a single image)
          `dimK` must be set to 0.
        dimN : int, optional (default 2)
          Number of spatial/temporal dimensions
        """

        if opt is None:
            opt = ConvBPDNDictLearn.Options(xmethod=xmethod, dmethod=dmethod)
        if xmethod is None:
            xmethod = opt.xmethod
        if dmethod is None:
            dmethod = opt.dmethod
        if opt.xmethod != xmethod or opt.dmethod != dmethod:
            raise ValueError('Parameters xmethod and dmethod must have the '
                             'same values used to initialise the Options '
                             'object')
        self.opt = opt
        self.xmethod = xmethod
        self.dmethod = dmethod

        # Get dictionary size
        if self.opt['DictSize'] is None:
            dsz = D0.shape
        else:
            dsz = self.opt['DictSize']

        # Construct object representing problem dimensions
        cri = cr.CDU_ConvRepIndexing(dsz, S, dimK, dimN)

        # Normalise dictionary
        D0 = cr.Pcn(D0,
                    dsz,
                    cri.Nv,
                    dimN,
                    cri.dimCd,
                    crp=True,
                    zm=opt['CCMOD', 'ZeroMean'])

        # Modify D update options to include initial value for Y
        optname = 'X0' if dmethod == 'fista' else 'Y0'
        opt['CCMOD'].update(
            {optname: cr.zpad(cr.stdformD(D0, cri.Cd, cri.M, dimN), cri.Nv)})

        # Create X update object
        xstep = ConvBPDN(D0,
                         S,
                         lmbda,
                         opt['CBPDN'],
                         method=xmethod,
                         dimK=dimK,
                         dimN=dimN)

        # Create D update object
        dstep = ConvCnstrMOD(None,
                             S,
                             dsz,
                             opt['CCMOD'],
                             method=dmethod,
                             dimK=dimK,
                             dimN=dimN)

        # Configure iteration statistics reporting
        isc = dictlrn.IterStatsConfig(isfld=dc.isfld(xmethod, dmethod, opt),
                                      isxmap=dc.isxmap(xmethod, opt),
                                      isdmap=dc.isdmap(dmethod),
                                      evlmap=dc.evlmap(opt['AccurateDFid']),
                                      hdrtxt=dc.hdrtxt(xmethod, dmethod, opt),
                                      hdrmap=dc.hdrmap(xmethod, dmethod, opt),
                                      fmtmap={
                                          'It_X': '%4d',
                                          'It_D': '%4d'
                                      })

        # Call parent constructor
        super(ConvBPDNDictLearn, self).__init__(xstep, dstep, opt, isc)
Exemplo n.º 5
0
    def __init__(self, D0, S, lmbda=None, opt=None):
        """

        |

        **Call graph**

        .. image:: ../_static/jonga/bpdndl_init.svg
           :width: 20%
           :target: ../_static/jonga/bpdndl_init.svg

        |


        Parameters
        ----------
        D0 : array_like, shape (N, M)
          Initial dictionary matrix
        S : array_like, shape (N, K)
          Signal vector or matrix
        lmbda : float
          Regularisation parameter
        opt : :class:`BPDNDictLearn.Options` object
          Algorithm options
        """

        if opt is None:
            opt = BPDNDictLearn.Options()
        self.opt = opt

        # Normalise dictionary according to D update options
        D0 = cmod.getPcn(opt['CMOD', 'ZeroMean'])(D0)

        # Modify D update options to include initial values for Y and U
        Nc = D0.shape[1]
        opt['CMOD'].update({'Y0': D0, 'U0': np.zeros((S.shape[0], Nc))})

        # Create X update object
        xstep = bpdn.BPDN(D0, S, lmbda, opt['BPDN'])

        # Create D update object
        Nm = S.shape[1]
        dstep = cmod.CnstrMOD(xstep.Y, S, (Nc, Nm), opt['CMOD'])

        # Configure iteration statistics reporting
        if self.opt['AccurateDFid']:
            isxmap = {
                'XPrRsdl': 'PrimalRsdl',
                'XDlRsdl': 'DualRsdl',
                'XRho': 'Rho'
            }
            evlmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1'}
        else:
            isxmap = {
                'ObjFun': 'ObjFun',
                'DFid': 'DFid',
                'RegL1': 'RegL1',
                'XPrRsdl': 'PrimalRsdl',
                'XDlRsdl': 'DualRsdl',
                'XRho': 'Rho'
            }
            evlmap = {}
        isc = dictlrn.IterStatsConfig(isfld=[
            'Iter', 'ObjFun', 'DFid', 'RegL1', 'Cnstr', 'XPrRsdl', 'XDlRsdl',
            'XRho', 'DPrRsdl', 'DDlRsdl', 'DRho', 'Time'
        ],
                                      isxmap=isxmap,
                                      isdmap={
                                          'Cnstr': 'Cnstr',
                                          'DPrRsdl': 'PrimalRsdl',
                                          'DDlRsdl': 'DualRsdl',
                                          'DRho': 'Rho'
                                      },
                                      evlmap=evlmap,
                                      hdrtxt=[
                                          'Itn', 'Fnc', 'DFid',
                                          u('ℓ1'), 'Cnstr', 'r_X', 's_X',
                                          u('ρ_X'), 'r_D', 's_D',
                                          u('ρ_D')
                                      ],
                                      hdrmap={
                                          'Itn': 'Iter',
                                          'Fnc': 'ObjFun',
                                          'DFid': 'DFid',
                                          u('ℓ1'): 'RegL1',
                                          'Cnstr': 'Cnstr',
                                          'r_X': 'XPrRsdl',
                                          's_X': 'XDlRsdl',
                                          u('ρ_X'): 'XRho',
                                          'r_D': 'DPrRsdl',
                                          's_D': 'DDlRsdl',
                                          u('ρ_D'): 'DRho'
                                      })

        # Call parent constructor
        super(BPDNDictLearn, self).__init__(xstep, dstep, opt, isc)
Exemplo n.º 6
0
 def config_itstats(self):
     """Config itstats output for fista."""
     # isfld
     isfld = ['Iter', 'ObjFun', 'DFid', 'RegL1', 'Cnstr']
     if self.opt['CBPDN', 'BackTrack', 'Enabled']:
         isfld.extend(
             ['X_F_Btrack', 'X_Q_Btrack', 'X_ItBt', 'X_L', 'X_Rsdl'])
     else:
         isfld.extend(['X_L', 'X_Rsdl'])
     if self.opt['CCMOD', 'BackTrack', 'Enabled']:
         isfld.extend(
             ['D_F_Btrack', 'D_Q_Btrack', 'D_ItBt', 'D_L', 'D_Rsdl'])
     else:
         isfld.extend(['D_L', 'D_Rsdl'])
     isfld.extend(['Time'])
     # isxmap/isdmap
     isxmap = {
         'X_F_Btrack': 'F_Btrack',
         'X_Q_Btrack': 'Q_Btrack',
         'X_ItBt': 'IterBTrack',
         'X_L': 'L',
         'X_Rsdl': 'Rsdl'
     }
     isdmap = {
         'Cnstr': 'Cnstr',
         'D_F_Btrack': 'F_Btrack',
         'D_Q_Btrack': 'Q_Btrack',
         'D_ItBt': 'IterBTrack',
         'D_L': 'L',
         'D_Rsdl': 'Rsdl'
     }
     # hdrtxt
     hdrtxt = ['Itn', 'Fnc', 'DFid', u('ℓ1'), 'Cnstr']
     if self.opt['CBPDN', 'BackTrack', 'Enabled']:
         hdrtxt.extend(['F_X', 'Q_X', 'It_X', 'L_X'])
     else:
         hdrtxt.append('L_X')
     if self.opt['CCMOD', 'BackTrack', 'Enabled']:
         hdrtxt.extend(['F_D', 'Q_D', 'It_D', 'L_D'])
     else:
         hdrtxt.append('L_D')
     # hdrmap
     hdrmap = {
         'Itn': 'Iter',
         'Fnc': 'ObjFun',
         'DFid': 'DFid',
         u('ℓ1'): 'RegL1',
         'Cnstr': 'Cnstr'
     }
     if self.opt['CBPDN', 'BackTrack', 'Enabled']:
         hdrmap.update({
             'F_X': 'X_F_Btrack',
             'Q_X': 'X_Q_Btrack',
             'It_X': 'X_ItBt',
             'L_X': 'X_L'
         })
     else:
         hdrmap.update({'L_X': 'X_L'})
     if self.opt['CCMOD', 'BackTrack', 'Enabled']:
         hdrmap.update({
             'F_D': 'D_F_Btrack',
             'Q_D': 'D_Q_Btrack',
             'It_D': 'D_ItBt',
             'L_D': 'D_L'
         })
     else:
         hdrmap.update({'L_D': 'D_L'})
     # evlmap
     _evlmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1'}
     if self.opt['AccurateDFid']:
         evlmap = _evlmap
     else:
         evlmap = {}
         isxmap.update(_evlmap)
     # fmtmap
     fmtmap = {'It_X': '%4d', 'It_D': '%4d'}
     return dictlrn.IterStatsConfig(isfld=isfld,
                                    isxmap=isxmap,
                                    isdmap=isdmap,
                                    evlmap=evlmap,
                                    hdrtxt=hdrtxt,
                                    hdrmap=hdrmap,
                                    fmtmap=fmtmap)
Exemplo n.º 7
0
    def __init__(self, D0, S, lmbda=None, W=None, opt=None):
        """
        Parameters
        ----------
        D0 : array_like, shape (N, M)
          Initial dictionary matrix
        S : array_like, shape (N, K)
          Signal vector or matrix
        lmbda : float
          Regularisation parameter
        W : array_like, shape (N, K)
          Weight matrix
        opt : :class:`WeightedBPDNDictLearn.Options` object
          Algorithm options
        """

        if opt is None:
            opt = WeightedBPDNDictLearn.Options()
        self.opt = opt

        # Normalise dictionary according to D update options
        D0 = cmod.getPcn(opt['CMOD', 'ZeroMean'],
                         opt['CMOD', 'NonNegCoef'])(D0)

        # Modify D update options to include initial values for Y and U
        Nc = D0.shape[1]
        opt['CMOD'].update({'X0': D0})

        # Create X update object
        xstep = bpdn.WeightedBPDN(D0, S, lmbda, W=W, opt=opt['BPDN'])

        # Create D update object
        Nm = S.shape[1]
        dstep = cmod.WeightedCnstrMOD(xstep.Y, S, W=W, dsz=(Nc, Nm),
                                      opt=opt['CMOD'])

        if W is None:
            W = np.array([1.0], dtype=xstep.dtype)
        if W.ndim > 0:
            W = atleast_nd(2, W)
        self.W = np.asarray(W, dtype=xstep.dtype)

        # Configure iteration statistics reporting
        if self.opt['AccurateDFid']:
            isxmap = {'XRsdl': 'Rsdl', 'XL': 'L'}
            evlmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1'}
        else:
            isxmap = {'ObjFun': 'ObjFun', 'DFid': 'DFid', 'RegL1': 'RegL1',
                      'XRsdl': 'Rsdl', 'XL': 'L'}
            evlmap = {}
        isc = dictlrn.IterStatsConfig(
            isfld=['Iter', 'ObjFun', 'DFid', 'RegL1', 'Cnstr', 'XRsdl',
                   'XL', 'DRsdl', 'DL', 'Time'],
            isxmap=isxmap,
            isdmap={'Cnstr': 'Cnstr', 'DRsdl': 'Rsdl', 'DL': 'L'},
            evlmap=evlmap,
            hdrtxt=['Itn', 'Fnc', 'DFid', u('ℓ1'), 'Cnstr', 'X_Rsdl',
                    'X_L', 'D_Rsdl', 'D_L'],
            hdrmap={'Itn': 'Iter', 'Fnc': 'ObjFun', 'DFid': 'DFid',
                    u('ℓ1'): 'RegL1', 'Cnstr': 'Cnstr', 'X_Rsdl': 'XRsdl',
                    'X_L': 'XL', 'D_Rsdl': 'DRsdl', 'D_L': 'DL'}
            )

        # Call parent constructor
        super(WeightedBPDNDictLearn, self).__init__(xstep, dstep, opt, isc)