Skip to content

Commit d638dcf

Browse files
committed
make strip-snapshots stuff more reusable
1 parent 63e65bf commit d638dcf

1 file changed

Lines changed: 51 additions & 29 deletions

File tree

‎paths_cli/commands/strip_snapshots.py‎

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,37 @@ def strip_snapshots(input_file, output_file, cv, blocksize):
2121
By giving the --cv option (once for each CV), you can select to only
2222
save certain CVs. If you do not give that option, all CVs will be saved.
2323
"""
24+
input_storage = INPUT_FILE.get(input_file)
2425
return strip_snapshots_main(
25-
input_storage=INPUT_FILE.get(input_file),
26+
input_storage=input_storage,
2627
output_storage=OUTPUT_FILE.get(output_file),
27-
cvs=CVS.get(cv),
28+
cvs=CVS.get(input_storage, cv),
2829
blocksize=blocksize
2930
)
3031

3132

32-
def block_precompute_cvs(cvs, proxy_snaps, blocksize):
33-
n_snaps = len(proxy_snaps)
34-
n_blocks = (n_snaps // blocksize) +1
33+
def make_blocks(listlike, blocksize):
34+
n_objs = len(listlike)
35+
n_blocks = (n_objs // blocksize) + 1
36+
minval = lambda i: i * blocksize
37+
maxval = lambda i: min((i + 1) * blocksize, n_objs)
38+
blocks = [listlike[minval(i):maxval(i)] for i in range(n_blocks)]
39+
return blocks
3540

36-
blocks = [proxy_snaps[i*blocksize: min((i+1)*blocksize, n_snaps)]
37-
for i in range(n_blocks)]
3841

39-
for block_num in tqdm(range(n_blocks), leave=False):
40-
block = blocks[block_num]
41-
for cv in cvs:
42-
cv.enable_diskcache()
43-
_ = cv(block)
42+
def precompute_cvs(cvs, block):
43+
for cv in cvs:
44+
cv.enable_diskcache()
45+
_ = cv(block)
46+
47+
48+
def rewrite_file(stage_names, stage_mapping):
49+
stages = tqdm(stage_names)
50+
for stage in stages:
51+
stages.set_description("%s" % stage)
52+
store_func, inputs = stage_mapping[stage]
53+
for obj in tqdm(inputs):
54+
store_func(obj)
4455

4556

4657
def strip_snapshots_main(input_storage, output_storage, cvs, blocksize):
@@ -50,24 +61,35 @@ def strip_snapshots_main(input_storage, output_storage, cvs, blocksize):
5061
# save template
5162
output_storage.save(input_storage.snapshots[0])
5263

64+
precompute_func = lambda inps: precompute_cvs(cvs, inps)
5365
snapshot_proxies = input_storage.snapshots.all().as_proxies()
54-
stages = tqdm(['precompute', 'cvs', 'snapshots', 'trajectories',
55-
'steps'])
56-
for stage in stages:
57-
stages.set_description("%s" % stage)
58-
if stage == 'precompute':
59-
block_precompute_cvs(cvs, snapshot_proxies, blocksize)
60-
else:
61-
store_func, inputs = {
62-
'cvs': (output_storage.cvs.save, input_storage.cvs),
63-
'snapshots': (output_storage.snapshots.mention,
64-
snapshot_proxies),
65-
'trajectories': (output_storage.trajectories.mention,
66-
input_storage.trajectories),
67-
'steps': (output_storage.steps.save, input_storage.steps)
68-
}[stage]
69-
for obj in tqdm(inputs):
70-
store_func(obj)
66+
snapshot_blocks = make_blocks(snapshot_proxies, blocksize)
67+
stages = ['precompute', 'cvs', 'snapshots', 'trajectories', 'steps']
68+
stage_mapping = {
69+
'precompute': (precompute_func, snapshot_blocks),
70+
'cvs': (output_storage.cvs.save, input_storage.cvs),
71+
'snapshots': (output_storage.snapshots.mention,
72+
snapshot_proxies),
73+
'trajectories': (output_storage.trajectories.mention,
74+
input_storage.trajectories),
75+
'steps': (output_storage.steps.save, input_storage.steps),
76+
}
77+
return rewrite_file(stages, stage_mapping)
78+
# for stage in stages:
79+
# stages.set_description("%s" % stage)
80+
# if stage == 'precompute':
81+
# block_precompute_cvs(cvs, snapshot_proxies, blocksize)
82+
# else:
83+
# store_func, inputs = {
84+
# 'cvs': (output_storage.cvs.save, input_storage.cvs),
85+
# 'snapshots': (output_storage.snapshots.mention,
86+
# snapshot_proxies),
87+
# 'trajectories': (output_storage.trajectories.mention,
88+
# input_storage.trajectories),
89+
# 'steps': (output_storage.steps.save, input_storage.steps)
90+
# }[stage]
91+
# for obj in tqdm(inputs):
92+
# store_func(obj)
7193

7294

7395
CLI = strip_snapshots

0 commit comments

Comments
 (0)