From cbe115bb8e981f142c9022bbf23f03d707b38192 Mon Sep 17 00:00:00 2001 From: James Kent Date: Fri, 20 Oct 2017 10:17:29 -0500 Subject: [PATCH] FIX: error fixes --- NiBetaSeries/workflows/analysis.py | 2 +- NiBetaSeries/workflows/model.py | 37 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/NiBetaSeries/workflows/analysis.py b/NiBetaSeries/workflows/analysis.py index 95794ea..fcb0e94 100644 --- a/NiBetaSeries/workflows/analysis.py +++ b/NiBetaSeries/workflows/analysis.py @@ -80,7 +80,7 @@ def check_imgs_length(nifti_lst): import nibabel as nib # remove nifti from list if it's less than 3 volumes in the 4th dimension # can't do correlations with less than 3 volumes - return [nifti for nifti in nifti_lst if nib.load(nifti).shape[3] > 2] + return [nifti for nifti in nifti_lst if len(nib.load(nifti).shape) == 4 and nib.load(nifti).shape[3] > 2] # mixes the rois and betaseries_files so I can run all combinations def cart(rois, bsfiles): diff --git a/NiBetaSeries/workflows/model.py b/NiBetaSeries/workflows/model.py index 7c356ca..6ec2783 100644 --- a/NiBetaSeries/workflows/model.py +++ b/NiBetaSeries/workflows/model.py @@ -50,6 +50,7 @@ def trial_events_iterator(events): verbose=1, n_jobs=n_jobs) events_df = pd.read_csv(events, sep='\t', index_col=None) + num_events = len(events_df) # initialize trial type tracker t_type_prev = 0 @@ -59,19 +60,6 @@ def trial_events_iterator(events): betaseries_files = [] beta_path = os.getcwd() for t_ev_idx, (t_ev, t_type, t_idx) in enumerate(trial_events_iterator(events_df)): - # if we have collected all betas for a trial type... - if t_type_prev != t_type and t_type_prev != 0: - # concatenate and save the 4d betaseries - betaseries = nib.funcs.concat_images(beta_list) - betaseries_file = os.path.join( - beta_path, 'trialtype-{}_betaseries.nii.gz'.format(t_type_prev)) - print('betaseries: {}'.format(betaseries_file)) - nib.save(betaseries, betaseries_file) - # add the 4d betaseries to the output list - betaseries_files.append(betaseries_file) - beta_list = [] - - t_type_prev = t_type if not os.path.exists(beta_path): os.makedirs(beta_path) @@ -88,12 +76,25 @@ def trial_events_iterator(events): else: model.refit_run_design(bold, t_ev, None) # had to remove conf - beta = model.compute_contrast(t_type, output_type='effect_size') - beta_list.append(beta) - # nib.save(beta, beta_file) + beta = model.compute_contrast(t_type, output_type='effect_size') + if t_type_prev != t_type and t_type_prev != 0 or t_ev_idx == (num_events-1): + if t_ev_idx == (num_events-1): + beta_list.append(beta) + # concatenate and save the 4d betaseries + betaseries = nib.funcs.concat_images(beta_list) + betaseries_file = os.path.join( + beta_path, 'trialtype-{}_betaseries.nii.gz'.format(t_type_prev)) + print('betaseries: {}'.format(betaseries_file)) + nib.save(betaseries, betaseries_file) + # add the 4d betaseries to the output list + betaseries_files.append(betaseries_file) + beta_list = [] + + t_type_prev = t_type + beta_list.append(beta) + # nib.save(beta, beta_file) - print('Done in %d seconds' % - (time.time() - start_time)) + print('Done in %d seconds' % (time.time() - start_time)) # return the 4d betaseries files print("all betaseries_files: {}".format(betaseries_files))