Exemplo n.º 1
0
 def from_spm_mat(self, spm_mat_file):
     """
     TODO: handle session durations
     """
     par_data, _ = load_paradigm_from_mat(spm_mat_file)
     par_data = stack_trees([par_data[s] for s in sorted(par_data.keys())])
     return Paradigm(par_data['onsets'],
                     stimDurations=par_data['stimulusLength'])
Exemplo n.º 2
0
 def from_spm_mat(self, spm_mat_file):
     """
     TODO: handle session durations
     """
     par_data, _ = load_paradigm_from_mat(spm_mat_file)
     par_data = stack_trees([par_data[s] for s in sorted(par_data.keys())])
     return Paradigm(par_data['onsets'],
                     stimDurations=par_data['stimulusLength'])
Exemplo n.º 3
0
def parse_data_options(options):
    """ Return an FmriData object corresponding to input 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:

        paradigm,tr = load_paradigm_from_mat(options.spmFile)
        nb_sessions = len(paradigm)

        if options.inputDataType == 'volume':
            SessDataClass = FMRISessionVolumicData
            if options.func_data_file is None:
                data_fns = [DEFAULT_BOLD_VOL_FILE] * nb_sessions
            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] * nb_sessions
            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

        sessions_data = []

        if len(data_fns) != nb_sessions:
            raise Exception('Inconsistent number of data files and sessions: '\
                            '%d sessions in paradigm, %d data files' \
                            %(nb_sessions, len(data_fns)))

        # 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])
Exemplo n.º 4
0
def parse_data_options(options):
    """ Return an FmriData object corresponding to input 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:

        paradigm, tr = load_paradigm_from_mat(options.spmFile)
        nb_sessions = len(paradigm)

        if options.inputDataType == 'volume':
            SessDataClass = FMRISessionVolumicData
            if options.func_data_file is None:
                data_fns = [DEFAULT_BOLD_VOL_FILE] * nb_sessions
            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] * nb_sessions
            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

        sessions_data = []

        if len(data_fns) != nb_sessions:
            raise Exception('Inconsistent number of data files and sessions: '
                            '%d sessions in paradigm, %d data files' %
                            (nb_sessions, len(data_fns)))

        # TODO: check nb of sessions and nb of data files

        for isess, sess in enumerate(sorted(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]

    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)

        if options.func_data_file is not None:
            sessions_data = []
            sessions_data.append(
                SessDataClass(onsets, durations, options.func_data_file))
        else:
            sessions_data = [sd]
        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])