-
Notifications
You must be signed in to change notification settings - Fork 4
/
collect_files_from_different_experiments.py
133 lines (101 loc) · 5.24 KB
/
collect_files_from_different_experiments.py
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from collections import OrderedDict
from PyPDF2 import PdfFileMerger
from shutil import copyfile
from utils.misc_utils import *
from utils.experiment_utils import *
import json
pdf_filenames = [
"figs/subbridges/val_all_waymarks_all_bridges_energies.pdf",
"ais/0/subbridges_ais/mcmc_all_states_all_bridges_energies.pdf",
"ais/0/subbridges_nuts_post_ais/mcmc_all_states_all_bridges_energies.pdf",
"figs/val_bridge_logratios.pdf",
"figs/subbridges/val_all_n_squared_losses.pdf",
"figs/subbridges/val_norm_of_grad_of_wmark_logp.pdf",
"figs/subbridges/scatter_noise_vs_ratio_at_data_val.pdf",
"ais/0/ais_low_to_high_loglik_whole_chains/0_page_0.pdf",
"ais/0/nuts_post_ais_samples_low_to_high_loglik_whole_chains/0_page_0.pdf",
]
def copy_img_samples(ais_dir, summary_dir, rel_img_dir, config_idx, name):
src_dir = path_join(ais_dir, rel_img_dir)
dest_parent_dir = path_join(summary_dir, rel_img_dir)
for file in os.listdir(src_dir):
src_file = path_join(src_dir, file)
dest_dir = path_join(dest_parent_dir, file)
os.makedirs(dest_dir, exist_ok=True)
dest_file = path_join(dest_dir, "{}_config{}.pdf".format(name, config_idx))
copyfile(src_file, dest_file)
def collect_ais_files(summary_dir, path, config_idx):
for j, name in enumerate(["nuts"]):
ais_dir = path_join(path, "ais/{}/".format(j))
try:
copy_img_samples(ais_dir, summary_dir, "ais_low_to_high_loglik_final_states/", config_idx, name)
copy_img_samples(ais_dir, summary_dir, "ais_low_to_high_loglik_whole_chains/", config_idx, name)
except Exception as e:
print("failed to collect ais {} samples. Error: {}".format(j, e))
try:
copy_img_samples(ais_dir, summary_dir, "ais_low_to_high_loglik_final_states/", config_idx, name)
copy_img_samples(ais_dir, summary_dir, "ais_low_to_high_loglik_whole_chains/", config_idx, name)
except Exception as e:
print("failed to collect ais {} samples. Error: {}".format(j, e))
try:
copy_img_samples(ais_dir, summary_dir, "{}_post_ais_samples_low_to_high_loglik_final_states/".format(name), config_idx, name)
except Exception as e:
print("failed to collect post_ais {} samples. Error: {}".format(j, e))
def collect_agg_files(energies_file, losses_file, path, i):
with open(path_join(path, "config.json")) as f:
config = AttrDict(json.load(f))
agg_dir = path_join(path, "aggregated_results/")
with open(path_join(agg_dir, "val_energies.txt"), "r") as f:
energies = f.read().split()
e_str = str(i) + " " + "/".join(energies)
if "ais_kl" in config: e_str += "/" + str(config.ais_kl)
if "raise_kl" in config: e_str += "/" + str(config.raise_kl)
e_str += "\n"
energies_file.write(e_str)
with open(path_join(agg_dir, "val_losses.txt"), "r") as f:
losses = f.read().split()
losses_file.writelines(str(i) + " " + "/".join(losses) + "\n")
def merge_pdfs_for_single_experiment(exp_dir):
pdfs = ['file1.pdf', 'file2.pdf', 'file3.pdf', 'file4.pdf']
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write("result.pdf")
merger.close()
# noinspection PyUnresolvedReferences,PyTypeChecker
def main():
"""Collect files from different experiments"""
np.set_printoptions(precision=3)
parser = ArgumentParser(description='Collect files across a set of experiments', formatter_class=ArgumentDefaultsHelpFormatter)
# parser.add_argument('--config_path', type=str, default="pca_cropped_mnist/20200110-1525")
# parser.add_argument('--config_path', type=str, default="cropped_mnist/20200114-1441")
parser.add_argument('--config_path', type=str, default="1d_gauss/20200501-0739")
parser.add_argument('--id', action='append', type=int, help="model config ids", default=[0])
args = parser.parse_args()
summary_dir = path_join(project_root, "saved_models/", args.config_path + "_summary/")
os.makedirs(summary_dir, exist_ok=True)
energies_file = open(path_join(summary_dir, "energies.txt"), "w+")
losses_file = open(path_join(summary_dir, "losses.txt"), "w+")
for config_idx in args.id:
path = path_join(project_root, "saved_models/", args.config_path + "_{}".format(config_idx))
try:
collect_ais_files(summary_dir, path, config_idx)
except Exception as e:
print("failed to collect ais samples for config {}. Error: {}".format(config_idx, e))
try:
collect_agg_files(energies_file, losses_file, path, config_idx)
except Exception as e:
print("failed to collect agg files for config {}. Error: {}".format(config_idx, e))
pdf_paths = [path_join(path, fname) for fname in pdf_filenames]
pdf_paths = [f for f in pdf_paths if os.path.exists(f)]
merger = PdfFileMerger()
merger.setPageLayout("/TwoColumnLeft")
for pdf in pdf_paths:
merger.append(pdf)
merger.write(path_join(summary_dir, "{}_results.pdf".format(config_idx)))
merger.close()
energies_file.close()
losses_file.close()
if __name__ == "__main__":
main()