0

I have a pipeline who need to take picture and with N4 algorithm correct them.

Problem is, my pipeline crash and I think for now the reason is the iteration of the dict where are stored the images. I did a test pipeline where everything worked well and for the main one it doesn't work (the organization in the folder changed too, so i tried to re-adapt the main one).

Here is the function to fetch the pictures and store them in a dict:

def fetch_image():
    
    original_path = os.getcwd()
    path = "{}/data".format(original_path)
    
    img_dict = {}
    
    for dirs_patient in os.listdir(path):
        path_patient = path + "/{}".format(dirs_patient)
        try:
            for dirs_type in os.listdir(path_patient):
                    if "gado" and "nii.gz" in dirs_type.lower():
                     
                        img = ants.image_read("{}/{}".format(path_patient, dirs_type))
                        logging.info("Image patient {} found".format(dirs_patient))
                        img_dict.update({'Img patient {}'.format(dirs_patient) : img})
                    
        except:
            logging.warning("Error while fetching images".format(dirs_patient))
                
                            
                

    return img_dict

With this output:

img = fetch_image()
print(len(img))
print(img)

10
{'Img patient P105': ANTsImage
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (256, 256, 493, 2)
     Spacing    : (2.3438, 2.3438, 2.78, 1.0)
     Origin     : (-298.8281, 298.8281, -1398.76, 0.0)
     Direction  : [ 1.  0.  0.  0.  0. -1.  0.  0.  0.  0.  1.  0.  0.  0.  0.  1.]
, 'Img patient P115': ANTsImage (LPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (512, 512, 176)
     Spacing    : (0.8203, 0.8203, 1.4)
     Origin     : (205.6953, 213.5723, -388.961)
     Direction  : [-1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P114': ANTsImage (RPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (256, 256, 453)
     Spacing    : (2.3438, 2.3438, 2.78)
     Origin     : (-298.8281, 298.8281, -1340.0601)
     Direction  : [ 1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P106': ANTsImage
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (256, 256, 413, 2)
     Spacing    : (2.3438, 2.3438, 2.78, 1.0)
     Origin     : (-298.8281, 298.8281, -1169.6899, 0.0)
     Direction  : [ 1.  0.  0.  0.  0. -1.  0.  0.  0.  0.  1.  0.  0.  0.  0.  1.]
, 'Img patient P110': ANTsImage (RPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (256, 256, 333)
     Spacing    : (1.9531, 1.9531, 2.78)
     Origin     : (-249.0234, 249.0234, -978.96)
     Direction  : [ 1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P112': ANTsImage (LPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (512, 512, 176)
     Spacing    : (0.7422, 0.7422, 1.4)
     Origin     : (215.7982, 196.2972, -391.537)
     Direction  : [-1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P108': ANTsImage (LPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (512, 512, 152)
     Spacing    : (0.7422, 0.7422, 1.4)
     Origin     : (206.3652, 216.1442, -425.117)
     Direction  : [-1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P111': ANTsImage (LPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (512, 512, 176)
     Spacing    : (0.7422, 0.7422, 1.4)
     Origin     : (192.8342, 201.9422, -405.621)
     Direction  : [-1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P116': ANTsImage (LPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (512, 512, 184)
     Spacing    : (0.7422, 0.7422, 1.4)
     Origin     : (181.2592, 215.7472, -375.792)
     Direction  : [-1.  0.  0.  0. -1.  0.  0.  0.  1.]
, 'Img patient P113': ANTsImage (RPI)
     Pixel Type : float (float32)
     Components : 1
     Dimensions : (256, 256, 333)
     Spacing    : (1.9531, 1.9531, 2.78)
     Origin     : (-249.0234, 249.0234, -993.21)
     Direction  : [ 1.  0.  0.  0. -1.  0.  0.  0.  1.]
}

When I try to use this dict to normalize those picture, the pipeline crash, so i tried to understand where is the bug and I've tried this:

def normalize_img(img_dict = {}):
    i = 1
    for key, value in img_dict.items():
        img = value
        print(i) 
        i+=1
        dirs_original = key[-4:]
        print(dirs_original)
        
       function_n4(img) #this is, i guess, where the pipeline crash

Output:

test = normalize_img(img_dict= img)
test

1
P105
3
P115
3
P114
3
P106
3
P110
3
P112
3
P108
3
P111
3
P116
3
P113

The problem is, I don't understand why the iteration doesn't go to 10 in the function but when i try outside the function like here:

i = 1
for key, value in img.items():
    print(i)
    i += 1

Output:

1
2
3
4
5
6
7
8
9
10

It work well and on the test pipeline everything worked well.

Thanks for the help

Royal
  • 95
  • 1
  • 7
  • Interesting... can you share the code of `function_n4` (or a minimal equivalent)? May be the variable i is changed in that? – Matteo Zanoni May 11 '21 at 14:25
  • Does `function_n4(img)` modify the dictionary `img` ? Are you using python3 ? If so, this link https://stackoverflow.com/a/6777569/10701340 may have answer to the above behavior. – sri May 11 '21 at 14:55
  • Hello sorry for the long time i was working on an other project. I kinda fixed my problem by changing how the files were stored and the program run smoothly. for the function_n4 you can find it here: https://antspy.readthedocs.io/en/latest/utils.html?highlight=N4%20bias%20field%20correction#ants.n4_bias_field_correction . It correct the intensity of the picture, so i would say it change the variable inside the dict – Royal Jun 28 '21 at 08:01

0 Answers0