Skip to content

Add sklearn integration#66

Merged
contramundum53 merged 3 commits intooptuna:mainfrom
y0z:feature/sklearn
Feb 6, 2024
Merged

Add sklearn integration#66
contramundum53 merged 3 commits intooptuna:mainfrom
y0z:feature/sklearn

Conversation

@y0z
Copy link
Copy Markdown
Member

@y0z y0z commented Feb 1, 2024

Motivation

Description of the changes

In this PR, I apply some changes required to migrate the sklearn integration as follows.

  • Introduce the lazy import of modules the same way as optuna.integration.
    • This allows us to lazily import optuna_integration.OptunaSearchCV and other modules.
  • Make API references correctly generated from optuna_integration, not optuna.integration (the current reference generation has an inappropriate cycle dependency, i.e., the documents for optuna_integraiton are generated from the implementations in optuna.integration).
    • I also replaced the references :func: and :class: for components in optuna.integration modules to hyperlinks because references to components of another repository do not work.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 1, 2024

Codecov Report

Attention: 30 lines in your changes are missing coverage. Please review.

Comparison is base (f6f7ade) 35.95% compared to head (29e74be) 54.12%.

Files Patch % Lines
optuna_integration/__init__.py 61.22% 19 Missing ⚠️
optuna_integration/sklearn.py 96.93% 11 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main      #66       +/-   ##
===========================================
+ Coverage   35.95%   54.12%   +18.17%     
===========================================
  Files          21       23        +2     
  Lines         865     1273      +408     
===========================================
+ Hits          311      689      +378     
- Misses        554      584       +30     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@y0z y0z changed the title Add sklearn integration [WIP] Add sklearn integration Feb 1, 2024
@y0z y0z marked this pull request as draft February 2, 2024 10:21
@y0z y0z force-pushed the feature/sklearn branch from ff5b999 to 29e74be Compare February 5, 2024 02:35
@y0z y0z changed the title [WIP] Add sklearn integration Add sklearn integration Feb 5, 2024
@y0z y0z marked this pull request as ready for review February 5, 2024 02:43
@contramundum53 contramundum53 self-assigned this Feb 6, 2024
Comment on lines +89 to +110

sys.modules[__name__] = _IntegrationModule(__name__)

__all__ = [
"AllenNLPExecutor",
"AllenNLPPruningCallback",
"CatalystPruningCallback",
"CatBoostPruningCallback",
"ChainerMNStudy",
"ChainerPruningExtension",
"FastAIPruningCallback",
"FastAIV1PruningCallback",
"FastAIV2PruningCallback",
"KerasPruningCallback",
"MXNetPruningCallback",
"OptunaSearchCV",
"ShapleyImportanceEvaluator",
"SkorchPruningCallback",
"TensorBoardCallback",
"TensorFlowPruningHook",
"TFKerasPruningCallback",
]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sys.modules[__name__] = _IntegrationModule(__name__)
__all__ = [
"AllenNLPExecutor",
"AllenNLPPruningCallback",
"CatalystPruningCallback",
"CatBoostPruningCallback",
"ChainerMNStudy",
"ChainerPruningExtension",
"FastAIPruningCallback",
"FastAIV1PruningCallback",
"FastAIV2PruningCallback",
"KerasPruningCallback",
"MXNetPruningCallback",
"OptunaSearchCV",
"ShapleyImportanceEvaluator",
"SkorchPruningCallback",
"TensorBoardCallback",
"TensorFlowPruningHook",
"TFKerasPruningCallback",
]
__all__ = [
"AllenNLPExecutor",
"AllenNLPPruningCallback",
"CatalystPruningCallback",
"CatBoostPruningCallback",
"ChainerMNStudy",
"ChainerPruningExtension",
"FastAIPruningCallback",
"FastAIV1PruningCallback",
"FastAIV2PruningCallback",
"KerasPruningCallback",
"MXNetPruningCallback",
"OptunaSearchCV",
"ShapleyImportanceEvaluator",
"SkorchPruningCallback",
"TensorBoardCallback",
"TensorFlowPruningHook",
"TFKerasPruningCallback",
]
sys.modules[__name__] = _IntegrationModule(__name__)

__all__ should be defined inside _IntegrationModule class, because the module itself will be overwritten.

Copy link
Copy Markdown
Member

@contramundum53 contramundum53 Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be discussed in a follow-up PR, since the same bug exists in optuna.integration. Let's merge this PR first.

Copy link
Copy Markdown
Member

@contramundum53 contramundum53 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, LGTM!
I checked that all existing modules and symbols inside are accessible directly from optuna_integration module, with the following test:

import pytest
import pkgutil
import importlib
import optuna_integration

exclude_modules = ["version"]
submodule_names = []

for importer, modname, ispkg in pkgutil.iter_modules([optuna_integration.__path__[0]]):
    if not modname.startswith("_") and not modname in exclude_modules:
        submodule_names.append(modname)

def test_integration_module():
    import optuna_integration
    import optuna_integration.version
    from optuna_integration import version

assert submodule_names != []

@pytest.mark.parametrize("submodule_name", submodule_names)
def test_import_modules(submodule_name: str) -> None:
    assert hasattr(optuna_integration, submodule_name)
    importlib.import_module(f"optuna_integration.{submodule_name}")

@pytest.mark.parametrize("symbol_name", optuna_integration.__all__)
def test_import_symbols(symbol_name: str) -> None:
    assert hasattr(optuna_integration, symbol_name)

However, the list in __init__.py must be updated each time a new integration is added. I think we could consider adding this test in CI so that nobody forgets to add it.

@contramundum53 contramundum53 merged commit 2734577 into optuna:main Feb 6, 2024
@y0z y0z deleted the feature/sklearn branch February 6, 2024 07:22
@nabenabe0928 nabenabe0928 added this to the v3.6.0 milestone Feb 8, 2024
@nabenabe0928 nabenabe0928 added the feature Change that does not break compatibility, but affects the public interfaces. label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Change that does not break compatibility, but affects the public interfaces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OptunaSearchCV using deprecated fit_params rather than params

3 participants