Skip to content

Commit

Permalink
Updates required by recent changes in ray (#462)
Browse files Browse the repository at this point in the history
* Resolve ray.tune deprecation warnings

* Resolve ray-project/ray#38202

* Change ray version requirements

* Fix tests
  • Loading branch information
bwohlberg authored Nov 2, 2023
1 parent aadb4ad commit d0e6c4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/examples_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
astra-toolbox
colour_demosaicing
xdesign>=0.5.5
ray[tune]>=2.0.0
ray[tune,train]>=2.5.0
hyperopt
bm3d>=4.0.0
bm4d>=4.2.2
22 changes: 12 additions & 10 deletions scico/ray/tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

try:
import ray.tune

os.environ["RAY_AIR_NEW_OUTPUT"] = "0"
except ImportError:
raise ImportError("Could not import ray.tune; please install it.")
import ray.air
Expand Down Expand Up @@ -75,7 +77,7 @@ def run(
config: Optional[Dict[str, Any]] = None,
hyperopt: bool = True,
verbose: bool = True,
local_dir: Optional[str] = None,
storage_path: Optional[str] = None,
) -> ray.tune.ExperimentAnalysis:
"""Simplified wrapper for `ray.tune.run`_.
Expand Down Expand Up @@ -109,7 +111,7 @@ def run(
running, and terminated trials are indicated by "P:", "R:",
and "T:" respectively, followed by the current best metric
value and the parameters at which it was reported.
local_dir: Directory in which to save tuning results. Defaults to
storage_path: Directory in which to save tuning results. Defaults to
a subdirectory "<username>/ray_results" within the path returned by
`tempfile.gettempdir()`, corresponding e.g. to
"/tmp/<username>/ray_results" under Linux.
Expand All @@ -136,12 +138,12 @@ def run(
name = run_or_experiment.__name__
name += "_" + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

if local_dir is None:
if storage_path is None:
try:
user = getpass.getuser()
except Exception: # pragma: no cover
user = "NOUSER"
local_dir = os.path.join(tempfile.gettempdir(), user, "ray_results")
storage_path = os.path.join(tempfile.gettempdir(), user, "ray_results")

# Record original logger.info
logger_info = ray.tune.tune.logger.info
Expand All @@ -160,7 +162,7 @@ def logger_info_filter(msg, *args, **kwargs):
name=name,
time_budget_s=time_budget_s,
num_samples=num_samples,
local_dir=local_dir,
storage_path=storage_path,
resources_per_trial=resources_per_trial,
max_concurrent_trials=max_concurrent_trials,
reuse_actors=True,
Expand Down Expand Up @@ -193,7 +195,7 @@ def __init__(
reuse_actors: bool = True,
hyperopt: bool = True,
verbose: bool = True,
local_dir: Optional[str] = None,
storage_path: Optional[str] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -226,7 +228,7 @@ def __init__(
running, and terminated trials are indicated by "P:", "R:",
and "T:" respectively, followed by the current best metric
value and the parameters at which it was reported.
local_dir: Directory in which to save tuning results. Defaults
storage_path: Directory in which to save tuning results. Defaults
to a subdirectory "<username>/ray_results" within the path
returned by `tempfile.gettempdir()`, corresponding e.g. to
"/tmp/<username>/ray_results" under Linux.
Expand Down Expand Up @@ -263,15 +265,15 @@ def __init__(
setattr(tune_config, k, v)

name = trainable.__name__ + "_" + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
if local_dir is None:
if storage_path is None:
try:
user = getpass.getuser()
except Exception: # pragma: no cover
user = "NOUSER"
local_dir = os.path.join(tempfile.gettempdir(), user, "ray_results")
storage_path = os.path.join(tempfile.gettempdir(), user, "ray_results")

run_config = kwargs.pop("run_config", None)
run_config_kwargs = {"name": name, "local_dir": local_dir, "verbose": 0}
run_config_kwargs = {"name": name, "storage_path": storage_path, "verbose": 0}
if verbose:
run_config_kwargs.update({"verbose": 1, "progress_reporter": _CustomReporter()})
if num_iterations is not None or time_budget is not None:
Expand Down
16 changes: 8 additions & 8 deletions scico/test/test_ray_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

try:
import ray
from scico.ray import train, tune
from scico.ray import report, tune

ray.init(num_cpus=1)
except ImportError as e:
Expand All @@ -18,7 +18,7 @@ def test_random_run():
def eval_params(config):
x, y = config["x"], config["y"]
cost = x**2 + (y - 0.5) ** 2
train.report({"cost": cost})
report({"cost": cost})

config = {"x": tune.uniform(-1, 1), "y": tune.uniform(-1, 1)}
resources = {"gpu": 0, "cpu": 1}
Expand All @@ -32,7 +32,7 @@ def eval_params(config):
resources_per_trial=resources,
hyperopt=False,
verbose=False,
local_dir=os.path.join(tempfile.gettempdir(), "ray_test"),
storage_path=os.path.join(tempfile.gettempdir(), "ray_test"),
)
best_config = analysis.get_best_config(metric="cost", mode="min")
assert np.abs(best_config["x"]) < 0.25
Expand All @@ -43,7 +43,7 @@ def test_random_tune():
def eval_params(config):
x, y = config["x"], config["y"]
cost = x**2 + (y - 0.5) ** 2
train.report({"cost": cost})
report({"cost": cost})

config = {"x": tune.uniform(-1, 1), "y": tune.uniform(-1, 1)}
resources = {"gpu": 0, "cpu": 1}
Expand All @@ -56,7 +56,7 @@ def eval_params(config):
num_samples=100,
hyperopt=False,
verbose=False,
local_dir=os.path.join(tempfile.gettempdir(), "ray_test"),
storage_path=os.path.join(tempfile.gettempdir(), "ray_test"),
)
results = tuner.fit()
best_config = results.get_best_result().config
Expand All @@ -68,7 +68,7 @@ def test_hyperopt_run():
def eval_params(config):
x, y = config["x"], config["y"]
cost = x**2 + (y - 0.5) ** 2
train.report({"cost": cost})
report({"cost": cost})

config = {"x": tune.uniform(-1, 1), "y": tune.uniform(-1, 1)}
resources = {"gpu": 0, "cpu": 1}
Expand All @@ -91,7 +91,7 @@ def test_hyperopt_tune():
def eval_params(config):
x, y = config["x"], config["y"]
cost = x**2 + (y - 0.5) ** 2
train.report({"cost": cost})
report({"cost": cost})

config = {"x": tune.uniform(-1, 1), "y": tune.uniform(-1, 1)}
resources = {"gpu": 0, "cpu": 1}
Expand All @@ -115,7 +115,7 @@ def test_hyperopt_tune_alt_init():
def eval_params(config):
x, y = config["x"], config["y"]
cost = x**2 + (y - 0.5) ** 2
train.report({"cost": cost})
report({"cost": cost})

config = {"x": tune.uniform(-1, 1), "y": tune.uniform(-1, 1)}
tuner = tune.Tuner(
Expand Down

0 comments on commit d0e6c4c

Please sign in to comment.