Skip to content

Commit

Permalink
refactor design matrix related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Aug 30, 2023
1 parent c859e91 commit 202a58a
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 449 deletions.
2 changes: 1 addition & 1 deletion nilearn/glm/first_level/design_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def check_design_matrix(design_matrix):
Per-event onset time (in seconds)
"""
names = [name for name in design_matrix.keys()]
names = list(design_matrix.keys())
frame_times = design_matrix.index
matrix = design_matrix.values
return frame_times, matrix, names
Expand Down
9 changes: 9 additions & 0 deletions nilearn/glm/first_level/experimental_paradigm.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ def check_events(events):
)
events_copy["trial_type"] = "dummy"

conditions_with_null_duration = events_copy["trial_type"][
events_copy["duration"] == 0
].unique()
if len(conditions_with_null_duration) > 0:
warnings.warn(

Check warning on line 78 in nilearn/glm/first_level/experimental_paradigm.py

View check run for this annotation

Codecov / codecov/patch

nilearn/glm/first_level/experimental_paradigm.py#L78

Added line #L78 was not covered by tests
"The following conditions contain events with null duration:\n"
f"{', '.join(conditions_with_null_duration)}."
)

# Handle modulation
if "modulation" in events_copy.columns:
print(
Expand Down
104 changes: 104 additions & 0 deletions nilearn/glm/tests/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import numpy as np
import pandas as pd

from nilearn.conftest import _rng


def _conditions():
return ["c0", "c0", "c0", "c1", "c1", "c1", "c2", "c2", "c2"]


def _onsets():
return [30, 70, 100, 10, 30, 90, 30, 40, 60]


def _durations():
return np.ones(len(_onsets()))


def _modulated_event_paradigm():
events = pd.DataFrame(
{
"trial_type": _conditions(),
"onset": _onsets(),
"duration": _durations(),
"modulation": _rng().uniform(size=len(_onsets())),
}
)
return events


def _block_paradigm():
events = pd.DataFrame(
{
"trial_type": _conditions(),
"onset": _onsets(),
"duration": 5 * _durations(),
}
)
return events


def _modulated_block_paradigm():
durations = 5 + 5 * _rng().uniform(size=len(_onsets()))
modulation = 1 + _rng().uniform(size=len(_onsets()))
events = pd.DataFrame(
{
"trial_type": _conditions(),
"onset": _onsets(),
"duration": durations,
"modulation": modulation,
}
)
return events


def _spm_paradigm(block_duration):
frame_times = np.linspace(0, 99, 100)
conditions = ["c0", "c0", "c0", "c1", "c1", "c1", "c2", "c2", "c2"]
onsets = [30, 50, 70, 10, 30, 80, 30, 40, 60]
durations = block_duration * np.ones(len(onsets))
events = pd.DataFrame(
{"trial_type": conditions, "onset": onsets, "duration": durations}
)
return events, frame_times


def _design_with_null_duration():
durations = _durations()
durations[2] = 0
durations[5] = 0
durations[8] = 0
events = pd.DataFrame(
{
"trial_type": _conditions(),
"onset": _onsets(),
"duration": durations,
}
)
return events


def _design_with_nan_duration():
durations = _durations()
durations[2] = np.nan
durations[5] = np.nan
durations[8] = np.nan
events = pd.DataFrame(
{
"trial_type": _conditions(),
"onset": _onsets(),
"duration": durations,
}
)
return events


def _duplicate_events_paradigm():
conditions = ["c0", "c0", "c0", "c0", "c1", "c1"]
onsets = [10, 30, 70, 70, 10, 30]
durations = [1.0, 1.0, 1.0, 1.0, 1.0, 1]
events = pd.DataFrame(
{"trial_type": conditions, "onset": onsets, "duration": durations}
)
return events

0 comments on commit 202a58a

Please sign in to comment.