Example #1
0
    def _forward_dataset(self, ds):
        targ = np.copy(ds.targets)
        mapped = None
        X = None
        i,j = ds.shape
        
        if self._stack=='v':
            chunks = np.unique(ds.sa[self._chunks_attr].value)
        else:
            chunks = np.unique(ds.fa[self._chunks_attr].value)
        
        for ch,chunk in enumerate(chunks):
            if self._stack == 'v':
                table = ds[ds.sa[self._chunks_attr].value==chunk,:]
            if self._stack == 'h':
                table = ds[:,ds.fa[self._chunks_attr].value==chunk]

            table = self.center_and_norm_table(table,
                    col_mean=self._subtable_stats[ch]['col_mean'],
                    col_norm=self._subtable_stats[ch]['col_norm'],
                    table_norm = self._subtable_stats[ch]['table_norm'])[0]


            if self._stack =='v':
                # Assume features align, use average Q
                Q_ = None
                for subtab in np.unique(self.subtable_idx):
                    if Q_ is None:
                        Q_ = self.Q[self.subtable_idx==subtab,:]
                    else:
                        Q_ = Q_ + self.Q[self.subtable_idx==subtab,:]
                Q_ = Q_ / len(np.unique(self.subtable_idx))
                part = Dataset(np.dot(table.samples,Q_))
                part.sa = table.sa
            if self._stack =='h':
                # Assume same number of features as in X
                part = Dataset(np.dot(table,self.Q[self.subtable_idx==chunk,:]))
                part.sa = table.sa
            part.sa['chunks'] = [chunk]*part.shape[0]

            if mapped is None:
                mapped = part.copy()
                X = table
            else:
                mapped.append(part.copy())
                X.append(table,'h')
        mapped.a['X'] = X
         
        if self.keep_dims == 'all':
            self.keep_dims = range(mapped.shape[1])
        return mapped[:,self.keep_dims]
Example #2
0
def run_bootstrap(ds, sts, niter=1000):
    """
       dataset: Input fmri dataset with targets and chunks, should be preprocessed
       Q:       The loading matrix from statis
       niter:   number of iteration for bootstrap
       This function iteratively samples random chunks from dataset with
       replacement and computes each statis solution.
        The factor scores from each statis are then projected into original compromise
       matrix space using Q
       OUTPUT: FBoot collects all projected factor scores
    """
    ntables = sts.ntables
    rows, nfactors = ds.shape
    nrows = rows/ntables
    boot = np.zeros((nrows,nfactors,niter))
    X = ds.a['X'].value
    X = X.samples

    ignore = 0 # Hack: for some reasone inter_table_Rv_analysis chokes...
    ident = np.unique(sts.subtable_idx)
    for i in range(niter):
        idx = ident[np.random.random_integers(0,ntables-1,size=(ntables,))]
        Y = None
        Y_idx = None
        fselect = np.zeros((nrows,nfactors,ntables))

        for k,j in enumerate(idx):
            Y_t = X[:,sts.subtable_idx==j]
            if Y_idx is None:
                Y_idx = np.ones((Y_t.shape[1]))*k
            else:
                Y_idx = np.hstack((Y_idx,np.ones((Y_t.shape[1]))*k))

            if Y == None:
                Y = Y_t
            else:
                Y = np.hstack((Y,Y_t))
            fselect[:,:,k] = ds.samples[ds.chunks==np.unique(ds.chunks)[j],:]

        (A,alpha,C,G) = inter_table_Rv_analysis(Y,Y_idx)
        if G is None:
            ignore += 1
            continue
        #print "shape alpha:", alpha.shape
        #print "shape fselect", fselect.shape
        #print alpha
        #print fselect
        boot[:,:,i] = np.sum(fselect*alpha.flatten(),2)

        if i%100==0:
            sys.stdout.write("iter:%s/%s\r"% (i,niter))
            sys.stdout.flush()

    print "completed %s/%s bootstraps." % (niter-ignore,niter)
    boot = Dataset(boot)
    boot.sa['targets'] = ds.targets[:nrows]
    return boot
Example #3
0
 def _call(self, ds):
     data_dsm = 1 - pdist(ds.samples, metric='correlation')
     data_dsm = np.arctanh(data_dsm)  # Fisher z transformation
     data_dsm = data_dsm[self.labels != 0]
     labels = self.labels[self.labels != 0]
     data_dsm = zscore(data_dsm)
     # difference between distances of same types of trials across run (labeled 1)
     # and different types of trals across run (labeled 2)
     sim = np.mean(data_dsm[labels == 1]) - np.mean(data_dsm[labels == 2])
     return Dataset([sim])
Example #4
0
 def _call(self, ds):
     data_dsm = pdist(ds.samples, metric='correlation')  # 'euclidean'
     corr = spearmanr(data_dsm, self.labels, axis=None).correlation
     return Dataset([corr])