This may be a super naive question, but I have a function that I'm trying to loop over with each item in a list being an input. The function is get_t1_file()
def get_bids_nifti(acq):
bids_niftis = [f for f in acq.files if ('info' in f.keys() and 'BIDS' in f.info.keys() and "nii" in f.name)]
if len(bids_niftis) > 0:
return(bids_niftis.pop())
else:
return(None)
def get_t1_file(sess):
'''
Function to pick the most desirable T1 file out of several in a session.
'''
#is_t1 = [any(['T1' in f.classification['Measurement'] for f in a.files \
# if 'Measurement' in f.classification.keys()]) for a in sess.acquisitions()]
#t1_acq = [a for (a, v) in zip(sess.acquisitions(), is_t1) if v]
t1_acq = []
acqlist = sess.acquisitions()
for acq in acqlist:
if any(['T1' in f.classification['Measurement'] for f in acq.files \
if 'Measurement' in f.classification.keys()]):
t1_acq.append(acq)
t1_file = None
for acq in t1_acq:
lab = acq.label.lower()
if ("vnav" in lab) and ("moco" in lab) and ("rms" in lab) and not ("nd" in lab):
t1_file = get_bids_nifti(acq)
return(t1_file)
for acq in t1_acq:
lab = acq.label.lower()
if ("vnav" in lab) and ("rms" in lab) and not ("nd" in lab):
t1_file = get_bids_nifti(acq)
return(t1_file)
for acq in t1_acq:
lab = acq.label.lower()
if ("ax" in lab) and ("mprage" in lab):
t1_file = get_bids_nifti(acq)
return(t1_file)
for acq in t1_acq:
lab = acq.label.lower()
if ("sag" in lab) and ("mprage" in lab):
t1_file = get_bids_nifti(acq)
return(t1_file)
return(t1_file)
My loop to run the function over items in a list is:
t1_files = []
for s in session_ids:
sess = fw.get(s)
get_t1_file(sess)
t1_files.append(t1_file)
But I have an error where the variable that is being returned is not defined. Is there something I'm missing from my loop or function to make sure t1_file
is defined?
Traceback (most recent call last):
File "<ipython-input-16-b85e5ebfcdb1>", line 5, in <module>
t1_files.append(t1_file)
NameError: name 't1_file' is not defined
You have to use the returned value of the function:
t1_files = []
for s in session_ids:
sess = fw.get(s)
t1_files.append(get_t1_file(sess))