docs: def find_and_apply_best_mask(matrix, version, is_micro, proposed_mask=None):
      """\
      Applies all mask patterns against the provided QR Code matrix and returns
      the best matrix and best pattern.
  
      ISO/IEC 18004:2015(E) -- 7.8.2 Data mask patterns (page 50)
      ISO/IEC 18004:2015(E) -- 7.8.3 Evaluation of data masking results (page 53)
      ISO/IEC 18004:2015(E) -- 7.8.3.1 Evaluation of QR Code symbols (page 53/54)
      ISO/IEC 18004:2015(E) -- 7.8.3.2 Evaluation of Micro QR Code symbols (page 54/55)
  
      :param matrix: A matrix.
      :param int version: A version (Micro) QR Code version constant.
      :param bool is_micro: Indicates if the matrix represents a Micro QR Code
      :param proposed_mask: Optional int to indicate the preferred mask.
      :rtype: tuple
      :return: A tuple of the best matrix and best data mask pattern index.
      """
      matrix_size = len(matrix)
      # ISO/IEC 18004:2015 -- 7.8.3.1 Evaluation of QR Code symbols (page 53/54)
      # The data mask pattern which results in the lowest penalty score shall
      # be selected for the symbol.
      is_better = lt
      best_score = _MAX_PENALTY_SCORE
      eval_mask = evaluate_mask
      if is_micro:
          # ISO/IEC 18004:2015(E) - 7.8.3.2 Evaluation of Micro QR Code symbols (page 54/55)
          # The data mask pattern which results in the highest score shall be
          # selected for the symbol.
          is_better = gt
          best_score = -1
          eval_mask = evaluate_micro_mask
      # Matrix to check if a module belongs to the encoding region
      # or function patterns
      function_matrix = make_matrix(version)
      add_finder_patterns(function_matrix, is_micro)
      add_alignment_patterns(function_matrix, version)
      if not is_micro:
          function_matrix[-8][8] = 0x1
  
      def is_encoding_region(i, j):
          return function_matrix[i][j] == 0x2
  
      mask_patterns = get_data_mask_functions(is_micro)
  
      # If the user supplied a mask pattern, the evaluation step is skipped
      if proposed_mask is not None:
          apply_mask(matrix, mask_patterns[proposed_mask], matrix_size,
                     is_encoding_region)
          return proposed_mask
  
      for mask_number, mask_pattern in enumerate(mask_patterns):
          apply_mask(matrix, mask_pattern, matrix_size, is_encoding_region)
          # NOTE: DO NOT add format / version info in advance of evaluation
          # See ISO/IEC 18004:2015(E) -- 7.8. Data masking (page 50)
          score = eval_mask(matrix, matrix_size)
          if is_better(score, best_score):
              best_score = score
              best_pattern = mask_number
          # Undo mask
          apply_mask(matrix, mask_pattern, matrix_size, is_encoding_region)
      apply_mask(matrix, mask_patterns[best_pattern], matrix_size, is_encoding_region)
      return best_pattern