nodes: 0, module; 1, function_definition; 2, function_name:sort_dicoms; 3, parameters; 4, block; 5, identifier:dicoms; 6, expression_statement; 7, comment:# find most significant axis to use during sorting; 8, comment:# the original way of sorting (first x than y than z) does not work in certain border situations; 9, comment:# where for exampe the X will only slightly change causing the values to remain equal on multiple slices; 10, comment:# messing up the sorting completely); 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, expression_statement; 17, if_statement; 18, if_statement; 19, if_statement; 20, comment:"""
      Sort the dicoms based om the image possition patient
  
      :param dicoms: list of dicoms
      """; 21, assignment; 22, assignment; 23, assignment; 24, assignment; 25, assignment; 26, assignment; 27, boolean_operator; 28, block; 29, boolean_operator; 30, block; 31, boolean_operator; 32, block; 33, identifier:dicom_input_sorted_x; 34, call; 35, identifier:dicom_input_sorted_y; 36, call; 37, identifier:dicom_input_sorted_z; 38, call; 39, identifier:diff_x; 40, call; 41, identifier:diff_y; 42, call; 43, identifier:diff_z; 44, call; 45, comparison_operator:diff_x >= diff_y; 46, comparison_operator:diff_x >= diff_z; 47, return_statement; 48, comparison_operator:diff_y >= diff_x; 49, comparison_operator:diff_y >= diff_z; 50, return_statement; 51, comparison_operator:diff_z >= diff_x; 52, comparison_operator:diff_z >= diff_y; 53, return_statement; 54, identifier:sorted; 55, argument_list; 56, identifier:sorted; 57, argument_list; 58, identifier:sorted; 59, argument_list; 60, identifier:abs; 61, argument_list; 62, identifier:abs; 63, argument_list; 64, identifier:abs; 65, argument_list; 66, identifier:diff_x; 67, identifier:diff_y; 68, identifier:diff_x; 69, identifier:diff_z; 70, identifier:dicom_input_sorted_x; 71, identifier:diff_y; 72, identifier:diff_x; 73, identifier:diff_y; 74, identifier:diff_z; 75, identifier:dicom_input_sorted_y; 76, identifier:diff_z; 77, identifier:diff_x; 78, identifier:diff_z; 79, identifier:diff_y; 80, identifier:dicom_input_sorted_z; 81, identifier:dicoms; 82, keyword_argument; 83, identifier:dicoms; 84, keyword_argument; 85, identifier:dicoms; 86, keyword_argument; 87, binary_operator:dicom_input_sorted_x[-1].ImagePositionPatient[0] - dicom_input_sorted_x[0].ImagePositionPatient[0]; 88, binary_operator:dicom_input_sorted_y[-1].ImagePositionPatient[1] - dicom_input_sorted_y[0].ImagePositionPatient[1]; 89, binary_operator:dicom_input_sorted_z[-1].ImagePositionPatient[2] - dicom_input_sorted_z[0].ImagePositionPatient[2]; 90, identifier:key; 91, lambda; 92, identifier:key; 93, lambda; 94, identifier:key; 95, lambda; 96, subscript; 97, subscript; 98, subscript; 99, subscript; 100, subscript; 101, subscript; 102, lambda_parameters; 103, parenthesized_expression; 104, lambda_parameters; 105, parenthesized_expression; 106, lambda_parameters; 107, parenthesized_expression; 108, attribute; 109, integer:0; 110, attribute; 111, integer:0; 112, attribute; 113, integer:1; 114, attribute; 115, integer:1; 116, attribute; 117, integer:2; 118, attribute; 119, integer:2; 120, identifier:x; 121, subscript; 122, identifier:x; 123, subscript; 124, identifier:x; 125, subscript; 126, subscript; 127, identifier:ImagePositionPatient; 128, subscript; 129, identifier:ImagePositionPatient; 130, subscript; 131, identifier:ImagePositionPatient; 132, subscript; 133, identifier:ImagePositionPatient; 134, subscript; 135, identifier:ImagePositionPatient; 136, subscript; 137, identifier:ImagePositionPatient; 138, attribute; 139, integer:0; 140, attribute; 141, integer:1; 142, attribute; 143, integer:2; 144, identifier:dicom_input_sorted_x; 145, unary_operator; 146, identifier:dicom_input_sorted_x; 147, integer:0; 148, identifier:dicom_input_sorted_y; 149, unary_operator; 150, identifier:dicom_input_sorted_y; 151, integer:0; 152, identifier:dicom_input_sorted_z; 153, unary_operator; 154, identifier:dicom_input_sorted_z; 155, integer:0; 156, identifier:x; 157, identifier:ImagePositionPatient; 158, identifier:x; 159, identifier:ImagePositionPatient; 160, identifier:x; 161, identifier:ImagePositionPatient; 162, integer:1; 163, integer:1; 164, integer:1
code: def sort_dicoms(dicoms):
      """
      Sort the dicoms based om the image possition patient
  
      :param dicoms: list of dicoms
      """
      # find most significant axis to use during sorting
      # the original way of sorting (first x than y than z) does not work in certain border situations
      # where for exampe the X will only slightly change causing the values to remain equal on multiple slices
      # messing up the sorting completely)
      dicom_input_sorted_x = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[0]))
      dicom_input_sorted_y = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[1]))
      dicom_input_sorted_z = sorted(dicoms, key=lambda x: (x.ImagePositionPatient[2]))
      diff_x = abs(dicom_input_sorted_x[-1].ImagePositionPatient[0] - dicom_input_sorted_x[0].ImagePositionPatient[0])
      diff_y = abs(dicom_input_sorted_y[-1].ImagePositionPatient[1] - dicom_input_sorted_y[0].ImagePositionPatient[1])
      diff_z = abs(dicom_input_sorted_z[-1].ImagePositionPatient[2] - dicom_input_sorted_z[0].ImagePositionPatient[2])
      if diff_x >= diff_y and diff_x >= diff_z:
          return dicom_input_sorted_x
      if diff_y >= diff_x and diff_y >= diff_z:
          return dicom_input_sorted_y
      if diff_z >= diff_x and diff_z >= diff_y:
          return dicom_input_sorted_z