def run(args): ds = arg2ds(args.data) verbose(3, 'Concatenation yielded %i samples with %i features' % ds.shape) # build list of events events = [] timebased_events = False if args.event_attrs is not None: def_attrs = dict([(k, ds.sa[k].value) for k in args.event_attrs]) events = find_events(**def_attrs) elif args.csv_events is not None: if args.csv_events == '-': csv = sys.stdin.read() import cStringIO csv = cStringIO.StringIO(csv) else: csv = open(args.csv_events, 'rU') csvt = _load_csv_table(csv) if not len(csvt): raise ValueError("no CSV columns found") if args.onset_column: csvt['onset'] = csvt[args.onset_column] nevents = len(csvt[csvt.keys()[0]]) events = [] for ev in xrange(nevents): events.append(dict([(k, v[ev]) for k, v in csvt.iteritems()])) elif args.onsets is not None: if not len(args.onsets): args.onsets = [i for i in sys.stdin] # time or sample-based? if args.time_attr is None: oconv = int else: oconv = float events = [{'onset': oconv(o)} for o in args.onsets] elif args.fsl_ev3 is not None: timebased_events = True from mvpa2.misc.fsl import FslEV3 events = [] for evsrc in args.fsl_ev3: events.extend(FslEV3(evsrc).to_events()) if not len(events): raise ValueError("no events defined") verbose(2, 'Extracting %i events' % len(events)) if args.event_compression is None: evmap = None elif args.event_compression == 'mean': evmap = FxMapper('features', np.mean, attrfx=merge2first) elif args.event_compression == 'median': evmap = FxMapper('features', np.median, attrfx=merge2first) elif args.event_compression == 'min': evmap = FxMapper('features', np.min, attrfx=merge2first) elif args.event_compression == 'max': evmap = FxMapper('features', np.max, attrfx=merge2first) # convert to event-related ds evds = eventrelated_dataset(ds, events, time_attr=args.time_attr, match=args.match_strategy, event_offset=args.offset, event_duration=args.duration, event_mapper=evmap) # act on all attribute options evds = process_common_dsattr_opts(evds, args) # and store ds2hdf5(evds, args.output, compression=args.hdf5_compression) return evds
def test_er_nifti_dataset(): # setup data sources tssrc = os.path.join(pymvpa_dataroot, u'bold.nii.gz') evsrc = os.path.join(pymvpa_dataroot, 'fslev3.txt') masrc = os.path.join(pymvpa_dataroot, 'mask.nii.gz') evs = FslEV3(evsrc).to_events() # load timeseries ds_orig = fmri_dataset(tssrc) # segment into events ds = eventrelated_dataset(ds_orig, evs, time_attr='time_coords') # we ask for boxcars of 9s length, and the tr in the file header says 2.5s # hence we should get round(9.0/2.4) * np.prod((1,20,40) == 3200 features assert_equal(ds.nfeatures, 3200) assert_equal(len(ds), len(evs)) # the voxel indices are reflattened after boxcaring , but still 3D assert_equal(ds.fa.voxel_indices.shape, (ds.nfeatures, 3)) # and they have been broadcasted through all boxcars assert_array_equal(ds.fa.voxel_indices[:800], ds.fa.voxel_indices[800:1600]) # each feature got an event offset value assert_array_equal(ds.fa.event_offsetidx, np.repeat([0, 1, 2, 3], 800)) # check for all event attributes assert_true('onset' in ds.sa) assert_true('duration' in ds.sa) assert_true('features' in ds.sa) # check samples origsamples = _load_anyimg(tssrc)[0] for i, onset in \ enumerate([value2idx(e['onset'], ds_orig.sa.time_coords, 'floor') for e in evs]): assert_array_equal(ds.samples[i], origsamples[onset:onset + 4].ravel()) assert_array_equal(ds.sa.time_indices[i], np.arange(onset, onset + 4)) assert_array_equal(ds.sa.time_coords[i], np.arange(onset, onset + 4) * 2.5) for evattr in [ a for a in ds.sa if a.count("event_attrs") and not a.count('event_attrs_event') ]: assert_array_equal(evs[i]['_'.join(evattr.split('_')[2:])], ds.sa[evattr].value[i]) # check offset: only the last one exactly matches the tr assert_array_equal(ds.sa.orig_offset, [1, 1, 0]) # map back into voxel space, should ignore addtional features nim = map2nifti(ds) # origsamples has t,x,y,z if externals.versions['nibabel'] >= '1.2': vol_shape = nim.shape else: vol_shape = nim.get_shape() assert_equal(vol_shape, origsamples.shape[1:] + (len(ds) * 4, )) # check shape of a single sample nim = map2nifti(ds, ds.samples[0]) if externals.versions['nibabel'] >= '1.2': vol_shape = nim.shape else: vol_shape = nim.get_shape() # pynifti image has [t,]z,y,x assert_equal(vol_shape, (40, 20, 1, 4)) # and now with masking ds = fmri_dataset(tssrc, mask=masrc) ds = eventrelated_dataset(ds, evs, time_attr='time_coords') nnonzero = len(_load_anyimg(masrc)[0].nonzero()[0]) assert_equal(nnonzero, 530) # we ask for boxcars of 9s length, and the tr in the file header says 2.5s # hence we should get round(9.0/2.4) * np.prod((1,20,40) == 3200 features assert_equal(ds.nfeatures, 4 * 530) assert_equal(len(ds), len(evs)) # and they have been broadcasted through all boxcars assert_array_equal(ds.fa.voxel_indices[:nnonzero], ds.fa.voxel_indices[nnonzero:2 * nnonzero])