def safe_sort_dicom_image_list(dicom_image_list):
    '\n    Sorts a list of DICOM image files based on a DICOM tag value.\n    This is a much safer method than reading SliceLocation.\n    It takes mandatory DICOM fields (Image Position [Patient]) and (Image Orientation [Patient]).\n    The list of DICOM files is sorted by projecting the image position onto the axis normal to the\n    place defined by the image orientation.\n\n    This accounts for differences in patient position (e.g. HFS/FFS).\n\n    Args:\n        dicom_image_list (list): [description]\n    '
    sorted_dict = {}
    for dicom_file in dicom_image_list:
        dcm = pydicom.read_file(dicom_file, force=True)
        image_position = np.array(dcm.ImagePositionPatient, dtype=float)
        image_orientation = np.array(dcm.ImageOrientationPatient, dtype=float)
        image_plane_normal = np.cross(image_orientation[:3], image_orientation[3:])
        slice_location = (image_position * image_plane_normal)[2]
        sorted_dict[dicom_file] = slice_location
    sorter_safe = (lambda dcm_file: sorted_dict[dcm_file])
    return sorted(dicom_image_list, key=sorter_safe)
Sorts a list of DICOM image files based on a DICOM tag value.
This is a much safer method than reading SliceLocation.
It takes mandatory DICOM fields (Image Position [Patient]) and (Image Orientation [Patient]).
The list of DICOM files is sorted by projecting the image position onto the axis normal to the
place defined by the image orientation.

This accounts for differences in patient position (e.g. HFS/FFS).

Args:
    dicom_image_list (list): [description]
def safe_sort_dicom_image_list(dicom_image_list):
    '\n    Sorts a list of DICOM image files based on a DICOM tag value.\n    This is a much safer method than reading SliceLocation.\n    It takes mandatory DICOM fields (Image Position [Patient]) and (Image Orientation [Patient]).\n    The list of DICOM files is sorted by projecting the image position onto the axis normal to the\n    place defined by the image orientation.\n\n    This accounts for differences in patient position (e.g. HFS/FFS).\n\n    Args:\n        dicom_image_list (list): [description]\n    '
    sorted_dict = {}
    for dicom_file in dicom_image_list:
        dcm = pydicom.read_file(dicom_file, force=True)
        image_position = np.array(dcm.ImagePositionPatient, dtype=float)
        image_orientation = np.array(dcm.ImageOrientationPatient, dtype=float)
        image_plane_normal = np.cross(image_orientation[:3], image_orientation[3:])
        slice_location = (image_position * image_plane_normal)[2]
        sorted_dict[dicom_file] = slice_location
    sorter_safe = (lambda dcm_file: sorted_dict[dcm_file])
    return sorted(dicom_image_list, key=sorter_safe)