0

我的目标是分别从多个 numpy 数组中生成多个 png 文件,这些数组是从我的 HD 中的医学图像加载的。为了让事情变得更快,我正在使用 dask 延迟。这是我的工作代码:

import os.path
from glob import glob

import nibabel as nib
import numpy as np
from dask import delayed

def process(data):
    # Need to have the import inside so that multiprocessing works.
    # Apparently doesn't solve the issue anyway..
    import matplotlib.pyplot as plt
    outpath = '/Users/user/outputdir/'
    name = os.path.basename(data.get_filename())
    savename = name[:name.index('.')] + '.png'

    plt.imshow(np.rot90(data.get_data()[15:74, 6:82, 18, 0]),
               extent=[0, 1, 0, 1], aspect=1.28, cmap='gray')
    plt.axis('off')
    out = os.path.join(outpath, savename)
    plt.savefig(out)
    plt.close()
    return out


L = []
for fn in glob("/Users/user/imagefiles/mb*.nii.gz"):
    nifti = delayed(nib.load)(fn)
    outpng = delayed(process)(nifti)
    L.append(outpng)

results = delayed(print)(L)
results.compute()

我的问题是,每次运行后,一些输出图像都是空的(png 中没有任何内容),并且哪些图像是空的似乎很随机,因为所有输入数据都是有效的。

我怀疑这是多处理和 matplotlib 的问题,如其他相关线程中所见。

有人对如何使用它有建议dask吗?

编辑:最小的工作示例

import os.path
import random
import string

import numpy as np
from dask import delayed

def gendata(fn):
    return

def process(data):
    # Need to have the import inside so that multiprocessing works.
    import matplotlib.pyplot as plt

    outpath = '/Users/user/Pictures/test/'
    name = ''.join(random.choices(string.ascii_lowercase, k=10))
    savename = name + '.png'

    data = np.random.randint(0, 255, size=(100,100,20,2))

    plt.imshow(np.rot90(data[15:74, 6:82, 18, 0]),
               extent=[0, 1, 0, 1], aspect=1.28, cmap='gray')
    plt.axis('off')
    out = os.path.join(outpath, savename)
    plt.savefig(out)
    plt.close()
    return out

L = []
for fn in range(0, 10):
    nifti = delayed(gendata)(fn)
    outpng = delayed(process)(nifti)
    L.append(outpng)

results = delayed(print)(L)
results.compute()
4

0 回答 0