예제 #1
0
def get_task():
    if not task:
        tasks = get_entity_vals(bids_root, entity_key='task')
        if not tasks:
            return None
        else:
            return tasks[0]
    else:
        return task
예제 #2
0
def get_runs():
    runs_ = copy.deepcopy(runs)  # Avoid clash with global variable.

    if runs_ == 'all':
        runs_ = get_entity_vals(bids_root, entity_key='run')

    if not runs_:
        return [None]
    else:
        return runs_
예제 #3
0
def get_sessions():
    sessions_ = copy.deepcopy(sessions)  # Avoid clash with global variable.

    if sessions_ == 'all':
        sessions_ = get_entity_vals(bids_root, entity_key='session')

    if not sessions_:
        return [None]
    else:
        return sessions_
예제 #4
0
def get_subjects():
    if subjects_list == 'all':
        s = get_entity_vals(bids_root, entity_key='subject')
    else:
        s = subjects_list

    subjects = set(s) - set(exclude_subjects)
    # Drop empty-room subject.
    subjects = subjects - set(['emptyroom'])

    return list(subjects)
예제 #5
0
def get_sessions():
    sessions_ = copy.deepcopy(sessions)  # Avoid clash with global variable.

    env = os.environ
    if env.get('MNE_BIDS_STUDY_SESSION'):
        sessions_ = env['MNE_BIDS_STUDY_SESSION']
    elif sessions_ == 'all':
        sessions_ = get_entity_vals(bids_root, entity_key='session')

    if not sessions_:
        return [None]
    else:
        return sessions_
예제 #6
0
def get_runs():
    runs_ = copy.deepcopy(runs)  # Avoid clash with global variable.
    valid_runs = get_entity_vals(bids_root, entity_key='run')

    env = os.environ
    if env.get('MNE_BIDS_STUDY_RUN'):
        env_run = env['MNE_BIDS_STUDY_RUN']
        raise ValueError(f'Invalid run. It can be {valid_runs} but '
                         f'got {env_run}')
        runs_ = [env_run]
    elif runs_ == 'all':
        runs_ = valid_runs

    if not runs_:
        return [None]
    else:
        return runs_
예제 #7
0
def get_task() -> Optional[str]:
    global task

    env = os.environ
    valid_tasks = get_entity_vals(bids_root, entity_key='task')

    if env.get('MNE_BIDS_STUDY_TASK'):
        task = env['MNE_BIDS_STUDY_TASK']
        if task not in valid_tasks:
            raise ValueError(f'Invalid task. It can be: '
                             f'{", ".join(valid_tasks)} but got: {task}')

    if not task:
        if not valid_tasks:
            return None
        else:
            return valid_tasks[0]
    else:
        return task
예제 #8
0
def get_subjects() -> List[str]:
    global subjects

    env = os.environ

    valid_subjects = get_entity_vals(bids_root, entity_key='subject')

    if env.get('MNE_BIDS_STUDY_SUBJECT'):
        env_subject = env['MNE_BIDS_STUDY_SUBJECT']
        if env_subject not in valid_subjects:
            raise ValueError(
                f'Invalid subject. It can be {valid_subjects} but '
                f'got {env_subject}')
        s = [env_subject]
    elif subjects == 'all':
        s = valid_subjects
    else:
        s = subjects

    subjects = set(s) - set(exclude_subjects)
    # Drop empty-room subject.
    subjects = subjects - set(['emptyroom'])

    return sorted(subjects)
예제 #9
0
def make_report(root, session=None, verbose=True):
    """Create a methods paragraph string from BIDS dataset.

    Summarizes the REQUIRED components in the BIDS specification
    and also some RECOMMENDED components. Currently, the methods
    paragraph summarize the:

      - dataset_description.json file
      - (optional) participants.tsv file
      - (optional) datatype-agnostic files for (M/I)EEG data,
        which reads files from the ``*_scans.tsv`` file.

    Parameters
    ----------
    root : str | pathlib.Path
        The path of the root of the BIDS compatible folder.
    session : str | None
            The (optional) session for a item. Corresponds to "ses".
    verbose : bool | None
        Set verbose output to true or false.

    Returns
    -------
    paragraph : str
        The paragraph wrapped with 80 characters per line
        describing the summary of the subjects.
    """
    # high level summary
    subjects = get_entity_vals(root, entity_key='subject')
    sessions = get_entity_vals(root, entity_key='session')
    modalities = get_datatypes(root)

    # only summarize allowed modalities (MEG/EEG/iEEG) data
    # map them to a pretty looking string
    datatype_map = {
        'meg': 'MEG',
        'eeg': 'EEG',
        'ieeg': 'iEEG',
    }
    modalities = [
        datatype_map[datatype] for datatype in modalities
        if datatype in datatype_map.keys()
    ]

    # REQUIRED: dataset_description.json summary
    dataset_summary = _summarize_dataset(root)

    # RECOMMENDED: participants summary
    participant_summary = _summarize_participants_tsv(root)

    # RECOMMENDED: scans summary
    scans_summary = _summarize_scans(root, session=session, verbose=verbose)

    # turn off 'recommended' report summary
    # if files are not available to summarize
    if not participant_summary:
        participant_template = ''
    else:
        content = f'{FUNCTION_TEMPLATE}{PARTICIPANTS_TEMPLATE}'
        participant_template = Template(content=content)
        participant_template = participant_template.substitute(
            **participant_summary)
        if verbose:
            print(f'The participant template found: {participant_template}')

    dataset_summary['PARTICIPANTS_TEMPLATE'] = str(participant_template)

    if not scans_summary:
        datatype_agnostic_template = ''
    else:
        datatype_agnostic_template = DATATYPE_AGNOSTIC_TEMPLATE

    dataset_summary.update({
        'system': modalities,
        'n_subjects': len(subjects),
        'n_sessions': len(sessions),
        'sessions': sessions,
    })

    # XXX: add channel summary for modalities (ieeg, meg, eeg)
    # create the content and mne Template
    # lower-case templates are "Recommended",
    # while upper-case templates are "Required".
    content = f'{FUNCTION_TEMPLATE}{BIDS_DATASET_TEMPLATE}' \
              f'{datatype_agnostic_template}'

    paragraph = Template(content=content)
    paragraph = paragraph.substitute(**dataset_summary, **scans_summary)

    # Clean paragraph
    paragraph = paragraph.replace('\n', ' ')
    paragraph = paragraph.replace('  ', ' ')

    return '\n'.join(textwrap.wrap(paragraph, width=80))
예제 #10
0
def make_report(root, session=None, verbose=None):
    """Create a methods paragraph string from BIDS dataset.

    Summarizes the REQUIRED components in the BIDS specification
    and also some RECOMMENDED components. Currently, the methods
    paragraph summarize the:

      - dataset_description.json file
      - (optional) participants.tsv file
      - (optional) datatype-agnostic files for (M/I)EEG data,
        which reads files from the ``*_scans.tsv`` file.

    Parameters
    ----------
    root : path-like
        The path of the root of the BIDS compatible folder.
    session : str | None
            The (optional) session for a item. Corresponds to "ses".
    %(verbose)s

    Returns
    -------
    paragraph : str
        The paragraph wrapped with 80 characters per line
        describing the summary of the subjects.
    """
    # high level summary
    subjects = get_entity_vals(root, entity_key='subject')
    sessions = get_entity_vals(root, entity_key='session')
    modalities = get_datatypes(root)

    # only summarize allowed modalities (MEG/EEG/iEEG) data
    # map them to a pretty looking string
    datatype_map = {
        'meg': 'MEG',
        'eeg': 'EEG',
        'ieeg': 'iEEG',
    }
    modalities = [
        datatype_map[datatype] for datatype in modalities
        if datatype in datatype_map.keys()
    ]

    # REQUIRED: dataset_description.json summary
    dataset_summary = _summarize_dataset(root)

    # RECOMMENDED: participants summary
    participant_summary = _summarize_participants_tsv(root)

    # RECOMMENDED: scans summary
    scans_summary = _summarize_scans(root, session=session)

    dataset_agnostic_summary = scans_summary.copy()
    dataset_agnostic_summary['system'] = _pretty_str(modalities)

    # turn off 'recommended' report summary
    # if files are not available to summarize
    if not participant_summary:
        participants_info = ''
    else:
        particpants_info_template = jinja_env.get_template(
            'participants.jinja')
        participants_info = particpants_info_template.render(
            **participant_summary)
        logger.info(f'The participant template found: {participants_info}')

    if not scans_summary:
        datatype_agnostic_info = ''
    else:
        datatype_agnostic_template = jinja_env.get_template(
            'datatype_agnostic.jinja')
        datatype_agnostic_info = datatype_agnostic_template.render(
            **dataset_agnostic_summary)

    dataset_summary.update({
        'n_subjects': len(subjects),
        'participants_info': participants_info,
        'n_sessions': len(sessions),
        'sessions': _pretty_str(sessions),
    })

    # XXX: add channel summary for modalities (ieeg, meg, eeg)
    # create the content and mne Template
    # lower-case templates are "Recommended",
    # while upper-case templates are "Required".

    dataset_summary_template = jinja_env.get_template('dataset_summary.jinja')
    dataset_summary_info = dataset_summary_template.render(**dataset_summary)

    # Concatenate info and clean the paragraph
    paragraph = f'{dataset_summary_info}\n{datatype_agnostic_info}'
    paragraph = paragraph.replace('\n', ' ')
    while '  ' in paragraph:
        paragraph = paragraph.replace('  ', ' ')

    return '\n'.join(textwrap.wrap(paragraph, width=80))