diff --git a/brainiak/utils/fmrisim.py b/brainiak/utils/fmrisim.py index 41c5d73dd..05838abf1 100644 --- a/brainiak/utils/fmrisim.py +++ b/brainiak/utils/fmrisim.py @@ -523,10 +523,6 @@ def generate_stimfunction(onsets, # Store the weights stimfunction[onset_idx:offset_idx, 0] = [weights[onset_counter]] - # Shorten the data if it's too long - if stimfunction.shape[0] > total_time * temporal_resolution: - stimfunction = stimfunction[0:int(total_time * temporal_resolution), 0] - return stimfunction diff --git a/brainiak/utils/utils.py b/brainiak/utils/utils.py index 1e7084266..56119e02a 100644 --- a/brainiak/utils/utils.py +++ b/brainiak/utils/utils.py @@ -427,10 +427,10 @@ def gen_design(stimtime_files, scan_duration, TR, style='FSL', n_C = len(stimtime_files) # number of conditions n_S = np.size(scan_duration) # number of scans if n_S > 1: - design = [np.empty([int(np.floor(duration / TR)), n_C]) + design = [np.empty([int(np.round(duration / TR)), n_C]) for duration in scan_duration] else: - design = [np.empty([int(np.floor(scan_duration / TR)), n_C])] + design = [np.empty([int(np.round(scan_duration / TR)), n_C])] scan_onoff = np.insert(np.cumsum(scan_duration), 0, 0) if style == 'FSL': design_info = _read_stimtime_FSL(stimtime_files, n_C, n_S, scan_onoff) @@ -445,21 +445,24 @@ def gen_design(stimtime_files, scan_duration, TR, style='FSL', # generate design matrix for i_s in range(n_S): for i_c in range(n_C): - stimfunction = generate_stimfunction( - onsets=design_info[i_s][i_c]['onset'], - event_durations=design_info[i_s][i_c]['duration'], - total_time=scan_duration[i_s], - weights=design_info[i_s][i_c]['weight'], - temporal_resolution=1.0/temp_res) - hrf = _double_gamma_hrf(response_delay=response_delay, - undershoot_delay=undershoot_delay, - response_dispersion=response_disp, - undershoot_dispersion=undershoot_disp, - undershoot_scale=undershoot_scale, - temporal_resolution=1.0/temp_res) - design[i_s][:, i_c] = convolve_hrf( - stimfunction, TR, hrf_type=hrf, scale_function=0, - temporal_resolution=1.0 / temp_res).transpose() * temp_res + if len(design_info[i_s][i_c]['onset']) > 0: + stimfunction = generate_stimfunction( + onsets=design_info[i_s][i_c]['onset'], + event_durations=design_info[i_s][i_c]['duration'], + total_time=scan_duration[i_s], + weights=design_info[i_s][i_c]['weight'], + temporal_resolution=1.0/temp_res) + hrf = _double_gamma_hrf(response_delay=response_delay, + undershoot_delay=undershoot_delay, + response_dispersion=response_disp, + undershoot_dispersion=undershoot_disp, + undershoot_scale=undershoot_scale, + temporal_resolution=1.0/temp_res) + design[i_s][:, i_c] = convolve_hrf( + stimfunction, TR, hrf_type=hrf, scale_function=False, + temporal_resolution=1.0 / temp_res).transpose() * temp_res + else: + design[i_s][:, i_c] = 0.0 # We multiply the resulting design matrix with # the temporal resolution to normalize it. # We do not use the internal normalization diff --git a/tests/utils/example_stimtime_2_AFNI.txt b/tests/utils/example_stimtime_2_AFNI.txt new file mode 100644 index 000000000..39d03d9ee --- /dev/null +++ b/tests/utils/example_stimtime_2_AFNI.txt @@ -0,0 +1 @@ +-10.0 diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 820b648e8..1348790db 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -101,7 +101,8 @@ def test_gen_design(): import os.path files = {'FSL1': 'example_stimtime_1_FSL.txt', 'FSL2': 'example_stimtime_2_FSL.txt', - 'AFNI1': 'example_stimtime_1_AFNI.txt'} + 'AFNI1': 'example_stimtime_1_AFNI.txt', + 'AFNI2': 'example_stimtime_2_AFNI.txt'} for key in files.keys(): files[key] = os.path.join(os.path.dirname(__file__), files[key]) design1 = gen_design(stimtime_files=files['FSL1'], scan_duration=[48, 20], @@ -130,6 +131,11 @@ def test_gen_design(): scan_duration=[48, 20], TR=2, style='AFNI') assert np.all(np.isclose(design1, design6)), ( 'design matrices generated from AFNI style and FSL style do not match') + design7 = gen_design(stimtime_files=[files['AFNI2']], + scan_duration=[48], TR=2, style='AFNI') + assert np.all(design7 == 0.0), ( + 'A negative stimulus onset of AFNI style should result in an all-zero' + + ' design matrix') def test_center_mass_exp():