-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
114 lines (88 loc) · 2.93 KB
/
main.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
"""
Investigate how to reconstruct parameters from scarce information
"""
import sys
from datetime import datetime
import numpy as np
import networkx as nx
from tqdm import tqdm, trange
from system import System
from reconstruction import Reconstructor
from visualization import show_reconstruction_overview
class DictWrapper(dict):
"""
Dict with dot-notation access functionality
"""
def __getattr__(self, attr):
if not attr in self:
raise KeyError('{} not in {}'.format(attr, self.keys()))
return self.get(attr)
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def main(inp):
"""
Main interface
"""
if inp is None:
# generate basis of system
graph = nx.gnp_random_graph(10, 0.6)
dim = len(graph.nodes())
assert nx.is_connected(graph), 'Graph not connected'
orig_A = nx.to_numpy_matrix(graph)
orig_B = np.random.uniform(10, 20, size=dim)
nz = np.nonzero(orig_A)
orig_A[nz] = np.random.uniform(2, 5, size=len(nz[0]))
print('Original A:\n', orig_A)
print('Original B:', orig_B)
omega = 3
OMEGA_list = [2.9,3.05,3.1,3.2]#np.arange(3.7, 4.3, 0.05)
# generate solutions
data = []
for OMEGA in tqdm(OMEGA_list):
runs = []
for i in trange(dim):
mask = np.ones(dim, dtype=bool)
mask[i] = 0
Bvec = orig_B.copy()
Bvec[mask] = 0
syst = System(orig_A, Bvec, omega, OMEGA)
sols, ts = syst.solve(0.01, 100)
pdiffs = Reconstructor.extract_phase_differences(sols, ts, syst.Phi)
#print(pdiffs)
#System.plot_solution(syst.Phi(ts), sols, ts)
if pdiffs is not None:
runs.append(pdiffs)
if len(runs) > 0:
data.append(((OMEGA, omega), runs))
# cache results
fname = '{}_{}'.format(datetime.now().strftime('%Y%m%d%H%M%S'), dim)
np.save('cache/{}'.format(fname), {
'data': data,
'ts': ts,
'orig_A': orig_A,
'orig_B': orig_B
})
else:
data, ts = inp.item()['data'], inp.item()['ts']
orig_A, orig_B = inp.item()['orig_A'], inp.item()['orig_B']
dim = orig_A.shape[0]
print('Original A:\n', orig_A)
print('Original B:', orig_B)
# reconstruct parameters
recon = Reconstructor(ts, data, dim)
rec_A, rec_B = recon.reconstruct()
print('Reconstructed A:\n', rec_A)
print('Reconstructed B:', rec_B)
# plot result
bundle = DictWrapper({
'orig_A': orig_A,
'orig_B': orig_B,
'rec_A': rec_A,
'rec_B': rec_B
})
show_reconstruction_overview(bundle, verbose=dim < 20)
if __name__ == '__main__':
inp = None
if len(sys.argv) == 2:
inp = np.load(sys.argv[1])
main(inp)