Skip to content

Commit

Permalink
Update Tune (#67)
Browse files Browse the repository at this point in the history
* Update Tune

* update

* update

* update

* update

* update

* update

* update

* fix tests
  • Loading branch information
goodwanghan authored Aug 21, 2024
1 parent e457e01 commit 8b8b344
Show file tree
Hide file tree
Showing 23 changed files with 187 additions and 75 deletions.
59 changes: 38 additions & 21 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
{
"name": "Fugue Development Environment",
"image": "fugueproject/devenv:latest",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"python.pythonPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
"image": "mcr.microsoft.com/vscode/devcontainers/python:3.10",
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"python.pythonPath": "/usr/local/bin/python",
"python.defaultInterpreterPath": "/usr/local/bin/python",
"editor.defaultFormatter": "ms-python.black-formatter",
"isort.interpreter": [
"/usr/local/bin/python"
],
"flake8.interpreter": [
"/usr/local/bin/python"
],
"pylint.interpreter": [
"/usr/local/bin/python"
],
"black-formatter.interpreter": [
"/usr/local/bin/python"
]
},
"extensions": [
"ms-python.python",
"ms-python.isort",
"ms-python.flake8",
"ms-python.pylint",
"ms-python.mypy",
"ms-python.black-formatter",
"GitHub.copilot",
"njpwerner.autodocstring"
]
}
},
"extensions": [
"ms-python.python"
],
"forwardPorts": [
8888
],
"postCreateCommand": "make devenv",
"mounts": [
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
]
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2.11.0": {},
"ghcr.io/devcontainers/features/java:1": {
"version": "11"
}
}
}
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- name: Build and publish
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
make package
twine upload dist/*
10 changes: 6 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand All @@ -27,7 +27,9 @@ jobs:
- name: Test
run: make test
- name: "Upload coverage to Codecov"
if: matrix.python-version == 3.7
uses: codecov/codecov-action@v1
if: matrix.python-version == '3.10'
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/testwin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
python-version: [ 3.7, 3.8, 3.9, "3.10"]
python-version: [ 3.8, 3.9, "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v2
Expand Down
6 changes: 5 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ repos:
- flake8-tidy-imports
- pycodestyle
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
rev: v0.971
hooks:
- id: mypy
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
hooks:
- id: pylint
- repo: https://github.com/ambv/black
rev: 22.3.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[MESSAGES CONTROL]
disable = C0103,C0114,C0115,C0116,C0122,C0200,C0201,C0302,C0411,C0415,E0401,E0712,E1130,R0201,R0205,R0801,R0902,R0903,R0904,R0911,R0912,R0913,R0914,R0915,R1705,R1710,R1718,R1720,R1724,W0102,W0107,W0108,W0201,W0212,W0221,W0223,W0237,W0511,W0613,W0631,W0640,W0703,W0707,W1116
disable = C0103,C0114,C0115,C0116,C0122,C0200,C0201,C0302,C0411,C0415,E0401,E0712,E1130,E1136,R0201,R0205,R0801,R0902,R0903,R0904,R0911,R0912,R0913,R0914,R0915,R1705,R1710,R1718,R1720,R1724,W0102,W0107,W0108,W0201,W0212,W0221,W0223,W0237,W0511,W0613,W0631,W0640,W0703,W0707,W1116
# TODO: R0205: inherits from object, can be safely removed
4 changes: 4 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.1.6

- Support Python 3.12

## 0.1.5

- Refactor `FunctionWrapper`, remove the Fugue contraint
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ sphinx-autodoc-typehints
nbsphinx
flask

pyspark
dask[dataframe,distributed]
fugue[spark,dask]

# publish to pypi
wheel
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spark_options =
spark.sql.adaptive.enabled: false

[flake8]
ignore = E24,E203,W503,C408,A003,W504,C407,C405
ignore = E24,E203,W503,C408,A003,A005,W504,C407,C405
max-line-length = 88
format = pylint
exclude = .svc,CVS,.bzr,.hg,.git,__pycache__,venv,tests/*,docs/*
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_version() -> str:
author_email="[email protected]",
keywords="hyper parameter hyperparameter tuning tune tuner optimzation",
url="http://github.com/fugue-project/tune",
install_requires=["fugue", "cloudpickle", "triad>=0.8.4"],
install_requires=["fugue", "cloudpickle", "triad>=0.8.4", "fs"],
extras_require={
"hyperopt": ["hyperopt"],
"optuna": ["optuna"],
Expand All @@ -54,14 +54,14 @@ def get_version() -> str:
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3 :: Only",
],
python_requires=">=3.6",
python_requires=">=3.8",
entry_points={
"tune.plugins": [
"mlflow = tune_mlflow[mlflow]",
Expand Down
8 changes: 4 additions & 4 deletions tests/tune/concepts/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_trial_report():
report = copy.copy(
TrialReport(
trial,
metric=np.float(0.1),
metric=np.float64(0.1),
params={"c": Rand(1, 2)},
metadata={"d": 4},
cost=2.0,
Expand All @@ -73,7 +73,7 @@ def test_trial_report():
assert 0.1 == report.sort_metric

report = copy.deepcopy(
TrialReport(trial, metric=np.float(0.111), cost=2.0, rung=4, sort_metric=1.23)
TrialReport(trial, metric=np.float64(0.111), cost=2.0, rung=4, sort_metric=1.23)
)
assert trial is report.trial
report = cloudpickle.loads(cloudpickle.dumps(report))
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_trial_report():
assert 5 == report.with_rung(5).rung

td = trial.with_dfs({"a": pd.DataFrame})
report = TrialReport(td, metric=np.float(0.1))
report = TrialReport(td, metric=np.float64(0.1))
assert 0 == len(report.trial.dfs)


Expand Down Expand Up @@ -177,7 +177,7 @@ def test_trial_report_heap():
def test_trial_decision():
trial = Trial("abc", {"a": 1}, {"b": Rand(0, 2)})
report = TrialReport(
trial, metric=np.float(0.1), params={"c": Rand(0, 3)}, metadata={"d": 4}
trial, metric=np.float64(0.1), params={"c": Rand(0, 3)}, metadata={"d": 4}
)
decision = TrialDecision(
report, budget=0.0, should_checkpoint=True, metadata={"x": 1}, reason="p"
Expand Down
18 changes: 18 additions & 0 deletions tests/tune/concepts/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from tune.concepts.logger import (
MetricLogger,
get_current_metric_logger,
set_current_metric_logger,
)


def test_logger_context():
m1 = MetricLogger()
m2 = MetricLogger()

with set_current_metric_logger(m1) as mm1:
assert get_current_metric_logger() is m1
assert mm1 is m1
with set_current_metric_logger(m2) as mm2:
assert get_current_metric_logger() is m2
assert mm2 is m2
assert get_current_metric_logger() is m1
21 changes: 16 additions & 5 deletions tests/tune/noniterative/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import pandas as pd
from fugue import FugueWorkflow
from pytest import raises

from tune import optimize_noniterative, suggest_for_noniterative_objective
from tune.concepts.dataset import TuneDatasetBuilder
from tune.concepts.flow import Monitor
from tune.concepts.logger import MetricLogger, get_current_metric_logger
from tune.concepts.space import Grid, Space
from tune.constants import TUNE_REPORT, TUNE_REPORT_METRIC
from tune.exceptions import TuneInterrupted
Expand All @@ -14,23 +16,29 @@


def objective(a: float, b: pd.DataFrame) -> float:
return a ** 2 + b.shape[0]
return a**2 + b.shape[0]


def objective_with_logger(a: float, b: pd.DataFrame) -> float:
m = get_current_metric_logger()
assert m.mock
return a**2 + b.shape[0]


def objective2(a: float, b: pd.DataFrame) -> float:
return -(a ** 2 + b.shape[0])
return -(a**2 + b.shape[0])


def objective3(a: float, b: pd.DataFrame) -> float:
if a == -2:
raise TuneInterrupted()
return a ** 2 + b.shape[0]
return a**2 + b.shape[0]


def objective4(a: float, b: pd.DataFrame) -> float:
if a == -2:
raise ValueError("expected")
return a ** 2 + b.shape[0]
return a**2 + b.shape[0]


def assert_metric(df: pd.DataFrame, metrics: List[float]) -> None:
Expand All @@ -56,12 +64,15 @@ def test_study(tmpdir):
# no data partition
builder = TuneDatasetBuilder(space, str(tmpdir)).add_df("b", dag.df(input_df))
dataset = builder.build(dag, 1)
logger = MetricLogger()
logger.mock = True
for distributed in [True, False, None]:
# min_better = True
result = optimize_noniterative(
objective=objective,
objective=objective_with_logger,
dataset=dataset,
distributed=distributed,
logger=logger,
)
result.result()[[TUNE_REPORT, TUNE_REPORT_METRIC]].output(
assert_metric, params=dict(metrics=[3.0, 4.0, 7.0])
Expand Down
13 changes: 10 additions & 3 deletions tests/tune_sklearn/test_suggest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from fugue_dask import DaskExecutionEngine
import fugue.test as ft
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Lasso, LinearRegression

from tune import TUNE_OBJECT_FACTORY, Grid, Rand
from tune_hyperopt.optimizer import HyperoptLocalOptimizer
from tune_sklearn import sk_space, suggest_sk_models, suggest_sk_models_by_cv

# from fugue_spark import SparkExecutionEngine


@ft.with_backend("dask")
def test_suggest(tmpdir):
TUNE_OBJECT_FACTORY.set_temp_path(str(tmpdir))

Expand All @@ -31,6 +33,7 @@ def test_suggest(tmpdir):
top_n=0,
distributed=False,
local_optimizer=HyperoptLocalOptimizer(max_iter=10, seed=0),
execution_engine="native",
)
assert 4 == len(result)
assert 50 > result[0].sort_metric
Expand All @@ -44,6 +47,7 @@ def test_suggest(tmpdir):
partition_keys=["sex"],
temp_path=str(tmpdir),
save_model=True,
execution_engine="native",
)
assert 16 == len(result)
assert 50 > result[0].sort_metric
Expand All @@ -55,12 +59,13 @@ def test_suggest(tmpdir):
"neg_mean_absolute_error",
top_n=1,
partition_keys=["sex"],
execution_engine=DaskExecutionEngine,
execution_engine="dask",
)
assert 2 == len(result)
assert 50 > result[0].sort_metric


@ft.with_backend("dask")
def test_suggest_cv(tmpdir):
TUNE_OBJECT_FACTORY.set_temp_path(str(tmpdir))

Expand All @@ -80,6 +85,7 @@ def test_suggest_cv(tmpdir):
top_n=0,
distributed=False,
local_optimizer=HyperoptLocalOptimizer(max_iter=10, seed=0),
execution_engine="native",
)
assert 4 == len(result)
assert 50 > result[0].sort_metric
Expand All @@ -92,6 +98,7 @@ def test_suggest_cv(tmpdir):
partition_keys=["sex"],
temp_path=str(tmpdir),
save_model=True,
execution_engine="native",
)
assert 16 == len(result)
assert 50 > result[0].sort_metric
Expand All @@ -102,7 +109,7 @@ def test_suggest_cv(tmpdir):
"neg_mean_absolute_error",
top_n=1,
partition_keys=["sex"],
execution_engine=DaskExecutionEngine,
execution_engine="dask",
)
assert 2 == len(result)
assert 50 > result[0].sort_metric
Expand Down
Loading

0 comments on commit 8b8b344

Please sign in to comment.