Home > Blockchain >  Using for loop to create a dictionary by function
Using for loop to create a dictionary by function

Time:10-28

I managed to use for loop to create a nested function. This is my code:

sub="01"
locals()['MSC'   sub] = {}
seeds = ["aHPC", "pHPC", "FPl", "FPm", "ERC", "PHC", "PRC", "46"] 
masks = ["whole", "ERC", "PFl", "FPm"]
hemispheres = ["R", "L"] 
sessions = ["01", "02"] 
for hemi in hemispheres:
    for seed in seeds:
        for mask in masks:
            current_pair = hemi   "_"   seed   "_"   mask
            locals()['MSC'   sub][current_pair] = {}
            for ses in sessions:
                input_dir = "/1D_fc/"   sub   "_"   ses   "/"
                mean_fn = input_dir   "R.Fim."   hemi   "_"   seed   "_"   mask   ".nii.gz"  
                try:
                    mean_brain=nib.load(mean_fn)
                    #control_brain=nib.load(control_fn)
                except:
                    "couldn't load"
                
                mean_data = mean_brain.get_data()  # 3D array; get the dimensions; type(mean_control_data)

                meanDataVector = mean_data.ravel()
                locals()['MSC' sub][current_pair][ses] = meanDataVector

When put all these code into a function, which request for the sub variable as a string. It does not return the nested dictionary as I needed.

def CreateDict(sub):
    subject=sub
    locals()['MSC'   sub] = {}
    seeds = ["aHPC", "pHPC", "FPl", "FPm", "ERC", "PHC", "PRC", "46"] 
    masks = ["whole", "ERC", "PFl", "FPm"] 
    hemispheres = ["R", "L"] 
    sessions = ["01", "02"] 
    for hemi in hemispheres:
        for seed in seeds:
            for mask in masks:
                current_pair = hemi   "_"   seed   "_"   mask
                locals()['MSC'   sub][current_pair] = {}
                for ses in sessions:
                    input_dir = "/1D_fc/"   sub   "_"   ses   "/"
                    mean_fn = input_dir   "R.Fim."   hemi   "_"   seed   "_"   mask   ".nii.gz"  
                    try:
                        mean_brain=nib.load(mean_fn)
                        
                    except:
                        "couldn't load"
                    
                    mean_data = mean_brain.get_data()  # 3D array; get the dimensions; type(mean_control_data)
                    
                    meanDataVector = mean_data.ravel()
                    locals()['MSC' sub][current_pair][ses] = meanDataVector
    return locals()['MSC'   subject]
sub="01"
CreateDict(sub)

There is no error message for the function.

Any insight on this? Thanks!

CodePudding user response:

Don't use locals like that, just create the dictionary inside your function, return it, and assign it to a variable at the call site. Something like this:

def create_dict(sub):
    data = {} # no need for locals
    # ...
    return data

msc01 = create_dict('01')
# proceed to use msc01

If you want to create and use multiple dictionaries for different sub values, consider using another dictionary for that as well:

msc = {}
for value in ('01', '02', '03'): 
    msc[value] = create_dict(value)

# now you have msc['01'] etc.
  • Related