Skip to content

Commit 2077da0

Browse files
committed
cli: clean
1 parent a586013 commit 2077da0

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

examples/example.py

+5
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ def postgrid4(ctx, parameters_per_trial):
6969
for i, p in enumerate(parameters_per_trial):
7070
if i % 2 == 0:
7171
yield p
72+
73+
74+
@experiment()
75+
def experiment5(trial):
76+
raise NotImplementedError("experiment5 is not implemented.")

experitur/cli.py

+46-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
@click.group()
11-
def cli():
11+
def cli(): # pragma: no cover
1212
pass
1313

1414

@@ -17,7 +17,9 @@ def cli():
1717
@click.argument('dox_fn')
1818
@click.option('--skip-existing/--no-skip-existing', default=True)
1919
@click.option('--catch/--no-catch', default=True)
20-
def run(dox_fn, skip_existing, catch):
20+
@click.option('--clean-failed/--no-clean-failed', default=False)
21+
@click.option('--yes', '-y', is_flag=True)
22+
def run(dox_fn, skip_existing, catch, clean_failed, yes):
2123
click.echo('Running {}...'.format(dox_fn))
2224

2325
wdir = os.path.splitext(dox_fn)[0]
@@ -29,36 +31,62 @@ def run(dox_fn, skip_existing, catch):
2931
}
3032

3133
with push_context(Context(wdir, config)) as ctx:
34+
if clean_failed:
35+
selected = {trial_id: trial for trial_id,
36+
trial in ctx.store.items() if trial.is_failed}
37+
38+
click.echo(
39+
"The following {} trials will be deleted:".format(len(selected)))
40+
list_trials(selected)
41+
if yes or click.confirm('Continue?'):
42+
ctx.store.delete_all(selected.keys())
43+
3244
load_dox(dox_fn)
3345
ctx.run()
3446

3547

3648
@cli.command()
3749
@click.argument('dox_fn')
3850
@click.argument('experiment_id', default=None, required=False)
39-
@click.option('--failed', is_flag=True)
4051
@click.option('--all', is_flag=True)
41-
@click.option('--dry-run', '-n', is_flag=True)
42-
def clean(dox_fn, experiment_id=None, failed=True, all=False, empty=False, successful=False, dry_run=False):
43-
click.echo('Cleaning failed results from {}...'.format(dox_fn))
52+
@click.option('--yes', '-y', is_flag=True)
53+
def clean(dox_fn, experiment_id, all, yes):
54+
"""Clean failed experiments."""
55+
click.echo('Cleaning results from {}...'.format(dox_fn))
56+
57+
wdir = os.path.splitext(dox_fn)[0]
58+
with push_context(Context(wdir)) as ctx:
59+
selected = {trial_id: trial for trial_id,
60+
trial in ctx.store.items() if trial.is_failed or all}
61+
62+
click.echo(
63+
"The following {} trials will be deleted:".format(len(selected)))
64+
list_trials(selected)
65+
66+
if yes or click.confirm('Continue?'):
67+
ctx.store.delete_all(selected.keys())
4468

45-
if all:
46-
click.confirm('Do you really want to permanently delete all results of {}?'.format(
47-
dox_fn), abort=True)
4869

49-
failed = failed or all
50-
empty = empty or all
51-
successful = successful or all
70+
def list_trials(trials):
71+
"""Show a sorted list of trials with a status signifier.
5272
53-
if not any((failed, empty, successful)):
54-
print("Nothing to do. Did you mean --failed?")
55-
return
73+
! Failed
74+
"""
75+
for trial_id, trial in sorted(trials.items()):
76+
status = "!" if trial.is_failed else " "
77+
78+
print("{} {}".format(status, trial_id))
79+
80+
81+
@cli.command()
82+
@click.argument('dox_fn')
83+
@click.argument('experiment_id', default=None, required=False)
84+
def show(dox_fn, experiment_id=None):
85+
"""List the trials of this DOX."""
5686

5787
wdir = os.path.splitext(dox_fn)[0]
5888
with push_context(Context(wdir)) as ctx:
59-
load_dox(dox_fn)
60-
ctx.clean(failed=failed, dry_run=dry_run,
61-
experiment_id=experiment_id)
89+
list_trials(ctx.store)
6290

6391

6492
@cli.command()

experitur/trial.py

+8
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ def save(self):
209209
def id(self):
210210
return self.data["id"]
211211

212+
@property
213+
def is_failed(self):
214+
return not self.data.get("success", False)
215+
212216

213217
class TrialStore(collections.abc.MutableMapping):
214218
def __init__(self, ctx):
@@ -310,6 +314,10 @@ def create(self, parameters, experiment):
310314

311315
return trial
312316

317+
def delete_all(self, keys):
318+
for k in keys:
319+
del self[k]
320+
313321

314322
class FileTrialStore(TrialStore):
315323
PATTERN = os.path.join("{}", "trial.yaml")

0 commit comments

Comments
 (0)