-
Notifications
You must be signed in to change notification settings - Fork 8
/
experiment_head.py
176 lines (131 loc) · 4.78 KB
/
experiment_head.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import datetime
import multiprocessing
import time
import matplotlib.pyplot as plt
import numpy as np
import bss
from bss.head import HEADUpdate
config = {
"seed": 873009,
"n_repeat": 1000,
"n_chan": [4, 6, 8],
"tol": -1.0, # i.e. ignore tolerance
"maxiter": 100,
"dtype": np.complex128,
}
methods = {
"IPA": HEADUpdate.IPA,
"IP": HEADUpdate.IP,
"ISS": HEADUpdate.ISS,
"IP2": HEADUpdate.IP2,
"NCG": HEADUpdate.NCG,
"IPA+NCG": HEADUpdate.IPA_NCG,
}
def progress_tracker(n_tasks, queue):
n_digits = len(str(n_tasks))
fmt = "Remaining tasks: {n:" + str(n_digits) + "d} / " + str(n_tasks)
start_date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
print(f"Start processing at {start_date}")
def print_status():
print(fmt.format(n=n_tasks), end="\r")
print_status()
while n_tasks > 0:
_ = queue.get(block=True)
n_tasks -= 1
print_status()
end_date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
print(f"All done. Finished at {end_date}")
def f_loop(args):
import mkl
mkl.set_num_threads(1)
# expand arguments
V, maxiter, tol, methods, the_queue = args
infos = {}
runtimes = {}
v_mat = V[None, ...]
for method, key in methods.items():
t_start = time.perf_counter()
_, info = bss.head.head_solver(
v_mat, maxiter=maxiter, tol=tol, method=key, verbose=False, info=True
)
ellapsed = time.perf_counter() - t_start
infos[method] = info
runtimes[method] = ellapsed
the_queue.put(True)
return V.shape[-1], infos, runtimes
def rand_V(n_freq, n_chan, n_mat=None, dtype=np.complex128):
if n_mat is None:
n_mat = n_chan
# random hermitian PSD matrices
X = bss.random.crandn(n_freq, n_mat, n_chan, n_chan)
w, U = np.linalg.eigh(X)
w[:] = np.random.rand(*w.shape)
V = (U * np.abs(w[..., None, :])) @ bss.utils.tensor_H(U)
V = 0.5 * (V + bss.utils.tensor_H(V))
X = bss.random.crandn(n_freq, n_mat, n_chan, 10 * n_chan)
V = X @ bss.utils.tensor_H(X)
return V
if __name__ == "__main__":
np.random.seed(config["seed"])
# we need a queue for inter-process communication
m = multiprocessing.Manager()
the_queue = m.Queue()
# construct the list of parallel arguments
parallel_args = []
for n_chan in config["n_chan"]:
V = rand_V(config["n_repeat"], n_chan, dtype=config["dtype"],)
for v_mat in V:
parallel_args.append(
(v_mat, config["maxiter"], config["tol"], methods, the_queue)
)
np.random.shuffle(parallel_args)
# run all the simulation in parallel
prog_proc = multiprocessing.Process(
target=progress_tracker, args=(len(parallel_args), the_queue,)
)
prog_proc.start()
t_start = time.perf_counter()
pool = multiprocessing.Pool()
results = pool.map(f_loop, parallel_args)
pool.close()
t_end = time.perf_counter()
infos = dict(zip(config["n_chan"], [{} for c in config["n_chan"]]))
runtimes = dict(zip(config["n_chan"], [{} for c in config["n_chan"]]))
# Post process the results
for (n_chan, res_info, res_rt) in results:
for method in methods:
if method not in infos[n_chan]:
infos[n_chan][method] = {
"head_errors": [res_info[method]["head_errors"]],
"head_costs": [res_info[method]["head_costs"]],
}
else:
infos[n_chan][method]["head_costs"].append(
res_info[method]["head_costs"]
)
infos[n_chan][method]["head_errors"].append(
res_info[method]["head_errors"]
)
if method not in runtimes[n_chan]:
runtimes[n_chan][method] = res_rt[method]
else:
runtimes[n_chan][method] += res_rt[method]
for n_chan in config["n_chan"]:
print(f"{n_chan} channels")
for method in methods:
runtimes[n_chan][method] /= config["n_repeat"]
infos[n_chan][method]["head_costs"] = np.concatenate(
infos[n_chan][method]["head_costs"], axis=0
)
infos[n_chan][method]["head_errors"] = np.concatenate(
infos[n_chan][method]["head_errors"], axis=0
)
print(f"=== {method:7s} runtime={runtimes[n_chan][method]:.3f} ===")
# get the date
date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
filename = f"data/{date}_experiment_head_results.npz"
# save to compressed numpy file
np.savez(filename, config=config, methods=methods, infos=infos, runtimes=runtimes)
print(f"Total time spent {t_end - t_start:.3f} s")
print(f"Results saved to {filename}")