<commit_before>''' Testing module for niworkflows.reports.core '''

import os
from pathlib import Path
from pkg_resources import resource_filename
import tempfile

import pytest

from ..core import Report


@pytest.fixture
def test_report():
    test_data_path = resource_filename(
        'niworkflows',
        os.path.join('data', 'tests', 'work', 'reportlets'))
    out_dir = tempfile.mkdtemp()

    return Report(Path(test_data_path), Path(out_dir), 'fakeiuud',
                  subject_id='01', packagename='fmriprep')


@pytest.mark.parametrize(
    "orderings,expected_entities,expected_value_combos",
    [
        (['session', 'task', 'run'],
         ['task', 'run'],
         [
            ('faketask', None),
            ('mixedgamblestask', 1),
            ('mixedgamblestask', 2),
            ('mixedgamblestask', 3),
        ]),
        (['run', 'task', 'session'],
         ['run', 'task'],
         [
            (None, 'faketask'),
            (1, 'mixedgamblestask'),
            (2, 'mixedgamblestask'),
            (3, 'mixedgamblestask'),
        ]),
        ([''], [], []),
        (['session'], [], []),
        ([], [], []),
        (['madeupentity'], [], []),
    ]
)
def test_process_orderings(test_report, orderings,
                           expected_entities, expected_value_combos):
    test_report.init_layout()
    entities, value_combos = test_report._process_orderings(orderings, test_report.layout)

    assert entities == expected_entities
    assert expected_value_combos == value_combos<commit_msg>refactor tests to be more comprehensive<commit_after>''' Testing module for niworkflows.reports.core '''

import os
from pathlib import Path
from pkg_resources import resource_filename
import tempfile
from itertools import product

import matplotlib.pyplot as plt
from bids.layout.writing import build_path
from bids.config import _default_settings

import pytest

from ..core import Report


@pytest.fixture()
def bids_sessions(tmpdir_factory):
    f, _ = plt.subplots()
    svg_dir = tmpdir_factory.mktemp('work') / 'fmriprep'
    svg_dir.ensure_dir()

    pattern = (
        "sub-{subject}[/ses-{session}]/{datatype<anat|func>}/"
        "sub-{subject}[_ses-{session}][_task-{task}][_acq-{acquisition}]"
        "[_ce-{contrast}][_dir-{direction}][_rec-{reconstruction}]"
        "[_mod-{modality}][_run-{run}][_echo-{echo}][_space-{space}]"
        "[_desc-{desc}]_{suffix<dseg|T1w|bold>}.{extension<svg>}"
    )
    subjects = ['01']
    sessions = ['pre', '1']
    tasks = ['t1', 't2', 't3']
    runs = ['01', '02', None]
    descs = ['aroma', 'bbregister', 'carpetplot', 'rois']
    # create functional data for both sessions
    all_combos = product(subjects, sessions, tasks, runs, descs)
    for subject, session, task, run, desc in all_combos:
        entities = {
            'subject': subject,
            'session': session,
            'task': task,
            'run': run,
            'desc': desc,
            'extension': 'svg',
            'suffix': 'bold',
            'datatype': 'func'
        }
        bids_path = build_path(entities, pattern)
        file_path = svg_dir / bids_path
        file_path.ensure()
        f.savefig(file_path)

    # create anatomical data for both sessions
    anat_opts = [
        {'desc': 'brain'},
        {'desc': 'conform'},
        {'desc': 'reconall'},
        {'desc': 'rois'},
        {'suffix': 'dseg'},
        {'space': 'MNI152NLin6Asym'},
        {'space': 'MNI152NLin2009cAsym'},
    ]
    anat_combos = product(subjects, sessions, anat_opts)
    for subject, session, anat_opt in anat_combos:
        anat_entities = {
            "subject": subject,
            "session": session,
            "datatype": 'anat',
            "suffix": 't1w'
        }
        anat_entities.update(**anat_opt)
        bids_path = build_path(entities, pattern)
        file_path = svg_dir / bids_path
        file_path.ensure()
        f.savefig(file_path)
    
    return svg_dir.dirname


@pytest.fixture()
def test_report1():
    test_data_path = resource_filename(
        'niworkflows',
        os.path.join('data', 'tests', 'work', 'reportlets'))
    out_dir = tempfile.mkdtemp()

    return Report(Path(test_data_path), Path(out_dir), 'fakeiuud',
                  subject_id='01', packagename='fmriprep')


@pytest.fixture()
def test_report2(bids_sessions):
    out_dir = tempfile.mkdtemp()
    return Report(Path(bids_sessions), Path(out_dir), 'fakeiuud',
                  subject_id='01', packagename='fmriprep')

@pytest.mark.parametrize(
    "orderings,expected_entities,expected_value_combos",
    [
        (['session', 'task', 'run'],
         ['task', 'run'],
         [
            ('faketask', None),
            ('faketask2', None),
            ('faketaskwithruns', 1),
            ('faketaskwithruns', 2),
            ('mixedgamblestask', 1),
            ('mixedgamblestask', 2),
            ('mixedgamblestask', 3),
        ]),
        (['run', 'task', 'session'],
         ['run', 'task'],
         [
            (None, 'faketask'),
            (None, 'faketask2'),
            (1, 'faketaskwithruns'),
            (1, 'mixedgamblestask'),
            (2, 'faketaskwithruns'),
            (2, 'mixedgamblestask'),
            (3, 'mixedgamblestask'),
        ]),
        ([''], [], []),
        (['session'], [], []),
        ([], [], []),
        (['madeupentity'], [], []),
    ]
)
def test_process_orderings_small(test_report1, orderings,
                                 expected_entities, expected_value_combos):
    report = test_report1
    report.init_layout()
    entities, value_combos = report._process_orderings(orderings, report.layout)

    assert entities == expected_entities
    assert expected_value_combos == value_combos


@pytest.mark.parametrize(
    "orderings,expected_entities,first_value_combo,last_value_combo",
    [
        (['session', 'task', 'run'],
         ['session', 'task', 'run'],
         ('1', 't1', None),
         ('pre', 't3', 2),
        ),
        (['run', 'task', 'session'],
         ['run', 'task', 'session'],
         (None, 't1', '1'),
         (2, 't3', 'pre'),
        ),
        ([''], [], None, None),
        (['session'], ['session'], ('1',), ('pre',)),
        ([], [], None, None),
        (['madeupentity'], [], None, None),
    ]
)
def test_process_orderings_large(test_report2, orderings,
                                 expected_entities, first_value_combo,
                                 last_value_combo):
    report = test_report2
    report.init_layout()
    entities, value_combos = report._process_orderings(orderings, report.layout)

    if not value_combos:
        value_combos = [None]

    assert entities == expected_entities
    assert value_combos[0] == first_value_combo
    assert value_combos[-1] == last_value_combo
{'fraction_non_alphanumeric': 0.15869756526840717, 'fraction_numerical': 0.009386916984452919, 'mean_word_length': 2.4737646459500766, 'pattern_counts': {'":': 4, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4923962', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2172, 'n_words': 412}