<commit_before>"""
Test the design_matrix utilities.

Note that the tests just looks whether the data produces has correct dimension,
not whether it is exact
"""

from __future__ import with_statement

import numpy as np
import os.path as osp
import pandas as pd
from nilearn._utils.testing import assert_raises_regex

from nistats.design_matrix import (
    _convolve_regressors, make_design_matrix,
    _cosine_drift, check_design_matrix,
    create_second_level_design)

from nibabel.tmpdirs import InTemporaryDirectory

from nose.tools import assert_true, assert_equal, assert_raises
from numpy.testing import assert_almost_equal, assert_array_equal


# load the spm file to test cosine basis
my_path = osp.dirname(osp.abspath(__file__))
full_path_design_matrix_file = osp.join(my_path, 'spm_dmtx.npz')
DESIGN_MATRIX = np.load(full_path_design_matrix_file)


def design_matrix_light(
    frame_times, paradigm=None, hrf_model='glover',
    drift_model='cosine', period_cut=128, drift_order=1, fir_delays=[0],
    add_regs=None, add_reg_names=None, min_onset=-24, path=None):
    """ Idem make_design_matrix, but only returns the computed matrix
    and associated names """
    dmtx = make_design_matrix(frame_times, paradigm, hrf_model,
    drift_model, period_cut, drift_order, fir_delays,
    add_regs, add_reg_names, min_onset)
    _, matrix, names = check_design_matrix(dmtx)
    return matrix, names


def basic_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets})
    return paradigm


def modulated_block_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 5 + 5 * np.random.rand(len(onsets))
    values = 1 + np.random.rand(len(onsets))
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration,
                          'modulation': values})
    return paradigm


def modulated_event_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    values = 1 + np.random.rand(len(onsets))
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'modulation': values})
    return paradigm


def block_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 5 * np.ones(9)
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration})
    return paradigm


def test_cosine_drift():
    # add something so that when the tests are launched
    #from a different directory
    spm_drifts = DESIGN_MATRIX['cosbf_dt_1_nt_20_hcut_0p1']
    tim = np.arange(20)
    P = 10  # period is half the time, gives us an order 4
    nistats_drifts = _cosine_drift(P, tim)
    assert_almost_equal(spm_drifts[:, 1:], nistats_drifts[:, : - 1])
    # nistats_drifts is placing the constant at the end [:, : - 1]

<commit_msg>add duration field to test-dataframes for paradigm & design matrix creation<commit_after>"""
Test the design_matrix utilities.

Note that the tests just looks whether the data produces has correct dimension,
not whether it is exact
"""

from __future__ import with_statement

import numpy as np
import os.path as osp
import pandas as pd
from nilearn._utils.testing import assert_raises_regex

from nistats.design_matrix import (
    _convolve_regressors, make_design_matrix,
    _cosine_drift, check_design_matrix,
    create_second_level_design)

from nibabel.tmpdirs import InTemporaryDirectory

from nose.tools import assert_true, assert_equal, assert_raises
from numpy.testing import assert_almost_equal, assert_array_equal


# load the spm file to test cosine basis
my_path = osp.dirname(osp.abspath(__file__))
full_path_design_matrix_file = osp.join(my_path, 'spm_dmtx.npz')
DESIGN_MATRIX = np.load(full_path_design_matrix_file)


def design_matrix_light(
    frame_times, paradigm=None, hrf_model='glover',
    drift_model='cosine', period_cut=128, drift_order=1, fir_delays=[0],
    add_regs=None, add_reg_names=None, min_onset=-24, path=None):
    """ Idem make_design_matrix, but only returns the computed matrix
    and associated names """
    dmtx = make_design_matrix(frame_times, paradigm, hrf_model,
    drift_model, period_cut, drift_order, fir_delays,
    add_regs, add_reg_names, min_onset)
    _, matrix, names = check_design_matrix(dmtx)
    return matrix, names


def basic_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 1 * np.ones(9)
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration})
    return paradigm


def modulated_block_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 5 + 5 * np.random.rand(len(onsets))
    values = 1 + np.random.rand(len(onsets))
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration,
                          'modulation': values})
    return paradigm


def modulated_event_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 1 * np.ones(9)
    values = 1 + np.random.rand(len(onsets))
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration,
                          'modulation': values})
    return paradigm


def block_paradigm():
    conditions = ['c0', 'c0', 'c0', 'c1', 'c1', 'c1', 'c2', 'c2', 'c2']
    onsets = [30, 70, 100, 10, 30, 90, 30, 40, 60]
    duration = 5 * np.ones(9)
    paradigm = pd.DataFrame({'trial_type': conditions,
                          'onset': onsets,
                          'duration': duration})
    return paradigm


def test_cosine_drift():
    # add something so that when the tests are launched
    #from a different directory
    spm_drifts = DESIGN_MATRIX['cosbf_dt_1_nt_20_hcut_0p1']
    tim = np.arange(20)
    P = 10  # period is half the time, gives us an order 4
    nistats_drifts = _cosine_drift(P, tim)
    assert_almost_equal(spm_drifts[:, 1:], nistats_drifts[:, : - 1])
    # nistats_drifts is placing the constant at the end [:, : - 1]


{'fraction_non_alphanumeric': 0.12412177985948478, 'fraction_numerical': 0.040983606557377046, 'mean_word_length': 3.186887254901961, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4505990', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2346, 'n_words': 631}