Example #1
0
 def __init__(self, stim_onsets=unstack_trees(DEFAULT_ONSETS)[0],
              stim_durations=unstack_trees(DEFAULT_STIM_DURATIONS)[0],
              func_data_file=DEFAULT_BOLD):
     Initable.__init__(self)
     self.onsets = stim_onsets
     self.durations = stim_durations
     self.func_data_file = func_data_file
Example #2
0
    def to_nipy_paradigm(self):

        p = self.join_sessions()
        sorted_conds = sorted(p.stimOnsets.keys())
        onsets = unstack_trees(p.stimOnsets)
        durations = unstack_trees(p.stimDurations)

        cond_ids = [
            np.hstack([[c] * len(o) for c, o in sorted(sess_ons.items())])
            for sess_ons in onsets
        ]
        # print 'cond_ids:', cond_ids
        onsets = [
            np.hstack([sess_ons[c] for c in sorted_conds])
            for sess_ons in onsets
        ]
        durations = [
            np.hstack([sess_dur[c] for c in sorted_conds])
            for sess_dur in durations
        ]
        # print 'onsets:'
        # print onsets
        # print 'durations'
        # print durations
        # print '[(dur>0.).any() for dur in durations]:'
        # print [(dur>0.).any() for dur in durations]

        if any([(dur > 0.).any() for dur in durations]):
            # Block paradigm
            if len(onsets) > 1:
                dd = [('session%02d' % i, BlockParadigm(d[0], d[1], d[2]))
                      for i, d in enumerate(zip(cond_ids, onsets, durations))]
                return dict(dd)
            else:
                return BlockParadigm(cond_ids[0],
                                     onsets[0],
                                     durations[0],
                                     amplitude=None)
        else:
            if len(onsets) > 1:

                dd = [('session%02d' % i, EventRelatedParadigm(d[0], d[1]))
                      for i, d in enumerate(zip(cond_ids, onsets))]
                return dict(dd)
            else:
                return EventRelatedParadigm(cond_ids[0],
                                            onsets[0],
                                            amplitude=None)
Example #3
0
    def to_nipy_paradigm(self):

        p = self.join_sessions()
        sorted_conds = sorted(p.stimOnsets.keys())
        onsets = unstack_trees(p.stimOnsets)
        durations = unstack_trees(p.stimDurations)


        cond_ids = [ np.hstack([[c]*len(o) \
                                    for c,o in sorted(sess_ons.items())]) \
                         for sess_ons in onsets ]
        #print 'cond_ids:', cond_ids
        onsets = [ np.hstack([sess_ons[c] for c in sorted_conds]) \
                       for sess_ons in onsets ]
        durations = [ np.hstack([sess_dur[c] for c in sorted_conds]) \
                          for sess_dur in durations ]
        # print 'onsets:'
        # print onsets
        # print 'durations'
        # print durations
        # print '[(dur>0.).any() for dur in durations]:'
        # print [(dur>0.).any() for dur in durations]

        if any([(dur>0.).any() for dur in durations]):
            # Block paradigm
            if len(onsets) > 1:
                dd = [('session%02d'%i, BlockParadigm(d[0], d[1], d[2])) \
                          for i,d in enumerate(zip(cond_ids, onsets, durations))]
                return dict(dd)
            else:
                return BlockParadigm(cond_ids[0], onsets[0], durations[0],
                                     amplitude=None)
        else:
            if len(onsets) > 1:

                dd = [('session%02d'%i, EventRelatedParadigm(d[0], d[1])) \
                          for i,d in enumerate(zip(cond_ids,onsets))]
                return dict(dd)
            else:
                return EventRelatedParadigm(cond_ids[0], onsets[0],
                                            amplitude=None)
Example #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)))

        # 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])
Example #5
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])