将列表转换为 Numpy 数组仅使用一个数据集丢失其三个轴中的两个

Convert a list into a Numpy array lose two of its three axis only with one dataset

我有一个 python 代码可以使用 SimpleITK 库读取 NIFTI 图像。然后它将这些图像转换为 Numpy 数组。然后,我将 Numpy 数组扩展到一个列表中。

我有 20 个 FLAIR.nii.gz 个文件。他们每个人都有48个切片。

当我拥有所有 20 名患者的所有 48 个切片时,我将列表转换为 Numpy 数组。

我这样做是因为我是 Python 的新手,我不知道任何其他方法。

密码是:

import os
import SimpleITK as sitk
import numpy as np

flair_dataset = []

# For each patient directory
# data_path is a list with all of the patient's directory.
for i in data_path:

    img_path = os.path.join(file_path, i, 'pre')
    mask_path = os.path.join(file_path, i)

    for name in glob.glob(img_path+'/FLAIR*'):
        # Reads images using SimpleITK.
        brain_image = sitk.ReadImage(name)
        # Get a numpy array from a SimpleITK Image.
        brain_array = sitk.GetArrayFromImage(brain_image)

        flair_dataset.extend(brain_array)

        if debug:
            print('brain_image size: ', brain_image.GetSize())
            print('brain_array Shape: ', brain_array.shape)
            print('flair_dataset length:', len(flair_dataset))

print('flair_dataset length: ', len(flair_dataset))
print('flair_dataset[1] type: ', print(type(flair_dataset[1])))
print('flair_dataset[1] shape: ', print(flair_dataset[1].shape))

flair_array = np.array(flair_dataset)
print('flair_array.shape: ', flair_array.shape)
print('flair_array.dtype: ', flair_array.dtype)

此代码生成此输出(所有 FLAIR.nii.gz 个文件具有相同的形状):

data_path =  ['68', '55', '50', '61', '63', '52', '51', '60', '67', '58', '59', '53', '69', '64', '56', '65', '54', '62', '66', '57']
patient_data_path =  68
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 48
Mask list length:  48

patient_data_path =  55
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 96
Mask list length:  96

patient_data_path =  50
brain_image size:  (256, 232, 48)
brain_array Shape:  (48, 232, 256)
flair_dataset length: 144
WMH image Size:  (256, 232, 48)
WMH array Shape:  (48, 232, 256)
Mask list length:  144

patient_data_path =  61
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 192
Mask list length:  192

patient_data_path =  63
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 240
Mask list length:  240

patient_data_path =  52
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 288
Mask list length:  288

patient_data_path =  51
brain_image size:  (256, 232, 48)
brain_array Shape:  (48, 232, 256)
flair_dataset length: 336
WMH image Size:  (256, 232, 48)
WMH array Shape:  (48, 232, 256)
Mask list length:  336

patient_data_path =  60
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 384
Mask list length:  384

patient_data_path =  67
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 432
Mask list length:  432

patient_data_path =  58
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 480
Mask list length:  480

patient_data_path =  59
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 528
Mask list length:  528

patient_data_path =  53
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 576
Mask list length:  576

patient_data_path =  69
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 624
Mask list length:  624

patient_data_path =  64
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 672
Mask list length:  672

patient_data_path =  56
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 720
Mask list length:  720

patient_data_path =  65
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 768
Mask list length:  768

patient_data_path =  54
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 816
Mask list length:  816

patient_data_path =  62
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 864
Mask list length:  864

patient_data_path =  66
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 912
Mask list length:  912

patient_data_path =  57
brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)
flair_dataset length: 960
Mask list length:  960

代码的最终输出是:

flair_dataset length:  960
mask_dataset length:  960
flair_dataset[1] type:  <class 'numpy.ndarray'>
flair_dataset[1] shape:  (256, 232)
flair_array.shape:  (960,)
flair_array.dtype:  object

我的问题:

我不明白为什么 flair_array 有这个形状:(960,)flair_array dtypeobject.

我已经尝试过相同的代码,没有做任何更改,并且运行良好。它也有 20 个患者,每个 FLAIR.nii.gz 文件也有 48 个切片。

其输出:

data_path =  ['39', '31', '2', '23', '35', '29', '17', '49', '27', '8', '33', '4', '19', '41', '37', '11', '25', '6', '0', '21']

patient_data_path =  39
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 48
Mask list length:  48

patient_data_path =  31
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 96
Mask list length:  96

patient_data_path =  2
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 144
Mask list length:  144

patient_data_path =  23
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 192
Mask list length:  192

patient_data_path =  35
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 240
Mask list length:  240

patient_data_path =  29
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 288
Mask list length:  288

patient_data_path =  17
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 336
Mask list length:  336

patient_data_path =  49
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 384
Mask list length:  384

patient_data_path =  27
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 432
Mask list length:  432

patient_data_path =  8
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 480
Mask list length:  480

patient_data_path =  33
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 528
Mask list length:  528

patient_data_path =  4
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 576
Mask list length:  576

patient_data_path =  19
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 624
Mask list length:  624

patient_data_path =  41
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 672
Mask list length:  672

patient_data_path =  37
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 720
Mask list length:  720

patient_data_path =  11
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 768
Mask list length:  768

patient_data_path =  25
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 816
Mask list length:  816

patient_data_path =  6
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 864
Mask list length:  864

patient_data_path =  0
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 912
Mask list length:  912

patient_data_path =  21
brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)
flair_dataset length: 960
Mask list length:  960

这是此数据集的最终输出:

flair_dataset length:  960
mask_dataset length:  960
flair_dataset[1] type:  <class 'numpy.ndarray'>
flair_dataset[1] shape:  (240, 240)
flair_array.shape:  (960, 240, 240)
flair_array.dtype:  float32

对于第二个数据集,flair_arrayfloat32

为什么第一个 flair_array 形状是 (960,)

更新:
在这两个数据集中,brain_array.dtype 总是 float32

我认为 flair_dataset.extend(brain_array) 只是用加载的数组扩展了 flair_dataset 列表。 所以在 flair_dataset 中有一个数组,所有 960 张图像都堆叠在一起。每张图片大小为240, 240.

你是批量加载图片,还是在图片上包含48层的深度? 如果是这样,请尝试附加数组而不是扩展它。然后你把加载的数组放在它的列表条目中。

一种情况

flair_array.shape:  (960,)
flair_array.dtype:  object

在另一个

flair_array.shape:  (960, 240, 240)
flair_array.dtype:  float32

你制作这些:

flair_array = np.array(flair_dataset)

如果flair_dataset的所有元素具有相同的形状,它可以从它们创建一个多维数组。

但是如果列表中的一个或多个数组的形状不同,它就不得不放弃多维目标,而只是制作一个对象 dtype 数组,它非常像一个列表——包含引用到原始数组。

原始列表中的大部分元素是

brain_image size:  (232, 256, 48)
brain_array Shape:  (48, 256, 232)

但我也看到了一些

brain_image size:  (256, 232, 48)
brain_array Shape:  (48, 232, 256)

第二组都是

brain_image size:  (240, 240, 48)
brain_array Shape:  (48, 240, 240)

当人们询问 (n,) 形状时,当他们期望 (n,m,p) 时,我怀疑第一个具有由元素形状混合引起的 object dtype。这就是我问 dtype.

的原因