code: def _get_grouped_dicoms(dicom_input):
      
      # if all dicoms have an instance number try sorting by instance number else by position
      if [d for d in dicom_input if 'InstanceNumber' in d]:
          dicoms = sorted(dicom_input, key=lambda x: x.InstanceNumber)
      else:
          dicoms = common.sort_dicoms(dicom_input)
      # now group per stack
      grouped_dicoms = [[]]  # list with first element a list
      timepoint_index = 0
      previous_stack_position = -1
  
      # loop over all sorted dicoms
      stack_position_tag = Tag(0x2001, 0x100a)  # put this there as this is a slow step and used a lot
      for index in range(0, len(dicoms)):
          dicom_ = dicoms[index]
          stack_position = 0
          if stack_position_tag in dicom_:
              stack_position = common.get_is_value(dicom_[stack_position_tag])
          if previous_stack_position == stack_position:
              # if the stack number is the same we move to the next timepoint
              timepoint_index += 1
              if len(grouped_dicoms) <= timepoint_index:
                  grouped_dicoms.append([])
          else:
              # if it changes move back to the first timepoint
              timepoint_index = 0
          grouped_dicoms[timepoint_index].append(dicom_)
          previous_stack_position = stack_position
  
      return grouped_dicoms
docs: Search all dicoms in the dicom directory, sort and validate them
  
      fast_read = True will only read the headers not the data