original_docstring: Convert a sparse matrix to a given format.
      Checks the sparse format of spmatrix and converts if necessary.
      Parameters
      ----------
      spmatrix : scipy sparse matrix
          Input to validate and convert.
      accept_sparse : string, boolean or list/tuple of strings
          String[s] representing allowed sparse matrix formats ('csc',
          'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). If the input is sparse but
          not in the allowed format, it will be converted to the first listed
          format. True allows the input to be any format. False means
          that a sparse matrix input will raise an error.
      dtype : string, type or None
          Data type of result. If None, the dtype of the input is preserved.
      copy : boolean
          Whether a forced copy will be triggered. If copy=False, a copy might
          be triggered by a conversion.
      force_all_finite : boolean or 'allow-nan', (default=True)
          Whether to raise an error on np.inf and np.nan in X. The possibilities
          are:
          - True: Force all values of X to be finite.
          - False: accept both np.inf and np.nan in X.
          - 'allow-nan': accept only np.nan values in X. Values cannot be
            infinite.
          .. versionadded:: 0.20
             ``force_all_finite`` accepts the string ``'allow-nan'``.
      Returns
      -------
      spmatrix_converted : scipy sparse matrix.
          Matrix that is ensured to have an allowed type.
      
docstring: Convert a sparse matrix to a given format.
  Checks the sparse format of spmatrix and converts if necessary.
  Parameters
  
  spmatrix : scipy sparse matrix
  Input to validate and convert.
  
  spmatrix_converted : scipy sparse matrix.
  Matrix that is ensured to have an allowed type.
docstring_tokens: Convert a sparse matrix to a given format . Checks the sparse format of spmatrix and converts if necessary . Parameters spmatrix : scipy sparse matrix Input to validate and convert . spmatrix_converted : scipy sparse matrix . Matrix that is ensured to have an allowed type .
code: def _ensure_sparse_format(spmatrix, accept_sparse, dtype, copy,
                            force_all_finite, accept_large_sparse):
      """Convert a sparse matrix to a given format.
      Checks the sparse format of spmatrix and converts if necessary.
      Parameters
      ----------
      spmatrix : scipy sparse matrix
          Input to validate and convert.
      accept_sparse : string, boolean or list/tuple of strings
          String[s] representing allowed sparse matrix formats ('csc',
          'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). If the input is sparse but
          not in the allowed format, it will be converted to the first listed
          format. True allows the input to be any format. False means
          that a sparse matrix input will raise an error.
      dtype : string, type or None
          Data type of result. If None, the dtype of the input is preserved.
      copy : boolean
          Whether a forced copy will be triggered. If copy=False, a copy might
          be triggered by a conversion.
      force_all_finite : boolean or 'allow-nan', (default=True)
          Whether to raise an error on np.inf and np.nan in X. The possibilities
          are:
          - True: Force all values of X to be finite.
          - False: accept both np.inf and np.nan in X.
          - 'allow-nan': accept only np.nan values in X. Values cannot be
            infinite.
          .. versionadded:: 0.20
             ``force_all_finite`` accepts the string ``'allow-nan'``.
      Returns
      -------
      spmatrix_converted : scipy sparse matrix.
          Matrix that is ensured to have an allowed type.
      """
      if dtype is None:
          dtype = spmatrix.dtype
  
      changed_format = False
  
      if isinstance(accept_sparse, str):
          accept_sparse = [accept_sparse]
  
      # Indices dtype validation
      _check_large_sparse(spmatrix, accept_large_sparse)
  
      if accept_sparse is False:
          raise TypeError('A sparse matrix was passed, but dense '
                          'data is required. Use X.toarray() to '
                          'convert to a dense numpy array.')
      elif isinstance(accept_sparse, (list, tuple)):
          if len(accept_sparse) == 0:
              raise ValueError("When providing 'accept_sparse' "
                               "as a tuple or list, it must contain at "
                               "least one string value.")
          # ensure correct sparse format
          if spmatrix.format not in accept_sparse:
              # create new with correct sparse
              spmatrix = spmatrix.asformat(accept_sparse[0])
              changed_format = True
      elif accept_sparse is not True:
          # any other type
          raise ValueError("Parameter 'accept_sparse' should be a string, "
                           "boolean or list of strings. You provided "
                           "'accept_sparse={}'.".format(accept_sparse))
  
      if dtype != spmatrix.dtype:
          # convert dtype
          spmatrix = spmatrix.astype(dtype)
      elif copy and not changed_format:
          # force copy
          spmatrix = spmatrix.copy()
  
      if force_all_finite:
          if not hasattr(spmatrix, "data"):
              warnings.warn("Can't check %s sparse matrix for nan or inf."
                            % spmatrix.format, stacklevel=2)
          else:
              _assert_all_finite(spmatrix.data,
                                 allow_nan=force_all_finite == 'allow-nan')
  
      return spmatrix
code_tokens: def _ensure_sparse_format ( spmatrix , accept_sparse , dtype , copy , force_all_finite , accept_large_sparse ) : if dtype is None : dtype = spmatrix . dtype changed_format = False if isinstance ( accept_sparse , str ) : accept_sparse = [ accept_sparse ] _check_large_sparse ( spmatrix , accept_large_sparse ) if accept_sparse is False : raise TypeError ( 'A sparse matrix was passed, but dense ' 'data is required. Use X.toarray() to ' 'convert to a dense numpy array.' ) elif isinstance ( accept_sparse , ( list , tuple ) ) : if len ( accept_sparse ) == 0 : raise ValueError ( "When providing 'accept_sparse' " "as a tuple or list, it must contain at " "least one string value." ) if spmatrix . format not in accept_sparse : spmatrix = spmatrix . asformat ( accept_sparse [ 0 ] ) changed_format = True elif accept_sparse is not True : raise ValueError ( "Parameter 'accept_sparse' should be a string, " "boolean or list of strings. You provided " "'accept_sparse={}'." . format ( accept_sparse ) ) if dtype != spmatrix . dtype : spmatrix = spmatrix . astype ( dtype ) elif copy and not changed_format : spmatrix = spmatrix . copy ( ) if force_all_finite : if not hasattr ( spmatrix , "data" ) : warnings . warn ( "Can't check %s sparse matrix for nan or inf." % spmatrix . format , stacklevel = 2 ) else : _assert_all_finite ( spmatrix . data , allow_nan = force_all_finite == 'allow-nan' ) return spmatrix
short_docstring: Convert a sparse matrix to a given format.
short_docstring_tokens: Convert a sparse matrix to a given format .
comment: """Convert a sparse matrix to a given format.
      Checks the sparse format of spmatrix and converts if necessary.
      Parameters
      ----------
      spmatrix : scipy sparse matrix
          Input to validate and convert.
      accept_sparse : string, boolean or list/tuple of strings
          String[s] representing allowed sparse matrix formats ('csc',
          'csr', 'coo', 'dok', 'bsr', 'lil', 'dia'). If the input is sparse but
          not in the allowed format, it will be converted to the first listed
          format. True allows the input to be any format. False means
          that a sparse matrix input will raise an error.
      dtype : string, type or None
          Data type of result. If None, the dtype of the input is preserved.
      copy : boolean
          Whether a forced copy will be triggered. If copy=False, a copy might
          be triggered by a conversion.
      force_all_finite : boolean or 'allow-nan', (default=True)
          Whether to raise an error on np.inf and np.nan in X. The possibilities
          are:
          - True: Force all values of X to be finite.
          - False: accept both np.inf and np.nan in X.
          - 'allow-nan': accept only np.nan values in X. Values cannot be
            infinite.
          .. versionadded:: 0.20
             ``force_all_finite`` accepts the string ``'allow-nan'``.
      Returns
      -------
      spmatrix_converted : scipy sparse matrix.
          Matrix that is ensured to have an allowed type.
      """$SEP$# Indices dtype validation$SEP$# ensure correct sparse format$SEP$# create new with correct sparse$SEP$# any other type$SEP$# convert dtype$SEP$# force copy