<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" Helper functions """

def bids_getfile(bids_root, data_type, subject_id, session_id=None, run_id=None):
    """
    A simple function to select files from a BIDS structure

    Example::

    >>> from mriqc.data import get_ds003_downsampled
    >>> bids_getfile(get_ds003_downsampled(), 'anat', '05') #doctest: +ELLIPSIS
    u'...ds003_downsampled/sub-05/anat/sub-05_T1w.nii.gz'

    """
    import os.path as op
    import glob

    if data_type == 'anat':
        scan_type = 'T1w'

    out_file = op.join(bids_root, 'sub-%s' % subject_id)

    onesession = (session_id is None or session_id == 'single_session')
    onerun = (run_id is None or run_id == 'single_run')

    if onesession and onerun:
        pattern = op.join(out_file, data_type, 'sub-%s_*%s.nii*' % (subject_id, scan_type))

    if not onesession and onerun:
        pattern = op.join(out_file, 'ses-%s' % session_id, data_type,
                          'sub-%s_ses-%s_*%s.nii*' % (subject_id, session_id, scan_type))

    results = glob.glob(pattern)

    if not results:
        raise RuntimeError('No file found with this pattern: "%s"' % pattern)

    return results[0]


def bids_scan_file_walker(dataset=".", include_types=None, warn_no_files=False):
    """
    Traverse a BIDS dataset and provide a generator interface
    to the imaging files contained within.

    :author: @chrisfilo

    https://github.com/preprocessed-connectomes-project/quality-assessment-prot\<commit_msg>fix bids name structure<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" Helper functions """

def bids_getfile(bids_root, data_type, subject_id, session_id=None, run_id=None):
    """
    A simple function to select files from a BIDS structure

    Example::

    >>> from mriqc.data import get_ds003_downsampled
    >>> bids_getfile(get_ds003_downsampled(), 'anat', '05') #doctest: +ELLIPSIS
    u'...ds003_downsampled/sub-05/anat/sub-05_T1w.nii.gz'

    """
    import os.path as op
    import glob

    if data_type == 'anat':
        scan_type = 'T1w'

    out_file = op.join(bids_root, subject_id)

    onesession = (session_id is None or session_id == 'single_session')
    onerun = (run_id is None or run_id == 'single_run')

    if onesession and onerun:
        pattern = op.join(out_file, data_type, '%s_*%s.nii*' % (subject_id, scan_type))

    if not onesession and onerun:
        pattern = op.join(out_file, session_id, data_type,
                          '%s_%s_*%s.nii*' % (subject_id, session_id, scan_type))

    results = glob.glob(pattern)

    if not results:
        raise RuntimeError('No file found with this pattern: "%s"' % pattern)

    return results[0]


def bids_scan_file_walker(dataset=".", include_types=None, warn_no_files=False):
    """
    Traverse a BIDS dataset and provide a generator interface
    to the imaging files contained within.

    :author: @chrisfilo

    https://github.com/preprocessed-connectomes-project/quality-assessment-prot\
{'fraction_non_alphanumeric': 0.13191881918819187, 'fraction_numerical': 0.014145141451414513, 'mean_word_length': 3.2746386333771356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1261708', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1112, 'n_words': 276}