Exemple #1
0
  def load_mmr(self,cData):

    X_in=np.loadtxt(self.training_input)
    X_out=np.loadtxt(self.training_output)
    alpha=np.loadtxt(self.dualParams)
    kernelParams=np.loadtxt(self.kernelParams)

    (m,nx)=X_in.shape
    ny=X_out.shape[1]

    cData.training_center=np.mean(X_in,axis=0)

    ## loading the offline training parameters
    cData.dual=base.cls_dual(alpha,np.zeros(ny)+0.1)

    ## add an empty test row to the trainig,
    ## it will be replaced with the real test inputs 
    X_in=[np.vstack((X_in,np.zeros((1,nx))))] ## list of views
    X_out=np.vstack((X_out,np.zeros((1,ny))))

    ## subspace output kernel
    cData.YKernel=mmr_kernel_explicit.cls_feature(ifeature=0)
    cData.YKernel.load_data(X_out,ifeature=0)
    cData.YKernel.ifeature=0
    cData.YKernel.title='output'
    ## setting output parameters
    cparams=cls_initial_params()
    cData.YKernel.kernel_params.set(cparams.get_yparams('kernel',0))
    cData.YKernel.crossval.set(cparams.get_yparams('cross',0))
    cData.YKernel.norm.set(cparams.get_yparams('norm',0))

    idata=0
    nview=1
    for iview in range(nview):
      cData.XKernel[idata]=mmr_kernel_explicit.cls_feature(ifeature=0)
      cData.XKernel[idata].load_data(X_in[iview],ifeature=0)
      cData.XKernel[idata].title='input_'+str(iview)


      ## setting input parameters
      cData.XKernel[idata].kernel_params.set(cparams. \
                                           get_xparams('kernel',idata))
      ## kernel parameters
      cData.XKernel[idata].kernel_params.ipar1=kernelParams[0]
      cData.XKernel[idata].kernel_params.ipar2=kernelParams[1]

      cData.XKernel[idata].crossval.set(cparams.get_xparams('cross',idata))
      cData.XKernel[idata].norm.set(cparams.get_xparams('norm',idata))
      idata+=1

    cData.ninputview=idata  ## set active views
    cData.mdata=cData.YKernel.dataraw.shape[0]

    cData.nfold=1
    cData.nrepeat=1
    cData.kmode=1   ## =0 additive (feature concatenation)
                    ## =1 multiplicative (fetaure tensor product)

    return
Exemple #2
0
    def load_data(self, xdata, ncategory, nrow, ncol, Y0):
        """
    load the sparse row,column,value format into the mvm object

    Input:
              xdata     list of arrays of row indexes, column indexes, values
              ncategory number of categories of the values,
                        otherwise =0, or None
              nrow      number of rows
              ncol      number of columns
              Y0        array of possible values, or None              
    """

        nldata = len(xdata)
        self.xdata_rel = [None] * nldata
        self.xdata_rel[0] = xdata[0]
        self.xdata_rel[1] = xdata[1]
        self.ncol = ncol
        self.nrow = nrow
        self.xdata_rel[2] = xdata[2]

        self.YKernel = mmr_kernel_mvm_y.cls_feature()
        self.XKernel[0] = mmr_kernel_mvm_x.cls_feature()
        self.categorymax = ncategory
        self.ndata = len(xdata[0])
        cparams = cls_initial_params()

        self.YKernel = mmr_kernel_mvm_y.cls_feature(ifeature=0)
        self.YKernel.kernel_params.set(cparams.get_yparams('kernel', 0))
        self.YKernel.crossval.set(cparams.get_yparams('cross', 0))
        self.YKernel.norm.set(cparams.get_yparams('norm', 0))

        ## setting input parameters
        iview = 0  ## we have only one kernel
        self.XKernel[iview] = mmr_kernel_mvm_x.cls_feature(ifeature=0)
        self.XKernel[iview].kernel_params.set(
            cparams.get_xparams('kernel', iview))
        self.XKernel[iview].crossval.set(cparams.get_xparams('cross', iview))
        self.XKernel[iview].norm.set(cparams.get_xparams('norm', iview))

        ## self.Y0=np.arange(self.categorymax)
        self.Y0 = Y0

        self.penalty.set_crossval()
Exemple #3
0
  def load_data(self,xdata,ncategory,nrow,ncol,Y0):
    """
    load the sparse row,column,value format into the mvm object

    Input:
              xdata     list of arrays of row indexes, column indexes, values
              ncategory number of categories of the values,
                        otherwise =0, or None
              nrow      number of rows
              ncol      number of columns
              Y0        array of possible values, or None              
    """

    nldata=len(xdata)
    self.xdata_rel=[None]*nldata
    self.xdata_rel[0]=xdata[0]
    self.xdata_rel[1]=xdata[1]
    self.ncol=ncol
    self.nrow=nrow
    self.xdata_rel[2]=xdata[2]
    
    self.YKernel=mmr_kernel_mvm_y.cls_feature()
    self.XKernel[0]=mmr_kernel_mvm_x.cls_feature()
    self.categorymax=ncategory
    self.ndata=len(xdata[0])
    cparams=cls_initial_params()
    
    self.YKernel=mmr_kernel_mvm_y.cls_feature(ifeature=0)
    self.YKernel.kernel_params.set(cparams.get_yparams('kernel',0))
    self.YKernel.crossval.set(cparams.get_yparams('cross',0))
    self.YKernel.norm.set(cparams.get_yparams('norm',0))

    ## setting input parameters
    iview=0   ## we have only one kernel
    self.XKernel[iview]=mmr_kernel_mvm_x.cls_feature(ifeature=0)
    self.XKernel[iview].kernel_params.set(cparams.get_xparams('kernel',iview))
    self.XKernel[iview].crossval.set(cparams.get_xparams('cross',iview))
    self.XKernel[iview].norm.set(cparams.get_xparams('norm',iview))
    
    ## self.Y0=np.arange(self.categorymax)
    self.Y0=Y0
    
    self.penalty.set_crossval()
    def load_mmr(self, cData, lviews):

        dmat = scipy.io.loadmat(self.sbasedir + self.datafile)
        ytrain = dmat['labels']
        xtrain = dmat['his']

        ytrain = ytrain[0, :]
        (m, n) = xtrain.shape
        nmax = ytrain.max()
        nmin = ytrain.min()
        ny = nmax - nmin + 1

        ncut = 10000
        if ncut < m:
            isample = np.random.permutation(m)
            isample = isample[:ncut]
            m = ncut
        else:
            isample = np.arange(m)

        ytrain = ytrain[isample]
        xtrain = xtrain[isample]
        X_in = [xtrain]

        X_out = np.zeros((m, ny))
        for i in range(m):
            X_out[i, ytrain[i] - nmin] = 1

        ## subspace output kernel
        cData.YKernel = mmr_kernel_explicit.cls_feature(ifeature=0)
        cData.YKernel.load_data(X_out, ifeature=0)
        cData.YKernel.ifeature = 0
        cData.YKernel.title = 'output'
        ## setting output parameters
        cparams = cls_initial_params()
        cData.YKernel.kernel_params.set(cparams.get_yparams('kernel', 0))
        ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0))
        cData.YKernel.crossval.set(cparams.get_yparams('cross', 0))
        cData.YKernel.norm.set(cparams.get_yparams('norm', 0))

        idata = 0
        nview = 1
        for iview in range(nview):
            if iview in lviews:
                cData.XKernel[idata] = mmr_kernel_explicit.cls_feature(
                    ifeature=0)
                cData.XKernel[idata].load_data(X_in[iview], ifeature=0)
                cData.XKernel[idata].title = 'input_' + str(iview)

                ## setting input parameters
                cData.XKernel[idata].kernel_params.set(cparams. \
                                                     get_xparams('kernel',idata))
                ## cData.XKernel[idata].prekernel_params.set(cparams. \
                ##                             get_xinparams('kernel',idata))
                cData.XKernel[idata].crossval.set(
                    cparams.get_xparams('cross', idata))
                cData.XKernel[idata].norm.set(
                    cparams.get_xparams('norm', idata))
                idata += 1

        cData.ninputview = idata  ## set active views
        cData.mdata = cData.YKernel.dataraw.shape[0]

        cData.nfold = 2
        cData.nrepeat = 2
        cData.kmode = 1  ## =0 additive (feature concatenation)
        ## =1 multiplicative (fetaure tensor product)

        return
Exemple #5
0
  def load_data(self,cData,idata,lfeatures):
    """
    cData       refers to an mmr class object, created by mmr_mmr_cls
    idata       index of the chosen database from self.datadirs
    lfeatures   list of indexes of the selected features,
                the 'annot' the labels always implicitly selected

                the data files assumed to be matlab files
    """

    ## read the outputs, list_feature[0]=annot'
    dmat=scipy.io.loadmat(self.basedir+self.datadirs[idata]+'/' \
                          +self.list_features[0]+'.mat')
    ytrain=dmat['xtrain']
    ytest=dmat['xtest']
    print('ytrain',ytrain.shape)
    print('ytest',ytest.shape)

    ytrain=1*(ytrain>0)   ## label indicators are 1 and 0
    ytest=1*(ytest>0)
    mtrain=ytrain.shape[0]
    mtest=ytest.shape[0]

    ## select random subsample of training
    if self.ncut<mtrain:
      itrainsample=np.random.permutation(mtrain)
      itrainsample=itrainsample[:self.ncut]
    else:
      itrainsample=np.arange(mtrain)

    ## select random subsample of test
    if self.ncut<mtest:
      itestsample=np.random.permutation(mtest)
      itestsample=itestsample[:self.ncut]
    else:
      itestsample=np.arange(mtest)

    ## store the indexes of the subtraining and subtest in the MMR object
    cData.ifixtrain=np.arange(len(itrainsample))
    cData.ifixtest=np.arange(len(itestsample))

    ## we can concatenate now the training and test since ifixtrain
    ## and ifixtest will select them
    ydata=np.vstack((ytrain[itrainsample],ytest[itestsample]))
    ## subspace output kernel
    cData.YKernel=mmr_kernel_explicit.cls_feature(ifeature=0)
    cData.YKernel.load_data(ydata,ifeature=0)
    cData.YKernel.ifeature=0
    cData.YKernel.title='output'
    ## setting output parameters
    cparams=mmr_initial_params.cls_initial_params()
    ## kernel parameters
    cData.YKernel.kernel_params.set(cparams.get_yparams('kernel',0))
    ## cross validation ranges
    cData.YKernel.crossval.set(cparams.get_yparams('cross',0))
    ## normalization, localization and scaling type
    cData.YKernel.norm.set(cparams.get_yparams('norm',0))
    
    ## process the input features 
    iview=0
    for ifeature in lfeatures:
      if ifeature==0:   ## if annot(labels) included drop it!
        continue        
      dmat=scipy.io.loadmat(self.basedir+self.datadirs[idata]+'/' \
                          +self.list_features[ifeature]+'.mat')
      xtrain=dmat['xtrain']
      xtest=dmat['xtest']

      print('xtrain',xtrain.shape)
      print('xtest',xtest.shape)

      xdata=np.vstack((xtrain[itrainsample], \
                                    xtest[itestsample])).astype(np.double)
      print('>>>>>>>',self.datadirs[idata],'ifeature:', \
            self.list_features[ifeature], \
            len(itrainsample),len(itestsample))
      cData.XKernel[iview]=mmr_kernel_explicit.cls_feature(ifeature=0)
      cData.XKernel[iview].load_data(xdata,ifeature=0)
      cData.XKernel[iview].title='input_'+str(ifeature)

      ## setting input kernel parameters
      cData.XKernel[iview].kernel_params.set(cparams. \
                                             get_xparams('kernel',iview))
      ## cross validation ranges
      cData.XKernel[iview].crossval.set(cparams.get_xparams('cross',iview))
      ## normalization, localization and scaling type
      cData.XKernel[iview].norm.set(cparams.get_xparams('norm',iview))
      iview+=1

    cData.ninputview=iview  ## set the number of active views
    cData.mdata=cData.YKernel.dataraw.shape[0]


    return
  def load_mmr(self,cData,lviews):

    dmat=scipy.io.loadmat(self.sbasedir+self.datafile)
    ytrain=dmat['labels']
    xtrain=dmat['his']

    ytrain=ytrain[0,:]
    (m,n)=xtrain.shape
    nmax=ytrain.max()
    nmin=ytrain.min()
    ny=nmax-nmin+1

    ncut=10000
    if ncut<m:
      isample=np.random.permutation(m)
      isample=isample[:ncut]
      m=ncut
    else:
      isample=np.arange(m)

    ytrain=ytrain[isample]
    xtrain=xtrain[isample]
    X_in=[xtrain]

    X_out=np.zeros((m,ny))
    for i in range(m):
      X_out[i,ytrain[i]-nmin]=1

    ## subspace output kernel
    cData.YKernel=mmr_kernel_explicit.cls_feature(ifeature=0)
    cData.YKernel.load_data(X_out,ifeature=0)
    cData.YKernel.ifeature=0
    cData.YKernel.title='output'
    ## setting output parameters
    cparams=cls_initial_params()
    cData.YKernel.kernel_params.set(cparams.get_yparams('kernel',0))
    ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0))
    cData.YKernel.crossval.set(cparams.get_yparams('cross',0))
    cData.YKernel.norm.set(cparams.get_yparams('norm',0))

    idata=0
    nview=1
    for iview in range(nview):
      if iview in lviews:
        cData.XKernel[idata]=mmr_kernel_explicit.cls_feature(ifeature=0)
        cData.XKernel[idata].load_data(X_in[iview],ifeature=0)
        cData.XKernel[idata].title='input_'+str(iview)

    ## setting input parameters
        cData.XKernel[idata].kernel_params.set(cparams. \
                                             get_xparams('kernel',idata))
        ## cData.XKernel[idata].prekernel_params.set(cparams. \
        ##                             get_xinparams('kernel',idata))
        cData.XKernel[idata].crossval.set(cparams.get_xparams('cross',idata))
        cData.XKernel[idata].norm.set(cparams.get_xparams('norm',idata))
        idata+=1

    cData.ninputview=idata  ## set active views
    cData.mdata=cData.YKernel.dataraw.shape[0]

    cData.nfold=2
    cData.nrepeat=2
    cData.kmode=1   ## =0 additive (feature concatenation)
                    ## =1 multiplicative (fetaure tensor product)

    return
Exemple #7
0
    def load_mmr(self, cData, lviews):

        ldata_labels = self.read_raw_txt(self.sbasedir, self.labelfile)
        if self.loadFurther == 1:
            ldata_labels.append([self.evaldir, 1])
        mdata = len(ldata_labels)

        ## !!!!!!!!!!!!!!!!!!!!!!!!!!
        ## fixed number of categories od sides
        ncategory = 4
        ## ncategory=3
        category_recode = [0, 1, 2, 3]
        ## ####################################
        X_out = np.zeros((mdata, ncategory))
        for i in range(mdata):
            ## X_out[i,int(ldata_labels[i][1])-1]=1
            icategory = int(ldata_labels[i][1]) - 1
            if icategory == 0:
                X_out[i, category_recode[icategory]] = 1
            else:
                if ncategory == 3:
                    X_out[i, category_recode[icategory]] = 1
                else:
                    X_out[i, category_recode[icategory]] = 1

        ## number of views: joint force, cart force, cart torque
        nview = len(self.linputmask)

        ## find common minimum length of trajectories
        xlength = np.zeros(mdata)
        for i in range(mdata):
            X = np.loadtxt(ldata_labels[i][0], skiprows=1)
            xlength[i] = X.shape[0]

        ixin_type = 0  ## =0 simple vectorized =1 angles
        ixin_decompose = 0  ## =0 no, =1 tensor decomposition
        icumulant = 0  ## =0 no cumulants, =1 cumulants are the feature components

        nmin = xlength.min()

        ## !!!!!!!!!!!!!!!!!!!!!!
        if nmin > 240:
            if ixin_decompose == 1:
                nmin = 240  ## 8*6*5
        tfactors = (30, 8, 1, 1)
        nfactor = len(tfactors)
        nlayer = 1

        ncumulant = 5
        if ncumulant > tfactors[0]:
            ncumulant = tfactors[0]
        if icumulant == 0:
            nitem = tfactors[0]
        else:
            nitem = ncumulant

        if ixin_type == 0:
            nboot = 1
            if ixin_decompose == 0:
                X_in = []
                for i in range(nview):
                    nslice = self.linputmask[i][1] - self.linputmask[i][0]
                    X_in.append(np.zeros((mdata, nboot * nmin * nslice)))

            elif ixin_decompose == 1:

                X_in = []
                for i in range(nview):
                    nslice = self.linputmask[i][1] - self.linputmask[i][0]
                    X_in.append(np.zeros((mdata, nlayer * nitem * nslice)))

            for i in range(mdata):
                ## if i==18:
                ##   print(i)
                X = np.loadtxt(ldata_labels[i][0], skiprows=1)
                X = X[:nmin, :]
                if ixin_decompose == 0:
                    for j in range(nview):
                        i0 = self.linputmask[j][0]
                        ik = self.linputmask[j][1]
                        ## fX=X[:,i0:ik].ravel()
                        ## fX=np.sort(fX)
                        ## X_in[j][i]=fX
                        X_in[j][i] = X[:, i0:ik].ravel()

                elif ixin_decompose == 1:
                    for j in range(nview):
                        i0 = self.linputmask[j][0]
                        ik = self.linputmask[j][1]
                        xmatrix = X[:, i0:ik]
                        ## xamtrix=spsignal.savgol_filter(xmatrix,7,5,deriv=0,axis=0, \
                        ##                            mode='interp')
                        ## xfactors assume 240 rows
                        xfactors=np.array([[tfactors[0],ik-i0],[tfactors[1],1], \
                                           [tfactors[2],1],[tfactors[3],1]])
                        ctensor = tensor_decomp.cls_tensor_decomp()
                        ctensor.load(xmatrix, xfactors)
                        niter = 10
                        ctensor.decompose2(niter)

                        nslice = self.linputmask[j][1] - self.linputmask[j][0]
                        fX = np.zeros(nlayer * nitem * nslice)
                        ipos = 0
                        for ilayer in range(nlayer):
                            for ifactor in range(1):
                                zfact = ctensor.lcomponents[ilayer][ifactor]
                                (mzfact, nzfact) = zfact.shape
                                for icol in range(nzfact):
                                    if np.mean(zfact[:, icol]) < 0:
                                        zfact[:, icol] = -zfact[:, icol]
                                if icumulant == 1:
                                    for icol in range(nzfact):
                                        xcumulants = cumulants(zfact[:, icol],
                                                               ncumulant,
                                                               icentral=1)
                                        fX[ipos:ipos + nitem] = xcumulants
                                        ipos += nitem
                                else:
                                    ## (u,s,v)=np.linalg.svd(zfact)
                                    ## zfact=np.dot(u[:,:nzfact],np.diag(s[:nzfact]))
                                    for icol in range(nzfact):
                                        fX[ipos:ipos + mzfact] = zfact[:, icol]
                                        ipos += mzfact

                        ## fX=np.sort(fX)
                        X_in[j][i] = fX

        elif ixin_type == 1:
            X_in = [np.zeros((mdata, nmin - 1)) for i in range(nview)]

            for i in range(mdata):
                X = np.loadtxt(self.sbasedir + ldata_labels[i][0], skiprows=1)
                X = X[:nmin, :]
                for j in range(nview):
                    i0 = self.linputmask[j][0]
                    ik = self.linputmask[j][1]
                    X_in[j][i] = angle_moment(X[:, i0:ik])

        ## subspace output kernel
        cData.YKernel = mmr_kernel_explicit.cls_feature(ifeature=0)
        cData.YKernel.load_data(X_out, ifeature=0)
        cData.YKernel.ifeature = 0
        cData.YKernel.title = 'output'
        ## setting output parameters
        cparams = cls_initial_params()
        cData.YKernel.kernel_params.set(cparams.get_yparams('kernel', 0))
        ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0))
        cData.YKernel.crossval.set(cparams.get_yparams('cross', 0))
        cData.YKernel.norm.set(cparams.get_yparams('norm', 0))

        idata = 0
        for iview in range(nview):
            if iview in lviews:
                cData.XKernel[idata] = mmr_kernel_explicit.cls_feature(
                    ifeature=0)
                cData.XKernel[idata].load_data(X_in[iview], ifeature=0)
                cData.XKernel[idata].title = 'input_' + str(iview)

                ## setting input parameters
                cData.XKernel[idata].kernel_params.set(cparams. \
                                                     get_xparams('kernel',idata))
                ## cData.XKernel[idata].prekernel_params.set(cparams. \
                ##                             get_xinparams('kernel',idata))
                cData.XKernel[idata].crossval.set(
                    cparams.get_xparams('cross', idata))
                cData.XKernel[idata].norm.set(
                    cparams.get_xparams('norm', idata))
                idata += 1

        cData.ninputview = idata  ## set active views
        cData.mdata = cData.YKernel.dataraw.shape[0]

        cData.nfold = 2
        cData.nrepeat = 2
        cData.kmode = 1  ## =0 additive (feature concatenation)
        ## =1 multiplicative (fetaure tensor product)

        return
Exemple #8
0
  def load_mmr(self, cData, lviews):
    
    ldata_labels = self.read_raw_txt(self.sbasedir,self.labelfile)
    if self.loadFurther==1:
      ldata_labels.append([self.evaldir, 1])
    mdata=len(ldata_labels)

    ## !!!!!!!!!!!!!!!!!!!!!!!!!!
    ## fixed number of categories od sides
    ncategory=4
    ## ncategory=3
    category_recode=[0,1,2,3]
    ## ####################################
    X_out=np.zeros((mdata,ncategory))
    for i in range(mdata):
      ## X_out[i,int(ldata_labels[i][1])-1]=1
      icategory=int(ldata_labels[i][1])-1
      if icategory==0:
        X_out[i,category_recode[icategory]]=1
      else:
        if ncategory==3:
          X_out[i,category_recode[icategory]]=1
        else:
          X_out[i,category_recode[icategory]]=1

    ## number of views: joint force, cart force, cart torque
    nview=len(self.linputmask)

    ## find common minimum length of trajectories
    xlength=np.zeros(mdata)
    for i in range(mdata):
      X=np.loadtxt(ldata_labels[i][0],skiprows=1)
      xlength[i]=X.shape[0]

    ixin_type=0   ## =0 simple vectorized =1 angles
    ixin_decompose=0  ## =0 no, =1 tensor decomposition
    icumulant=0   ## =0 no cumulants, =1 cumulants are the feature components 

    nmin=xlength.min()

    ## !!!!!!!!!!!!!!!!!!!!!!
    if nmin>240:
      if ixin_decompose==1:
        nmin=240    ## 8*6*5
    tfactors=(30,8,1,1)
    nfactor=len(tfactors)
    nlayer=1
    
    ncumulant=5
    if ncumulant>tfactors[0]:
      ncumulant=tfactors[0]
    if icumulant==0:
      nitem=tfactors[0]
    else:
      nitem=ncumulant
        

    if ixin_type==0:
      nboot=1
      if ixin_decompose==0:
        X_in=[]
        for i in range(nview):
          nslice=self.linputmask[i][1]-self.linputmask[i][0]
          X_in.append(np.zeros((mdata,nboot*nmin*nslice)))
          
      elif ixin_decompose==1:
        
        X_in=[]
        for i in range(nview):
          nslice=self.linputmask[i][1]-self.linputmask[i][0]
          X_in.append(np.zeros((mdata,nlayer*nitem*nslice)))

      for i in range(mdata):
        ## if i==18:
        ##   print(i)
        X=np.loadtxt(ldata_labels[i][0],skiprows=1)
        X=X[:nmin,:]
        if ixin_decompose==0:
          for j in range(nview):
            i0=self.linputmask[j][0]
            ik=self.linputmask[j][1]
            ## fX=X[:,i0:ik].ravel()
            ## fX=np.sort(fX)
            ## X_in[j][i]=fX
            X_in[j][i]=X[:,i0:ik].ravel()

        elif ixin_decompose==1:
          for j in range(nview):
            i0=self.linputmask[j][0]
            ik=self.linputmask[j][1]
            xmatrix=X[:,i0:ik]
            ## xamtrix=spsignal.savgol_filter(xmatrix,7,5,deriv=0,axis=0, \
            ##                            mode='interp')
            ## xfactors assume 240 rows
            xfactors=np.array([[tfactors[0],ik-i0],[tfactors[1],1], \
                               [tfactors[2],1],[tfactors[3],1]])
            ctensor=tensor_decomp.cls_tensor_decomp()
            ctensor.load(xmatrix,xfactors)
            niter=10
            ctensor.decompose2(niter)

            nslice=self.linputmask[j][1]-self.linputmask[j][0]
            fX=np.zeros(nlayer*nitem*nslice)
            ipos=0
            for ilayer in range(nlayer):
              for ifactor in range(1):
                zfact=ctensor.lcomponents[ilayer][ifactor]
                (mzfact,nzfact)=zfact.shape
                for icol in range(nzfact):
                  if np.mean(zfact[:,icol])<0:
                    zfact[:,icol]=-zfact[:,icol]
                if icumulant==1:
                  for icol in range(nzfact):
                    xcumulants=cumulants(zfact[:,icol],ncumulant,icentral=1)
                    fX[ipos:ipos+nitem]=xcumulants
                    ipos+=nitem
                else:
                  ## (u,s,v)=np.linalg.svd(zfact)
                  ## zfact=np.dot(u[:,:nzfact],np.diag(s[:nzfact]))
                  for icol in range(nzfact):
                    fX[ipos:ipos+mzfact]=zfact[:,icol]
                    ipos+=mzfact

            ## fX=np.sort(fX)
            X_in[j][i]=fX
          
    elif ixin_type==1:
      X_in=[ np.zeros((mdata,nmin-1)) for i in range(nview)]

      for i in range(mdata):
        X=np.loadtxt(self.sbasedir+ldata_labels[i][0],skiprows=1)
        X=X[:nmin,:]
        for j in range(nview):
          i0=self.linputmask[j][0]
          ik=self.linputmask[j][1]
          X_in[j][i]=angle_moment(X[:,i0:ik])
       

    ## subspace output kernel
    cData.YKernel=mmr_kernel_explicit.cls_feature(ifeature=0)
    cData.YKernel.load_data(X_out,ifeature=0)
    cData.YKernel.ifeature=0
    cData.YKernel.title='output'
    ## setting output parameters
    cparams=cls_initial_params()
    cData.YKernel.kernel_params.set(cparams.get_yparams('kernel',0))
    ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0))
    cData.YKernel.crossval.set(cparams.get_yparams('cross',0))
    cData.YKernel.norm.set(cparams.get_yparams('norm',0))

    idata=0
    for iview in range(nview):
      if iview in lviews:
        cData.XKernel[idata]=mmr_kernel_explicit.cls_feature(ifeature=0)
        cData.XKernel[idata].load_data(X_in[iview],ifeature=0)
        cData.XKernel[idata].title='input_'+str(iview)

    ## setting input parameters
        cData.XKernel[idata].kernel_params.set(cparams. \
                                             get_xparams('kernel',idata))
        ## cData.XKernel[idata].prekernel_params.set(cparams. \
        ##                             get_xinparams('kernel',idata))
        cData.XKernel[idata].crossval.set(cparams.get_xparams('cross',idata))
        cData.XKernel[idata].norm.set(cparams.get_xparams('norm',idata))
        idata+=1

    cData.ninputview=idata  ## set active views
    cData.mdata=cData.YKernel.dataraw.shape[0]

    cData.nfold=2
    cData.nrepeat=2
    cData.kmode=1   ## =0 additive (feature concatenation)
                    ## =1 multiplicative (fetaure tensor product)

    return
Exemple #9
0
    def load_data(self, cData, idata, lfeatures):
        """
    cData       refers to an mmr class object, created by mmr_mmr_cls
    idata       index of the chosen database from self.datadirs
    lfeatures   list of indexes of the selected features,
                the 'annot' the labels always implicitly selected

                the data files assumed to be matlab files
    """

        ## read the outputs, list_feature[0]=annot'
        dmat=scipy.io.loadmat(self.basedir+self.datadirs[idata]+'/' \
                              +self.list_features[0]+'.mat')
        ytrain = dmat['xtrain']
        ytest = dmat['xtest']
        print('ytrain', ytrain.shape)
        print('ytest', ytest.shape)

        ytrain = 1 * (ytrain > 0)  ## label indicators are 1 and 0
        ytest = 1 * (ytest > 0)
        mtrain = ytrain.shape[0]
        mtest = ytest.shape[0]

        ## select random subsample of training
        if self.ncut < mtrain:
            itrainsample = np.random.permutation(mtrain)
            itrainsample = itrainsample[:self.ncut]
        else:
            itrainsample = np.arange(mtrain)

        ## select random subsample of test
        if self.ncut < mtest:
            itestsample = np.random.permutation(mtest)
            itestsample = itestsample[:self.ncut]
        else:
            itestsample = np.arange(mtest)

        ## store the indexes of the subtraining and subtest in the MMR object
        cData.ifixtrain = np.arange(len(itrainsample))
        cData.ifixtest = np.arange(len(itestsample))

        ## we can concatenate now the training and test since ifixtrain
        ## and ifixtest will select them
        ydata = np.vstack((ytrain[itrainsample], ytest[itestsample]))
        ## subspace output kernel
        cData.YKernel = mmr_kernel_explicit.cls_feature(ifeature=0)
        cData.YKernel.load_data(ydata, ifeature=0)
        cData.YKernel.ifeature = 0
        cData.YKernel.title = 'output'
        ## setting output parameters
        cparams = mmr_initial_params.cls_initial_params()
        ## kernel parameters
        cData.YKernel.kernel_params.set(cparams.get_yparams('kernel', 0))
        ## cross validation ranges
        cData.YKernel.crossval.set(cparams.get_yparams('cross', 0))
        ## normalization, localization and scaling type
        cData.YKernel.norm.set(cparams.get_yparams('norm', 0))

        ## process the input features
        iview = 0
        for ifeature in lfeatures:
            if ifeature == 0:  ## if annot(labels) included drop it!
                continue
            dmat=scipy.io.loadmat(self.basedir+self.datadirs[idata]+'/' \
                                +self.list_features[ifeature]+'.mat')
            xtrain = dmat['xtrain']
            xtest = dmat['xtest']

            print('xtrain', xtrain.shape)
            print('xtest', xtest.shape)

            xdata=np.vstack((xtrain[itrainsample], \
                                          xtest[itestsample])).astype(np.double)
            print('>>>>>>>',self.datadirs[idata],'ifeature:', \
                  self.list_features[ifeature], \
                  len(itrainsample),len(itestsample))
            cData.XKernel[iview] = mmr_kernel_explicit.cls_feature(ifeature=0)
            cData.XKernel[iview].load_data(xdata, ifeature=0)
            cData.XKernel[iview].title = 'input_' + str(ifeature)

            ## setting input kernel parameters
            cData.XKernel[iview].kernel_params.set(cparams. \
                                                   get_xparams('kernel',iview))
            ## cross validation ranges
            cData.XKernel[iview].crossval.set(
                cparams.get_xparams('cross', iview))
            ## normalization, localization and scaling type
            cData.XKernel[iview].norm.set(cparams.get_xparams('norm', iview))
            iview += 1

        cData.ninputview = iview  ## set the number of active views
        cData.mdata = cData.YKernel.dataraw.shape[0]

        return