def _remove_localizers_by_orientation(dicoms):
    """
    Removing localizers based on the orientation.
    This is needed as in some cases with ct data there are some localizer/projection type images that cannot
    be distiguished by the dicom headers. This is why we kick out all orientations that do not have more than 4 files
    4 is the limit anyway for converting to nifti on our case
    """
    orientations = []
    sorted_dicoms = {}
    # Loop overall files and build dict
    for dicom_header in dicoms:
        # Create affine matrix (http://nipy.sourceforge.net/nibabel/dicom/dicom_orientation.html#dicom-slice-affine)
        image_orient1 = numpy.array(dicom_header.ImageOrientationPatient)[0:3]
        image_orient2 = numpy.array(dicom_header.ImageOrientationPatient)[3:6]
        image_orient_combined = (image_orient1.tolist(), image_orient2.tolist())
        found_orientation = False
        for orientation in orientations:
            if numpy.allclose(image_orient_combined[0], numpy.array(orientation[0]), rtol=0.001, atol=0.001) \
                    and numpy.allclose(image_orient_combined[1], numpy.array(orientation[1]), rtol=0.001,
                                       atol=0.001):
                sorted_dicoms[str(orientation)].append(dicom_header)
                found_orientation = True
                break
        if not found_orientation:
            orientations.append(image_orient_combined)
            sorted_dicoms[str(image_orient_combined)] = [dicom_header]

    # if there are multiple possible orientations delete orientations where there are less than 4 files
    # we don't convert anything less that that anyway

    if len(sorted_dicoms) > 1:
        filtered_dicoms = []
        for orientation in sorted_dicoms.keys():
            if len(sorted_dicoms[orientation]) >= 4:
                filtered_dicoms.extend(sorted_dicoms[orientation])
        return filtered_dicoms
    else:
        return six.next(six.itervalues(sorted_dicoms))
def _remove_localizers_by_orientation ( dicoms ) : orientations = [ ] sorted_dicoms = { } for dicom_header in dicoms : image_orient1 = numpy . array ( dicom_header . ImageOrientationPatient ) [ 0 : 3 ] image_orient2 = numpy . array ( dicom_header . ImageOrientationPatient ) [ 3 : 6 ] image_orient_combined = ( image_orient1 . tolist ( ) , image_orient2 . tolist ( ) ) found_orientation = False for orientation in orientations : if numpy . allclose ( image_orient_combined [ 0 ] , numpy . array ( orientation [ 0 ] ) , rtol = 0.001 , atol = 0.001 ) and numpy . allclose ( image_orient_combined [ 1 ] , numpy . array ( orientation [ 1 ] ) , rtol = 0.001 , atol = 0.001 ) : sorted_dicoms [ str ( orientation ) ] . append ( dicom_header ) found_orientation = True break if not found_orientation : orientations . append ( image_orient_combined ) sorted_dicoms [ str ( image_orient_combined ) ] = [ dicom_header ] if len ( sorted_dicoms ) > 1 : filtered_dicoms = [ ] for orientation in sorted_dicoms . keys ( ) : if len ( sorted_dicoms [ orientation ] ) >= 4 : filtered_dicoms . extend ( sorted_dicoms [ orientation ] ) return filtered_dicoms else : return six . next ( six . itervalues ( sorted_dicoms ) )
Removing localizers based on the orientation.
    This is needed as in some cases with ct data there are some localizer/projection type images that cannot
    be distiguished by the dicom headers. This is why we kick out all orientations that do not have more than 4 files
    4 is the limit anyway for converting to nifti on our case