docs: def convert_directory(dicom_directory, output_folder, compression=True, reorient=True):
      """
      This function will order all dicom files by series and order them one by one
  
      :param compression: enable or disable gzip compression
      :param reorient: reorient the dicoms according to LAS orientation
      :param output_folder: folder to write the nifti files to
      :param dicom_directory: directory with dicom files
      """
      # sort dicom files by series uid
      dicom_series = {}
      for root, _, files in os.walk(dicom_directory):
          for dicom_file in files:
              file_path = os.path.join(root, dicom_file)
              # noinspection PyBroadException
              try:
                  if compressed_dicom.is_dicom_file(file_path):
                      # read the dicom as fast as possible
                      # (max length for SeriesInstanceUID is 64 so defer_size 100 should be ok)
  
                      dicom_headers = compressed_dicom.read_file(file_path,
                                                                 defer_size="1 KB",
                                                                 stop_before_pixels=False,
                                                                 force=dicom2nifti.settings.pydicom_read_force)
                      if not _is_valid_imaging_dicom(dicom_headers):
                          logger.info("Skipping: %s" % file_path)
                          continue
                      logger.info("Organizing: %s" % file_path)
                      if dicom_headers.SeriesInstanceUID not in dicom_series:
                          dicom_series[dicom_headers.SeriesInstanceUID] = []
                      dicom_series[dicom_headers.SeriesInstanceUID].append(dicom_headers)
              except:  # Explicitly capturing all errors here to be able to continue processing all the rest
                  logger.warning("Unable to read: %s" % file_path)
                  traceback.print_exc()
  
      # start converting one by one
      for series_id, dicom_input in iteritems(dicom_series):
          base_filename = ""
          # noinspection PyBroadException
          try:
              # construct the filename for the nifti
              base_filename = ""
              if 'SeriesNumber' in dicom_input[0]:
                  base_filename = _remove_accents('%s' % dicom_input[0].SeriesNumber)
                  if 'SeriesDescription' in dicom_input[0]:
                      base_filename = _remove_accents('%s_%s' % (base_filename,
                                                                 dicom_input[0].SeriesDescription))
                  elif 'SequenceName' in dicom_input[0]:
                      base_filename = _remove_accents('%s_%s' % (base_filename,
                                                                 dicom_input[0].SequenceName))
                  elif 'ProtocolName' in dicom_input[0]:
                      base_filename = _remove_accents('%s_%s' % (base_filename,
                                                                 dicom_input[0].ProtocolName))
              else:
                  base_filename = _remove_accents(dicom_input[0].SeriesInstanceUID)
              logger.info('--------------------------------------------')
              logger.info('Start converting %s' % base_filename)
              if compression:
                  nifti_file = os.path.join(output_folder, base_filename + '.nii.gz')
              else:
                  nifti_file = os.path.join(output_folder, base_filename + '.nii')
              convert_dicom.dicom_array_to_nifti(dicom_input, nifti_file, reorient)
              gc.collect()
          except:  # Explicitly capturing app exceptions here to be able to continue processing
              logger.info("Unable to convert: %s" % base_filename)
              traceback.print_exc()