在python函数中迭代dict时遇到问题

2024-04-29 12:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个管道谁需要采取的照片和与N4算法纠正他们

问题是,我的管道崩溃了,我认为现在的原因是dict的迭代,图像存储在哪里。我做了一个测试管道,在那里所有的东西都工作得很好,而对于主管道它不工作(文件夹中的组织也发生了变化,所以我尝试重新调整主管道)

以下是获取图片并将其存储在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

使用此输出:

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.]
}

当我试图用这个dict来规范这些图片时,管道崩溃了,所以我试图理解错误在哪里,我尝试了以下方法:

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

输出:

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

问题是,我不明白为什么迭代没有在函数中变为10,但当我在函数外尝试时,如下图所示:

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

输出:

1
2
3
4
5
6
7
8
9
10

它运行良好,在测试管道上,一切都运行良好

谢谢你的帮助


Tags: imgtypecomponentsoriginfloatdictdimensionsdirs