<commit_before>import os
import re
import unittest
from io import BytesIO
from zipfile import ZipFile

from future.standard_library import install_aliases

import fmriprep.utils.bids as bids

install_aliases()
from urllib.request import urlopen  # noqa: E402


class TestCollectBids(unittest.TestCase):
    fake_data_root_location = os.path.join("test", "fake_data")
    fake_ds_location = os.path.join(fake_data_root_location,
                                    "BIDS-examples-1-enh-ds054",
                                    "ds054")
    subject_id = "sub-100185"

    @classmethod
    def setUp(cls):
        if not os.path.exists(cls.fake_data_root_location):
            print("Downloading test data")
            url = "http://github.com/chrisfilo/BIDS-examples-1/archive/enh/ds054.zip"
            with urlopen(url) as zipresp:
                with ZipFile(BytesIO(zipresp.read())) as zfile:
                    zfile.extractall(cls.fake_data_root_location)

        cls.imaging_data = {
            cls.subject_id: bids.collect_data(os.path.join(cls.fake_ds_location),
                                              cls.subject_id)
        }

    def test_collect_bids_data(self):
        ''' test data has at least one subject with at least one session '''
        self.assertNotEqual(0, len(self.imaging_data))
        self.assertNotEqual(0, len(next(iter(self.imaging_data.values()))))

    def test_epi(self):
        epi_template = os.path.join(self.fake_ds_location, '{subject}', 'func',
                                    '{subject}_task-machinegame_run-06_bold.nii.gz')
        self.assert_key_exists(epi_template, 'func')

    def test_sbref(self):
        sbref_template = (self.fake_ds_location + "/{subject}/func/"
                          "{subject}_task-machinegame_run-06_sbref.nii.gz")
        self.assert_key_exists(sbref_template, 'sbref')

    def test_t1w(self):
        t1_template = self.fake_ds_location + "/{subject}/anat/{subject}_T1w.nii.gz"
        self.assert_key_exists(t1_template, 't1w')

    def test_fieldmaps(self):
        fieldmap_pattern = r"{0}\/fmap\/{0}_dir-[0-9]+_run-[0-9]+_epi\.nii\.gz"
        self.assert_fieldmap_files_exist(fieldmap_pattern, 'fieldmaps')

    # HELPER ASSERTIONS

    def assert_fieldmap_files_exist(self, pattern, key):
        for subject in self.imaging_data:
            search_pattern = pattern.format(subject)
            for fieldmap in self.imaging_data[subject][key]:
                match = re.search(search_pattern, fieldmap)
                self.assertTrue(match)

    def assert_key_exists(self, template, key):
        for subject in self.imaging_data:
            self.assertIn(template.format(subject=subject),
                          '\n'.join(self.imaging_data[subject][key]))<commit_msg>fix tests<commit_after>import os
import re
import unittest
from io import BytesIO
from zipfile import ZipFile

from future.standard_library import install_aliases

import fmriprep.utils.bids as bids

install_aliases()
from urllib.request import urlopen  # noqa: E402


class TestCollectBids(unittest.TestCase):
    fake_data_root_location = os.path.join("test", "fake_data")
    fake_ds_location = os.path.join(fake_data_root_location,
                                    "BIDS-examples-1-enh-ds054",
                                    "ds054")
    subject_id = "sub-100185"

    @classmethod
    def setUp(cls):
        if not os.path.exists(cls.fake_data_root_location):
            print("Downloading test data")
            url = "http://github.com/chrisfilo/BIDS-examples-1/archive/enh/ds054.zip"
            with urlopen(url) as zipresp:
                with ZipFile(BytesIO(zipresp.read())) as zfile:
                    zfile.extractall(cls.fake_data_root_location)

        cls.imaging_data = {
            cls.subject_id: bids.collect_data(os.path.join(cls.fake_ds_location),
                                              cls.subject_id)
        }

    def test_collect_bids_data(self):
        ''' test data has at least one subject with at least one session '''
        self.assertNotEqual(0, len(self.imaging_data))
        self.assertNotEqual(0, len(next(iter(self.imaging_data.values()))))

    def test_epi(self):
        epi_template = os.path.join(self.fake_ds_location, '{subject}', 'func',
                                    '{subject}_task-machinegame_run-06_bold.nii.gz')
        self.assert_key_exists(epi_template, 'bold')

    def test_sbref(self):
        sbref_template = (self.fake_ds_location + "/{subject}/func/"
                          "{subject}_task-machinegame_run-06_sbref.nii.gz")
        self.assert_key_exists(sbref_template, 'sbref')

    def test_t1w(self):
        t1_template = self.fake_ds_location + "/{subject}/anat/{subject}_T1w.nii.gz"
        self.assert_key_exists(t1_template, 't1w')

    def test_fieldmaps(self):
        fieldmap_pattern = r"{0}\/fmap\/{0}_dir-[0-9]+_run-[0-9]+_epi\.nii\.gz"
        self.assert_fieldmap_files_exist(fieldmap_pattern, 'fmap')

    # HELPER ASSERTIONS

    def assert_fieldmap_files_exist(self, pattern, key):
        for subject in self.imaging_data:
            search_pattern = pattern.format(subject)
            for fieldmap in self.imaging_data[subject][key]:
                match = re.search(search_pattern, fieldmap)
                self.assertTrue(match)

    def assert_key_exists(self, template, key):
        for subject in self.imaging_data:
            self.assertIn(template.format(subject=subject),
                          '\n'.join(self.imaging_data[subject][key]))
{'fraction_non_alphanumeric': 0.10941155299622098, 'fraction_numerical': 0.013316537700197948, 'mean_word_length': 2.4478908188585606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5736144', 'n_tokens_mistral': 1808, 'n_tokens_neox': 1701, 'n_words': 284}