OSError: [ข้อผิดพลาด 24] มีไฟล์ที่เปิดอยู่มากเกินไปโดยใช้ Nibabel

ฉันมีโปรแกรม python3.6 โดยใช้แพ็คเกจ nibabel เพื่อวิเคราะห์ภาพทางการแพทย์ในรูปแบบ NIFTI

import glob
import nibabel as nib
health = [nib.load(pt) for pt in glob.glob(healthdir+'*.nii')] # len = 200
health_data = [h.get_data() for h in health]

มันเกิดขึ้น OSError: [Errno 24] Too many open files ในบรรทัดสุดท้าย ฉันใช้รหัสต่อไปนี้และพบว่าเกิดข้อผิดพลาดในองค์ประกอบสุดท้าย

health_data = []
for i in range(len(health)):
    try:
        health_data.append(health[i].get_data())
    except:
        print(i) # 199

ฉันได้ลองค้นหาหัวข้อที่เกี่ยวข้องเช่น Nibabel: IOError: [Errno 24 ] มีไฟล์ที่เปิดมากเกินไป: อย่างไรก็ตาม มันไม่ได้แก้ปัญหา นอกจากนี้ ฉันไม่ต้องการใช้ ulimit ขอบคุณ!


person djfire    schedule 04.03.2018    source แหล่งที่มา
comment
สวัสดี @djfire คุณเคยแก้ไขปัญหานี้หรือไม่?   -  person JDoe2    schedule 18.12.2020


คำตอบ (2)


ไม่คุ้นเคยกับนิบาเบล แต่ลอง with

    health_data = []
    for filepath in glob.glob(healthdir+'*.nii'):
       with nib.load(filepath) as health:
           health_data.append(health.get_data())

**ไม่ได้ทดสอบ

person webbyfox    schedule 04.03.2018

คุณอาจต้องลบวัตถุหลังจากใช้งาน

def show_origin_image(name,s=100,max_limit=None, min_limit=None):
    origin = name
    file_name_list = [each for each in os.listdir(origin) if not each.startswith('.')]
    file_name_list = file_name_list[min_limit:max_limit]
    dimension = 2
    width_num = 6
    height_num = math.ceil(len(file_name_list) / width_num)
    plt.figure(figsize=(15, height_num * 2.8))
    data_list = []
    for n,each in enumerate(file_name_list, 1):
        agent = nib.load(os.path.join(origin, each), keep_file_open=False)
        three_d_data = np.asarray(agent.dataobj)
        size = three_d_data.shape
        image = np.take(three_d_data, s, dimension)
        plt.subplot(height_num, width_num, n)
        plt.imshow(image, 'gray')
        plt.axis('off')
        data_list.append(three_d_data)
        # add delete operation!
        del agent
    return data_list
person Weiziyoung    schedule 12.07.2019