11''' Testing module for nibetaseries.interfaces.nistats '''
22import os
33import json
4+ import re
45
6+ import nibabel as nib
7+ from nilearn .image import load_img
8+ import pandas as pd
59import pytest
610
711
@@ -16,7 +20,6 @@ def test_lss_beta_series(sub_metadata, preproc_file, sub_events,
1620 """Test lss interface with nibabel nifti images
1721 """
1822 if use_nibabel :
19- import nibabel as nib
2023 bold_file = nib .load (str (preproc_file ))
2124 mask_file = nib .load (str (brainmask_file ))
2225 else :
@@ -39,16 +42,35 @@ def test_lss_beta_series(sub_metadata, preproc_file, sub_events,
3942 high_pass = 0.008 )
4043 res = beta_series .run ()
4144
45+ events_df = pd .read_csv (str (sub_events ), sep = '\t ' )
46+ trial_types = events_df ['trial_type' ].unique ()
47+
48+ # check for the correct number of beta maps
49+ assert len (trial_types ) == len (res .outputs .beta_maps )
50+
51+ input_img_dim = load_img (bold_file ).shape [:3 ]
4252 for beta_map in res .outputs .beta_maps :
53+ trial_type = re .search (r'desc-([A-Za-z0-9]+)_' , beta_map ).groups ()[0 ]
54+ # check if correct trial type is made
55+ assert trial_type in trial_types
56+ # check if output is actually a file
4357 assert os .path .isfile (beta_map )
58+ # check if image dimensions are correct
59+ assert input_img_dim == load_img (beta_map ).shape [:3 ]
60+ # check if number of trials are correct
61+ assert (events_df ['trial_type' ] == trial_type ).sum () == load_img (beta_map ).shape [- 1 ]
62+ # clean up
4463 os .remove (beta_map )
4564
65+ # check residual image
66+ assert load_img (bold_file ).shape == load_img (res .outputs .residual ).shape
67+ os .remove (res .outputs .residual )
68+
4669
4770@pytest .mark .parametrize ("use_nibabel" , [(True ), (False )])
4871def test_fs_beta_series (sub_metadata , preproc_file , sub_events ,
4972 confounds_file , brainmask_file , use_nibabel ):
5073 if use_nibabel :
51- import nibabel as nib
5274 bold_file = nib .load (str (preproc_file ))
5375 mask_file = nib .load (str (brainmask_file ))
5476 else :
@@ -73,16 +95,35 @@ def test_fs_beta_series(sub_metadata, preproc_file, sub_events,
7395 high_pass = 0.008 )
7496 res = beta_series .run ()
7597
98+ events_df = pd .read_csv (str (sub_events ), sep = '\t ' )
99+ trial_types = events_df ['trial_type' ].unique ()
100+
101+ # check for the correct number of beta maps
102+ assert len (trial_types ) * len (fir_delays ) == len (res .outputs .beta_maps )
103+
104+ input_img_dim = load_img (bold_file ).shape [:3 ]
76105 for beta_map in res .outputs .beta_maps :
106+ trial_type = re .search (r'desc-([A-Za-z0-9]+)Delay[0-9]+Vol_' , beta_map ).groups ()[0 ]
107+ # check if correct trial type is made
108+ assert trial_type in trial_types
109+ # check if output is actually a file
77110 assert os .path .isfile (beta_map )
111+ # check if image dimensions are correct
112+ assert input_img_dim == load_img (beta_map ).shape [:3 ]
113+ # check if number of trials are correct
114+ assert (events_df ['trial_type' ] == trial_type ).sum () == load_img (beta_map ).shape [- 1 ]
115+ # clean up
78116 os .remove (beta_map )
79117
118+ # check residual image
119+ assert load_img (bold_file ).shape == load_img (res .outputs .residual ).shape
120+ os .remove (res .outputs .residual )
121+
80122
81123@pytest .mark .parametrize ("use_nibabel" , [(True ), (False )])
82124def test_lsa_beta_series (sub_metadata , preproc_file , sub_events ,
83125 confounds_file , brainmask_file , use_nibabel ):
84126 if use_nibabel :
85- import nibabel as nib
86127 bold_file = nib .load (str (preproc_file ))
87128 mask_file = nib .load (str (brainmask_file ))
88129 else :
@@ -105,10 +146,30 @@ def test_lsa_beta_series(sub_metadata, preproc_file, sub_events,
105146 high_pass = 0.008 )
106147 res = beta_series .run ()
107148
149+ events_df = pd .read_csv (str (sub_events ), sep = '\t ' )
150+ trial_types = events_df ['trial_type' ].unique ()
151+
152+ # check for the correct number of beta maps
153+ assert len (trial_types ) == len (res .outputs .beta_maps )
154+
155+ input_img_dim = load_img (bold_file ).shape [:3 ]
108156 for beta_map in res .outputs .beta_maps :
157+ trial_type = re .search (r'desc-([A-Za-z0-9]+)_' , beta_map ).groups ()[0 ]
158+ # check if correct trial type is made
159+ assert trial_type in trial_types
160+ # check if output is actually a file
109161 assert os .path .isfile (beta_map )
162+ # check if image dimensions are correct
163+ assert input_img_dim == load_img (beta_map ).shape [:3 ]
164+ # check if number of trials are correct
165+ assert (events_df ['trial_type' ] == trial_type ).sum () == load_img (beta_map ).shape [- 1 ]
166+ # clean up
110167 os .remove (beta_map )
111168
169+ # check residual image
170+ assert load_img (bold_file ).shape == load_img (res .outputs .residual ).shape
171+ os .remove (res .outputs .residual )
172+
112173
113174def test_lss_events_iterator (sub_events ):
114175 # all but the first instance of waffle
0 commit comments