icometrix / dicom2nifti

MIT License
339 stars 79 forks source link

What should pass to the function sort_dicoms? #89

Open shuhaojie opened 3 years ago

shuhaojie commented 3 years ago

In the documentation of sort_dicoms(in common.py), it says the input is :param dicoms: list of dicoms. However, as I pass a list of dicoms:['~home/1.dcm', '~home/2.dcm'] there is an error dicom_input_sorted_x = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[0])) AttributeError: 'str' object has no attribute 'ImagePositionPatient'. What should I pass?

abrys commented 3 years ago

@shuhaojie The sort_dicoms function takes a list if dicom objects as read by pydicom, the documentation is maybe not clear enough. For example use the read_dicom_directory to read all dicom files in a directory or just loop over the files ad use pydicom.read_file or our wrapper compressed_dicom.read_file to read them manually.

shuhaojie commented 3 years ago

@shuhaojie The sort_dicoms function takes a list if dicom objects as read by pydicom, the documentation is maybe not clear enough. For example use the read_dicom_directory to read all dicom files in a directory or just loop over the files ad use pydicom.read_file or our wrapper compressed_dicom.read_file to read them manually.

Thanks @abrys You are right. I use pydicom.dcmread() to read every dicom files in my list. It did work out. The question is it seems to return a metadata. For what I am expecting is it returns a sorted list. Below is my code, anything wrong? Appreciate your work. dicom_path = '/home/test' dicom_list = os.listdir(dicom_path) file_list = [] for dicom in dicom_list: ds = pydicom.dcmread(dicom_path+'/'+dicom) file_list.append(ds) sorted_file = sort_dicoms(file_list)

abrys commented 3 years ago

@shuhaojie You provide it with the list of read dicom objects and it returns a sorted list of the same dicom objects (not the files). One example usage would be here https://github.com/icometrix/dicom2nifti/blob/master/dicom2nifti/convert_generic.py#L57.