<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;">
<p></p>
<div>Hi everyone, I'm Jorge and I'm trying to get a nifti image from a Searchlight Hyperalignment of event-design running project, but I have this problem. If I dont try to get the nifti image, the program gets a very acceptable accuracy. This is my program:
<br>
<br>
<div><span style="font-size:10pt">from mvpa2.suite import *</span><br>
<span style="font-size:10pt">from mvpa2.support.pylab import pl</span><br>
<span style="font-size:10pt">from mvpa2.misc.data_generators import noisy_2d_fx</span><br>
<span style="font-size:10pt">from mvpa2.mappers.svd import SVDMapper</span><br>
<span style="font-size:10pt">from mvpa2.mappers.mdp_adaptor import ICAMapper, PCAMapper</span><br>
<span style="font-size:10pt">from mvpa2 import cfg</span><br>
<br>
<span style="font-size:10pt">verbose.level = 2</span><br>
<br>
<span style="font-size:10pt">verbose(1, "Loading data...")</span><br>
<br>
<span style="font-size:10pt">def Lee_Sujeto(direc):</span><br>
<span style="font-size:10pt"> data_path = '/misc/charcot/jhevia/azalea_pymvpa/data'+str(direc)+'/Azalea2017/'</span><br>
<span style="font-size:10pt"> #mask_fname = os.path.join(data_path,"/misc/arwen/azalea/PPI/TPJ_previous/MasksSubjects/PH101_Cor01_left.nii.gz")</span><br>
<span style="font-size:10pt"> #mask_fname = os.path.join("/misc/charcot/jhevia/azalea_pymvpa/mascara_bin-mask2.nii.gz")</span><br>
<span style="font-size:10pt"> mask_fname = os.path.join("/misc/arwen/azalea/niigui/PH101_410/PH101_Cor01.feat/mask.nii.gz")</span><br>
<span style="font-size:10pt"> print '\n', data_path, '\n'</span><br>
<br>
<span style="font-size:10pt"> dhandle = OpenFMRIDataset(data_path)</span><br>
<br>
<span style="font-size:10pt"> task = 1 # object viewing task</span><br>
<span style="font-size:10pt"> model = 1 # image stimulus category model</span><br>
<span style="font-size:10pt"> subj = 1</span><br>
<span style="font-size:10pt"> run_datasets = []</span><br>
<span style="font-size:10pt"> </span><br>
<span style="font-size:10pt"> for run_id in dhandle.get_task_bold_run_ids(task)[subj]:</span><br>
<span style="font-size:10pt"> print '\n run_id, task, sujeto ', run_id, task, subj</span><br>
<span style="font-size:10pt"> # load design info for this run</span><br>
<span style="font-size:10pt"> run_events = dhandle.get_bold_run_model(model, subj, run_id)</span><br>
<span style="font-size:10pt"> # load BOLD data for this run (with masking); add 0-based chunk ID</span><br>
<span style="font-size:10pt"> run_ds = dhandle.get_bold_run_dataset(subj, task, run_id,</span><br>
<span style="font-size:10pt"> chunks=run_id -1,</span><br>
<span style="font-size:10pt"> mask=mask_fname)</span><br>
<span style="font-size:10pt"> # convert event info into a sample attribute and assign as 'targets'</span><br>
<span style="font-size:10pt"> run_ds.sa['targets'] = events2sample_attr(</span><br>
<span style="font-size:10pt"> run_events, run_ds.sa.time_coords, noinfolabel='REST')</span><br>
<span style="font-size:10pt"> # additional time series preprocessing can go here</span><br>
<span style="font-size:10pt"> run_datasets.append(run_ds)</span><br>
<span style="font-size:10pt"># print run_ds.sa.targets</span><br>
<span style="font-size:10pt"> </span><br>
<span style="font-size:10pt"> print '------------------------------------------------'</span><br>
<span style="font-size:10pt"># this is PyMVPA's vstack() for merging samples from multiple datasets</span><br>
<span style="font-size:10pt"># a=0 indicates that the dataset attributes of the first run should be used</span><br>
<span style="font-size:10pt"># for the merged dataset</span><br>
<span style="font-size:10pt"> ww = vstack(run_datasets, a=0)</span><br>
<span style="font-size:10pt"> print ' shape ww'</span><br>
<span style="font-size:10pt"> print ww.shape</span><br>
<span style="font-size:10pt"> return ww</span><br>
<br>
<span style="font-size:10pt">ds_all = []</span><br>
<span style="font-size:10pt">for suj in range(5):</span><br>
<span style="font-size:10pt"> print suj</span><br>
<span style="font-size:10pt"> sujeto = suj+1</span><br>
<span style="font-size:10pt"> if sujeto != 7 and sujeto !=9 and sujeto != 19 and sujeto != 34 and sujeto != 26 and sujeto != 30:
</span><br>
<span style="font-size:10pt"> xx = Lee_Sujeto(sujeto)</span><br>
<span style="font-size:10pt"> poly_detrend(xx, polyord=1, chunks_attr='chunks')</span><br>
<span style="font-size:10pt"> _ = [zscore(xx) for xx in ds_all]</span><br>
<span style="font-size:10pt"> xx = xx[xx.sa.targets != "REST"]</span><br>
<span style="font-size:10pt"> ds_all.append(xx)</span><br>
<br>
<span style="font-size:10pt">print 'type: \n'</span><br>
<span style="font-size:10pt">print(type(ds_all))</span><br>
<br>
<span style="font-size:10pt">print 'len: \n'</span><br>
<span style="font-size:10pt">print(len(ds_all))</span><br>
<br>
<span style="font-size:10pt">xx = ds_all[2]</span><br>
<span style="font-size:10pt">print 'type: \n'</span><br>
<br>
<span style="font-size:10pt">print type(xx)</span><br>
<br>
<span style="font-size:10pt">print 'shape: \n'</span><br>
<span style="font-size:10pt">print(xx.shape)</span><br>
<br>
<span style="font-size:10pt">print 'sa: \n'</span><br>
<span style="font-size:10pt">print xx.sa</span><br>
<br>
<span style="font-size:10pt">print 'fa: \n'</span><br>
<span style="font-size:10pt">print xx.fa</span><br>
<br>
<span style="font-size:10pt">print 'a: \n'</span><br>
<span style="font-size:10pt">print xx.a</span><br>
<span style="font-size:10pt">print xx.nsamples</span><br>
<br>
<span style="font-size:10pt">print 'resumen: \n'</span><br>
<span style="font-size:10pt">print(summary(xx))</span><br>
<br>
<span style="font-size:10pt">#print '----------------------- zscore all datasets individually ----------------------'</span><br>
<span style="font-size:10pt">#_ = [zscore(ds) for ds in ds_all]</span><br>
<span style="font-size:10pt">#inject the subject ID into all datasets</span><br>
<span style="font-size:10pt">for i, sd in enumerate(ds_all):</span><br>
<span style="font-size:10pt"> print i, len(sd)</span><br>
<span style="font-size:10pt"> sd.sa['subject'] = np.repeat(i, len(sd))</span><br>
<span style="font-size:10pt">#number of subjects</span><br>
<span style="font-size:10pt">print type(sd.sa)</span><br>
<span style="font-size:10pt">print len(sd.sa)</span><br>
<span style="font-size:10pt">nsubjs = len(ds_all)</span><br>
<span style="font-size:10pt">print "nsubjs = ", nsubjs</span><br>
<span style="font-size:10pt">#number of categories</span><br>
<span style="font-size:10pt">ncats = len(ds_all[0].UT)</span><br>
<span style="font-size:10pt">print "ncats =", ncats</span><br>
<span style="font-size:10pt">#number of run</span><br>
<span style="font-size:10pt">nruns = len(ds_all[0].UC)</span><br>
<span style="font-size:10pt">print "nruns=", nruns</span><br>
<span style="font-size:10pt">#verbose(2, "%d subjects" % len(ds_all))</span><br>
<span style="font-size:10pt">#verbose(2, "Per-subject dataset: %i samples with %i features" % ds_all[0].shape)</span><br>
<span style="font-size:10pt">#verbose(2, "Stimulus categories: %s" % ', '.join(ds_all[0].UT))</span><br>
<br>
<span style="font-size:10pt">print "-------------------------------------------------------------------------------"</span><br>
<br>
<span style="font-size:10pt"># feature selection helpers</span><br>
<span style="font-size:10pt">slhyper_start_time = time.time()</span><br>
<span style="font-size:10pt">bsc_slhyper_results = []</span><br>
<br>
<span style="font-size:10pt">clf = LinearCSVMC()</span><br>
<br>
<span style="font-size:10pt"># same cross-validation over subjects as before</span><br>
<span style="font-size:10pt">cv = CrossValidation(clf, NFoldPartitioner(attr='subject'),errorfx=mean_match_accuracy)</span><br>
<br>
<span style="font-size:10pt"># leave-one-run-out for hyperalignment training</span><br>
<span style="font-size:10pt">for test_run in range(nruns):</span><br>
<span style="font-size:10pt"> ds_train = [sd[sd.sa.chunks != test_run, :] for sd in ds_all]</span><br>
<span style="font-size:10pt"> ds_test = [sd[sd.sa.chunks == test_run, :] for sd in ds_all]</span><br>
<br>
<span style="font-size:10pt"># Initializing Searchlight Hyperalignment with Sphere searchlights of 3 voxelradius.</span><br>
<span style="font-size:10pt"># Using 40% features in each SL and spacing centers at 3-voxels distance.</span><br>
<span style="font-size:10pt"> slhyper = SearchlightHyperalignment(radius=3, featsel=0.4, sparse_radius=3)</span><br>
<span style="font-size:10pt"> </span><br>
<span style="font-size:10pt"># Performing searchlight hyperalignment on training data.</span><br>
<span style="font-size:10pt"># This step is similar to regular hyperalignment, calling the searchlight hyperalignment object with a list of datasets. Searchlight Hyperalignment returns a list of mappers corresponding to subjects in the same order as the list
of datasets we passed in.</span><br>
<span style="font-size:10pt"> slhypmaps = slhyper(ds_train)</span><br>
<br>
<br>
<span style="font-size:10pt"># Applying hyperalignment parameters is similar to applying any mapper in PyMVPA. We apply the hyperalignment parameters by running the test dataset through the forward() function of the mapper.</span><br>
<span style="font-size:10pt"> ds_hyper = [h.forward(sd) for h, sd in zip(slhypmaps, ds_test)]</span><br>
<span style="font-size:10pt"># Running between-subject classification as before.</span><br>
<span style="font-size:10pt"> ds_hyper = vstack(ds_hyper)</span><br>
<span style="font-size:10pt"> #zscore(ds_hyper, chunks_attr='subject')</span><br>
<span style="font-size:10pt"> res_cv = cv(ds_hyper)</span><br>
<span style="font-size:10pt"> bsc_slhyper_results.append(res_cv)</span><br>
<span style="font-size:10pt"> </span><br>
<span style="font-size:10pt">bsc_slhyper_results = hstack(bsc_slhyper_results)</span><br>
<br>
<br>
<span style="font-size:10pt">#Comparing the results</span><br>
<span style="font-size:10pt">verbose(1, "Average classification accuracies:")</span><br>
<span style="font-size:10pt">verbose(2, "between-subject (searchlight hyperaligned): %.2f +/-%.3f" \</span><br>
<span style="font-size:10pt"> % (np.mean(bsc_slhyper_results),</span><br>
<span style="font-size:10pt"> np.std(np.mean(bsc_slhyper_results, axis=1)) / np.sqrt(nsubjs - 1)))</span><br>
<br>
<br>
<span style="font-size:10pt">#revtest = np.arange(100, 100 + ds_all.nfeatures)</span><br>
<span style="font-size:10pt">ts = bsc_slhyper_results.a.mapper.reverse1(1 - bsc_slhyper_results.samples[0])</span><br>
<span style="font-size:10pt">ts = np.rollaxis(ts, 0, 4)</span><br>
<span style="font-size:10pt">ni = nb.Nifti1Image(ts, xx.a.imgaffine).to_filename('ersl.nii')</span><br>
<span style="font-size:10pt">os.unlink('ersl.nii')</span><br>
<span style="font-size:10pt">verbose(2, "done in %.1f seconds" % (time.time() - slhyper_start_time,))</span><br>
</div>
<br>
<span style="font-size:10pt">WARNING: 67 stds are too different (max diff=1) from 1 in dataset 0 to come from a zscored dataset. Please zscore datasets first for correct operation (unless if was intentional)</span><br>
<span style="font-size:10pt"> Average classification accuracies:</span><br>
<span style="font-size:10pt"> between-subject (searchlight hyperaligned): 1.00 +/-0.000</span><br>
<span style="font-size:10pt">Traceback (most recent call last):</span><br>
<span style="font-size:10pt"> File "Searchlight_Hyperalignment_Hevia_oct0517.py", line 153, in <module></span><br>
<span style="font-size:10pt"> ts = bsc_slhyper_results.a.mapper.reverse1(1 - bsc_slhyper_results.samples[0])</span><br>
<span style="font-size:10pt"> File "/home/inb/lconcha/fmrilab_software/miniconda2/lib/python2.7/site-packages/mvpa2/base/collections.py", line 492, in __getattribute__</span><br>
<span style="font-size:10pt"> return _object_getattribute(self, key)</span><br>
<span style="font-size:10pt">AttributeError: 'DatasetAttributesCollection' object has no attribute 'mapper'</span><br>
<span style="font-size:10pt">jhevia@charcot:/misc/charcot/jhevia/azalea_pymvpa$ <br>
<br>
Any suggest will be appreciate. Thanks a lot!<br>
<br>
Jorge<br>
</span></div>
<br>
<p></p>
<p><br>
</p>
<div id="Signature">
<div id="divtagdefaultwrapper" dir="ltr" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri,Arial,Helvetica,sans-serif,"EmojiFont","Apple Color Emoji","Segoe UI Emoji",NotoColorEmoji,"Segoe UI Symbol","Android Emoji",EmojiSymbols;">
<i>Dr. Jorge Carlos Hevia Orozco</i>
<div><i>Unidad de Analisis de Imagenes, Instituto de Neurobiologia, UNAM campus Juriquilla</i>
</div>
</div>
</div>
</div>
</body>
</html>