run.py 2.49 KB
Newer Older
1
2
3
4
import argparse
import sys
import os
import importlib
5
import pickle
6
7
8
9
10
11
import modes.experiments as exp

def mkdir_if_not_exists(path):
    if not os.path.exists(path):
        os.mkdir(path)

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Run(object):
    def __init__(self, experiment, env, outpath):
        self.experiment = experiment
        self.env = env
        self.outpath = outpath
        self.output = None

class Runtime(object):
    def add_run(self, run):
        pass

    def start(self):
        pass

class LocalSimpleRuntime(Runtime):
    def __init__(self):
        self.runnable = []
        self.complete = []

    def add_run(self, run):
        self.runnable.append(run)

    def start(self):
        for run in self.runnable:
            run.output = exp.run_exp_local(run.experiment, run.env)
            self.complete.append(run)

            with open(run.outpath, 'w') as f:
                f.write(run.output.dumps())

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
parser = argparse.ArgumentParser()
parser.add_argument('experiments', metavar='EXP', type=str, nargs='+',
        help='An experiment file to run')
parser.add_argument('--runs', metavar='N', type=int, default=1,
        help='Number of runs')
parser.add_argument('--repo', metavar='DIR', type=str,
        default='..', help='Repo directory')
parser.add_argument('--workdir', metavar='DIR', type=str,
        default='./out/', help='Work directory base')
parser.add_argument('--outdir', metavar='DIR',  type=str,
        default='./out/', help='Output directory base')

args = parser.parse_args()

experiments = []
for path in args.experiments:
58
59
60
61
62
63
64
65
66
67
    modname, modext = os.path.splitext(os.path.basename(path))

    if modext == '.py':
        spec = importlib.util.spec_from_file_location(modname, path)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)
        experiments += mod.experiments
    else:
        with open(path, 'rb') as f:
            experiments.append(pickle.load(f))
68
69
70
71

mkdir_if_not_exists(args.workdir)
mkdir_if_not_exists(args.outdir)

72
73
runtime = LocalSimpleRuntime()

74
75
76
77
78
79
80
81
82
83
84
85
86
87
for e in experiments:
    workdir_base = '%s/%s' % (args.workdir, e.name)
    mkdir_if_not_exists(workdir_base)

    for run in range(0, args.runs):
        outpath = '%s/%s-%d.json' % (args.outdir, e.name, run)
        if os.path.exists(outpath):
            print('skip %s run %d' % (e.name, run))
            continue

        workdir = '%s/%d' % (workdir_base, run)
        mkdir_if_not_exists(workdir)

        env = exp.ExpEnv(args.repo, workdir)
88
        runtime.add_run(Run(e, env, outpath))
89

90
runtime.start()