Ejemplo n.º 1
0
 def from_csv(self, csvFile, delim=' '):
     """
     Create a Paradigm object from a CSV file which columns are:
     session, task name, stimulation onset, stimulation duration, [amplitude]
     """
     onsets, durations = load_paradigm_from_csv(csvFile, delim)
     return Paradigm(onsets, stimDurations=durations)
Ejemplo n.º 2
0
    def load_and_get_fdata_params(self, mask):

        if op.splitext(self.paradigm_file)[-1] == '.csv':
            onsets, durations = pio.load_paradigm_from_csv(self.paradigm_file)
        else:
            raise Exception('Only CSV file format support for paradigm')

        fns = self.func_data_files
        pyhrf.verbose(1, 'Load functional data from: %s' %',\n'.join(fns))
        fdata = stack_cuboids([xndarray.load(fn) for fn in fns], 'session')

        fdata = np.concatenate(fdata.data) #scan sessions along time axis
        pio.discard_bad_data(fdata, mask)
        pyhrf.verbose(1, 'Functional data shape %s' %str(fdata.shape))

        return {'stim_onsets': onsets, 'stim_durations':durations,
                'func_data': fdata}
Ejemplo n.º 3
0
def parse_data_options(options):

    from pyhrf.core import DEFAULT_BOLD_SURF_FILE, DEFAULT_BOLD_VOL_FILE, \
        DEFAULT_SIMULATION_FILE
    # If SPM.mat is provided, retrieve paradigm from it for all sessions.
    # Leave data file pathes to unknown.
    if options.spmFile is not None:
        if options.inputDataType == 'volume':
            SessDataClass = FMRISessionVolumicData
            if options.func_data_file is None:
                data_fns = DEFAULT_BOLD_VOL_FILE
            else:
                data_fns = options.func_data_file
            if options.mask_file is None:
                options.mask_file = DEFAULT_MASK_VOL_FILE
            fmriDataInit = FmriData.from_vol_ui
        elif options.inputDataType == 'surface':
            SessDataClass = FMRISessionSurfacicData
            if options.func_data_file is None:
                data_fns = DEFAULT_BOLD_SURF_FILE
            else:
                data_fns = options.func_data_file
            if options.mask_file is None:
                options.mask_file = DEFAULT_MASK_SURF_FILE

            fmriDataInit = FmriData.from_surf_ui
        elif options.inputDataType == 'simulation':
            SessDataClass = FMRISessionSimulationData
            data_fns = DEFAULT_SIMULATION_FILE
            fmriDataInit = FmriData.from_simu_ui


        if not isinstance(data_fns, list):
            data_fns = [data_fns]

        paradigm,tr = load_paradigm_from_mat(options.spmFile)
        sessions_data = []

        # print 'paradigm:', paradigm.keys()
        # print 'data_fns:', len(data_fns)
        # print data_fns
        #TODO: check nb of sessions and nb of data files
        for isess, sess in enumerate(sorted(paradigm.keys())):
            #print len(paradigm.keys())
            sessions_data.append(SessDataClass(paradigm[sess]['onsets'],
                                               paradigm[sess]['stimulusLength'],
                                               data_fns[isess]))

        if options.inputDataType == 'surface':

            return fmriDataInit(sessions_data=sessions_data, tr=tr,
                                mask_file=options.mask_file,
                                mesh_file=options.mesh_file)

        return fmriDataInit(sessions_data=sessions_data, tr=tr,
                            mask_file=options.mask_file)


    #unstack & take 1st set of onsets for each condition to get only one session
    onsets = unstack_trees(eval('pyhrf.paradigm.onsets_%s' %options.paradigm))[0]
    durations = unstack_trees(eval('pyhrf.paradigm.durations_%s' \
                                   %options.paradigm))[0]
    #print options.paradigm

    if options.paradigm_csv is not None:
        onsets, durations = load_paradigm_from_csv(options.paradigm_csv)
        from pyhrf.tools import apply_to_leaves
        onsets = apply_to_leaves(onsets, lambda x: x[0])
        durations = apply_to_leaves(durations, lambda x: x[0])



    ### Set data type:
    if options.inputDataType == 'volume' :
        if options.data_scenario == 'default':
            SessDataClass = FMRISessionVolumicData
            sd = SessDataClass(onsets, durations)
            if options.mask_file is None:
                options.mask_file = DEFAULT_MASK_VOL_FILE
        elif options.data_scenario == 'small':
            sd = SessDataClass(onsets, durations)
            if options.mask_file is None:
                options.mask_file = DEFAULT_MASK_SMALL_VOL_FILE
        elif options.data_scenario == 'realistic':
            sd = FMRISessionVolumicData(onsets, durations,
                                        REALISTIC_REAL_DATA_BOLD_VOL_FILE)
            if options.mask_file is None:
                options.mask_file = REALISTIC_REAL_DATA_MASK_VOL_FILE
        else:
            raise Exception("Uknown data scenario: %s" %options.data_scenario)
        #print 'sd!!:', sd
        #print 'mask:', options.mask_file



        if options.func_data_file is not None:
            #print 'options.func_data_file:', options.func_data_file
            sessions_data = []
            sessions_data.append(SessDataClass(onsets, durations, options.func_data_file))
        else:
            sessions_data = [sd]
        #print options
        if hasattr(options, 'tr') and options.tr is not None:
            tr = options.tr
            res = FmriData.from_vol_ui(sessions_data=sessions_data, tr=tr,
                                    mask_file=options.mask_file)
        else:
            res = FmriData.from_vol_ui(sessions_data=sessions_data,
                                       mask_file=options.mask_file)

        return res

    elif options.inputDataType == 'surface':
        mask_fn = DEFAULT_MASK_SURF_FILE
        mesh_fn = DEFAULT_MESH_FILE

        if options.data_scenario == 'default':
            #TODO: create a bigger surface default dataset
            sd = FMRISessionSurfacicData(onsets, durations)
        if options.data_scenario == 'small':
            sd = FMRISessionSurfacicData(onsets, durations)
        elif options.data_scenario == 'realistic':
            raise NotImplementedError('Realistic surfacic dataset not yet '\
                                          'available (TODO)')

        return FmriData.from_surf_ui(sessions_data=[sd], mask_file=mask_fn,
                                     mesh_file=mesh_fn)

    elif options.inputDataType == 'simulation':
        if options.data_scenario == 'default':
            sd = FMRISessionSimulationData(onsets,durations)
        elif options.data_scenario == 'small':
            raise NotImplementedError('Small artificial dataset not yet '\
                                          'available (TODO)')
        if options.data_scenario == 'realistic':
            raise NotImplementedError('Realistic artificial dataset not yet '\
                                          'available (TODO)')


        return FmriData.from_simu_ui(sessions_data=[sd])