Passed
Push — master ( 16a37b...944f53 )
by Simon
01:23
created

hyperactive.distribution.dist_ray()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 3
dl 0
loc 14
rs 9.95
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import warnings
6
7
def try_ray_import():
8
    try:
9
        import ray
10
11
        if ray.is_initialized():
12
            rayInit = True
13
        else:
14
            rayInit = False
15
    except ImportError:
16
        warnings.warn("failed to import ray", ImportWarning)
17
        ray = None
18
        rayInit = False
19
20
    return ray, rayInit
21
22
23
def dist(optimizer_class, _main_args_, _opt_args_):
24
    ray, rayInit = try_ray_import()
25
26
    if rayInit:
27
        dist_ray(optimizer_class, _main_args_, _opt_args_)
28
    else:
29
        dist_default(optimizer_class, _main_args_, _opt_args_)
30
31
32
def dist_default(optimizer_class, _main_args_, _opt_args_):
33
    _optimizer_ = optimizer_class(_main_args_, _opt_args_)
34
    params_results, pos_list, score_list = (
35
        _optimizer_.search()
36
    )
37
38
    # print("params_results", params_results)
39
40
def dist_ray(optimizer_class, _main_args_, _opt_args_):
41
    optimizer_class = ray.remote(optimizer_class)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ray does not seem to be defined.
Loading history...
42
    opts = [
43
        optimizer_class.remote(_main_args_, _opt_args_)
44
        for job in range(_main_args_.n_jobs)
45
    ]
46
    searches = [
47
        opt.search.remote(job, rayInit=rayInit) for job, opt in enumerate(opts)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable rayInit does not seem to be defined.
Loading history...
48
    ]
49
    params_results, pos_list, score_list = ray.get(searches)[0]
50
51
    # print("params_results", params_results)
52
53
    ray.shutdown()