shimenghuang / KernelBiome

GNU General Public License v3.0
7 stars 1 forks source link

Verbose problem #1

Open ggmirandac opened 8 months ago

ggmirandac commented 8 months ago

Hi,

For the function KernelBiome I have tried many inputs for the verbose argument and I couldn't find the option to turn off the outputs of the function. Which argument should I put there to turn it off?

Best regards,

Gabriel

shimenghuang commented 8 months ago

Hi @ggmirandac , have you tried verbose=0? If it still prints, could you give an example of what it still prints?

For now, maybe you could try capturing the output like https://stackoverflow.com/questions/8391411/how-to-block-calls-to-print or https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call

Thanks! Shimeng

ggmirandac commented 7 months ago

Hi, Yes, Here is the output:

---------------------------------------------------------------------------
InvalidParameterError                     Traceback (most recent call last)
Cell In[145], line 11
      4 # Fit Aitchison KB
      5 KB = KernelBiome(kernel_estimator='KernelRidge',
      6                  center_kmat=True,
      7                  models=model,
      8                  estimator_pars={'n_hyper_grid': 100},
      9                  n_jobs=-1, 
     10                  verbose=0)
---> 11 KB.fit(X_train_relative, y_train_kb)

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/kernelbiome/kernelbiome.py:156, in KernelBiome.fit(self, X, y, w)
    153     lambdas = np.real(np.linalg.eigvals(kmat_fun(X, X)))
    154     print('* max/min eigenvalue of K: ' +
    155           str(np.max(lambdas)) + '/' + str(np.min(lambdas)))
--> 156 cv_results = outer_cv(
    157     X, y, self._estimator,
    158     kmat_fun=kmat_fun,
    159     center_kmat=self._center_kmat,
    160     cv_split=cv_split,
    161     scoring=self._cv_pars['scoring'],
    162     n_fold_outer=self._cv_pars['n_fold_outer'],
    163     n_fold_inner=self._cv_pars['n_fold_inner'],
    164     hyperpar_grid=self._hyperpar_grid,
    165     n_hyper_grid=self._n_hyper_grid,
    166     estimator_pars=self._estimator_pars_outer,
    167     n_jobs=self._n_jobs,
    168     verbose=self._verbose-1)
    169 train_scores[ii, :] = cv_results['train_scores']
    170 test_scores[ii, :] = cv_results['test_scores']

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/kernelbiome/helpers_fitting.py:308, in outer_cv(X, y, estimator, kmat_fun, center_kmat, cv_split, scoring, hyperpar_grid, n_fold_outer, n_fold_inner, estimator_pars, n_hyper_grid, n_jobs, verbose)
    306 y_train, y_test = y[train_index], y[test_index]
    307 # Fit model
--> 308 fitted_mod = fit_single_model(
    309     X_train, y_train, estimator,
    310     kmat_fun,
    311     scoring,
    312     hyperpar_grid=hyperpar_grid,
    313     center_kmat=center_kmat,
    314     n_fold=n_fold_inner,
    315     return_gscv=True,
    316     estimator_pars=estimator_pars,
    317     n_jobs=n_jobs, verbose=verbose)
    318 # training score
    319 train_scores[kk] = fitted_mod['train_score']

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/kernelbiome/helpers_fitting.py:253, in fit_single_model(X, y, estimator, kmat_fun, scoring, hyperpar_grid, center_kmat, return_gscv, n_hyper_grid, estimator_pars, n_fold, n_jobs, verbose)
    250     intercept = 0
    252 # Fit model
--> 253 gscv.fit(K, y-intercept)
    255 # Define prediction function
    256 if center_kmat:

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/sklearn/base.py:1145, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1140 partial_fit_and_fitted = (
   1141     fit_method.__name__ == "partial_fit" and _is_fitted(estimator)
   1142 )
   1144 if not global_skip_validation and not partial_fit_and_fitted:
-> 1145     estimator._validate_params()
   1147 with config_context(
   1148     skip_parameter_validation=(
   1149         prefer_skip_nested_validation or global_skip_validation
   1150     )
   1151 ):
   1152     return fit_method(estimator, *args, **kwargs)

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/sklearn/base.py:638, in BaseEstimator._validate_params(self)
    630 def _validate_params(self):
    631     """Validate types and values of constructor parameters
    632 
    633     The expected type and values must be defined in the `_parameter_constraints`
   (...)
    636     accepted constraints.
    637     """
--> 638     validate_parameter_constraints(
    639         self._parameter_constraints,
    640         self.get_params(deep=False),
    641         caller_name=self.__class__.__name__,
    642     )

File ~/micromamba/envs/VenturelliLab/lib/python3.10/site-packages/sklearn/utils/_param_validation.py:95, in validate_parameter_constraints(parameter_constraints, params, caller_name)
     89 else:
     90     constraints_str = (
     91         f"{', '.join([str(c) for c in constraints[:-1]])} or"
     92         f" {constraints[-1]}"
     93     )
---> 95 raise InvalidParameterError(
     96     f"The {param_name!r} parameter of {caller_name} must be"
     97     f" {constraints_str}. Got {param_val!r} instead."
     98 )

InvalidParameterError: The 'verbose' parameter of GridSearchCV must be an int in the range [0, inf), an instance of 'bool' or an instance of 'numpy.bool_'. Got -1 instead.

As you can see, I have verbose set to 0. I think the issue is in: kernelbiome.py at line 215. where you have

 verbose=self._verbose-1)

Maybe, I am wrong, but that could be the reason why the error is


InvalidParameterError: The 'verbose' parameter of GridSearchCV must be an int in the range [0, inf), an instance of 'bool' or an instance of 'numpy.bool_'. Got -1 instead.
shimenghuang commented 7 months ago

Hi @ggmirandac , thanks a lot for the details! I'll get into this in the coming few days and get back to you.

Best wishes, Shimeng

shimenghuang commented 7 months ago

Hi @ggmirandac , I've updated the verbose setting. The verbose argument in init is now removed and you can set verbose in fit instead, to be consistent with other methods in the class. Now setting verbose to 0 should not print anything anymore. (I updated both here and on pypi.) Please let me know if it's working properly for you. Thanks again :)

Best, Shimeng