Skip to content

Commit cbe115b

Browse files
committed
FIX: error fixes
1 parent 85876a5 commit cbe115b

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

NiBetaSeries/workflows/analysis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def check_imgs_length(nifti_lst):
8080
import nibabel as nib
8181
# remove nifti from list if it's less than 3 volumes in the 4th dimension
8282
# can't do correlations with less than 3 volumes
83-
return [nifti for nifti in nifti_lst if nib.load(nifti).shape[3] > 2]
83+
return [nifti for nifti in nifti_lst if len(nib.load(nifti).shape) == 4 and nib.load(nifti).shape[3] > 2]
8484

8585
# mixes the rois and betaseries_files so I can run all combinations
8686
def cart(rois, bsfiles):

NiBetaSeries/workflows/model.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def trial_events_iterator(events):
5050
verbose=1, n_jobs=n_jobs)
5151

5252
events_df = pd.read_csv(events, sep='\t', index_col=None)
53+
num_events = len(events_df)
5354

5455
# initialize trial type tracker
5556
t_type_prev = 0
@@ -59,19 +60,6 @@ def trial_events_iterator(events):
5960
betaseries_files = []
6061
beta_path = os.getcwd()
6162
for t_ev_idx, (t_ev, t_type, t_idx) in enumerate(trial_events_iterator(events_df)):
62-
# if we have collected all betas for a trial type...
63-
if t_type_prev != t_type and t_type_prev != 0:
64-
# concatenate and save the 4d betaseries
65-
betaseries = nib.funcs.concat_images(beta_list)
66-
betaseries_file = os.path.join(
67-
beta_path, 'trialtype-{}_betaseries.nii.gz'.format(t_type_prev))
68-
print('betaseries: {}'.format(betaseries_file))
69-
nib.save(betaseries, betaseries_file)
70-
# add the 4d betaseries to the output list
71-
betaseries_files.append(betaseries_file)
72-
beta_list = []
73-
74-
t_type_prev = t_type
7563
if not os.path.exists(beta_path):
7664
os.makedirs(beta_path)
7765

@@ -88,12 +76,25 @@ def trial_events_iterator(events):
8876
else:
8977
model.refit_run_design(bold, t_ev, None) # had to remove conf
9078

91-
beta = model.compute_contrast(t_type, output_type='effect_size')
92-
beta_list.append(beta)
93-
# nib.save(beta, beta_file)
79+
beta = model.compute_contrast(t_type, output_type='effect_size')
80+
if t_type_prev != t_type and t_type_prev != 0 or t_ev_idx == (num_events-1):
81+
if t_ev_idx == (num_events-1):
82+
beta_list.append(beta)
83+
# concatenate and save the 4d betaseries
84+
betaseries = nib.funcs.concat_images(beta_list)
85+
betaseries_file = os.path.join(
86+
beta_path, 'trialtype-{}_betaseries.nii.gz'.format(t_type_prev))
87+
print('betaseries: {}'.format(betaseries_file))
88+
nib.save(betaseries, betaseries_file)
89+
# add the 4d betaseries to the output list
90+
betaseries_files.append(betaseries_file)
91+
beta_list = []
92+
93+
t_type_prev = t_type
94+
beta_list.append(beta)
95+
# nib.save(beta, beta_file)
9496

95-
print('Done in %d seconds' %
96-
(time.time() - start_time))
97+
print('Done in %d seconds' % (time.time() - start_time))
9798

9899
# return the 4d betaseries files
99100
print("all betaseries_files: {}".format(betaseries_files))

0 commit comments

Comments
 (0)