-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_inspect_plots.py
147 lines (112 loc) · 6.25 KB
/
run_inspect_plots.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
import pandas as pd
import tensorflow as tf
from collections import OrderedDict
import os
import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
from tensorflow.keras.layers import Input, Dense, Conv1D, GlobalAveragePooling1D, Dropout
from tensorflow.keras import Sequential,Model
import tensorflow.keras as keras
import socket
if socket.gethostname() == 'marvin':
config = ConfigProto()
config.gpu_options.allow_growth = True
sess = InteractiveSession(config=config)
import numpy as np
import matplotlib.pyplot as pplot
tfd = tfp.distributions
from ipywidgets import FloatSlider, IntSlider, interact, interactive
from datasets.femto_bearing import FEMTOBearingsDataset
from minigraphnets import Edge,Node, Graph
from graphnet_utils import GraphNetFunctionFactory, GraphNet
import sys
if __name__ == "__main__":
## Load model:
model_path = sys.argv[1]
models_root = 'models_sept20_runs'
p = pd.read_pickle(os.path.join(models_root,'runs_dataframe'))
figures_root = os.path.join(*[models_root,'figures'])
#p = pd.read_pickle("models/runs_dataframe")
p['min_val_loss'] = p['losses'].apply(lambda x : np.min(x['val_loss']))
model_row = p[p['model_path'] == model_path].iloc[0]
gn_tot = GraphNetFunctionFactory.make_from_record(model_row)
gn_tot.load(model_path)
self = gn_tot
def eval_graphnets(graph_data_, iterations = 5, eval_mode = "safe", return_reparametrization = True):
"""
graph_data_ : is a "graph" object that contains a batch of graphs (more correctly, a graph tuple as DM calls it)
iterations : number of core iterations for the computation.
return_distr_params : return the distribution parameters instead of the distribution itself. This is in place because of some buggy model loading (loaded models don't return distribution objects).
"""
graph_out = self.graph_indep.graph_eval(graph_data_,eval_mode = eval_mode)
for iterations in range(iterations):
graph_out = self.core.graph_eval(graph_out, eval_mode = eval_mode) + graph_out # Addition adds all representations (look at implementation of "Graph")
# Finally the node_to_prob returns a reparametrized "Gamma" distribution from only the final node state
if not return_reparametrization:
return self.core.node_to_prob_function(graph_out.nodes[-1].node_attr_tensor)
else:
v = self.core.node_to_prob_function.get_layer("output")(graph_out.nodes[-1].node_attr_tensor)
return _instantiate_gamma(v)
def _instantiate_gamma(t, NParams_ = 1):
return tfd.Gamma(concentration = t[...,0:NParams_], rate = t[...,NParams_:2*NParams_])
femto_dataset = FEMTOBearingsDataset()
femto_dataset.set_dataset_config({'training_set' : model_row.training_set, 'validation_set' : model_row.validation_set})
from utils import get_graph_data
experiments_to_plot = [1]
def plot_experiments(experiments_to_plot):
#if True:
#training = inds_exp_source
nsampled = 500
#pplot.figure(figsize = (15,10), dpi = 150)
pplot.figure(figsize = (15,10), dpi = 75)
nnodes_list = [1,10,25]
nseq_len = [100,200,300]
minspacing= [10,10,10]
gnsteps = [5,5,5]
normalization_factor_time = femto_dataset.normalization_factor_time
kk = 0;
for ee in experiments_to_plot:
for nnodes, gnsteps_,nseq_,minspacing_ in zip(nnodes_list, gnsteps, nseq_len, minspacing):
#ee = training[0]
graphs, y_times = get_graph_data(ee, X_ = femto_dataset.X, eid_oh_ = femto_dataset.eid_oh,
yrem_norm_ = femto_dataset.yrem_norm, n_sampled_graphs = nsampled,
nnodes=nnodes, fixed_spacing_indices=False, min_spacing=minspacing_,
nseq_range=nseq_)
probs = eval_graphnets(graphs,gnsteps_, eval_mode="batched")
#eval_graphnets()
ids_sorted = np.argsort(y_times)
time_grid = np.linspace(np.min(y_times),np.max(y_times), 150);
time_grid = np.linspace(np.min(y_times), 60000./normalization_factor_time, 150)
#time_grid = np.linspace(np.min(y_times),3.5, 150);
e_y = probs.mean()
p_y = probs.prob(time_grid).numpy().T
y_times_sorted = y_times[ids_sorted];
pplot.subplot( len(experiments_to_plot),len(nnodes_list), kk+1)
pplot.pcolor([r for r in range(p_y.shape[1])], time_grid*normalization_factor_time, p_y[:,ids_sorted], cmap = "gray")
pplot.plot(y_times_sorted *normalization_factor_time)
q90 = np.quantile(probs.sample(1000).numpy(),[0.05,0.95],0)[:,:,0].T[ids_sorted]
#pplot.plot(e_y.numpy()[ids_sorted]*normalization_factor_time,'C1',label = "$E[t_f]$",alpha = 0.5)<3
pplot.plot(q90*femto_dataset.normalization_factor_time,'C1-.', alpha = 0.4)
pplot.ylim(0,75000)
pplot.xlabel("Sample")
pplot.ylabel("Time to failure [s]")
nll = -np.mean(probs.log_prob(y_times[np.newaxis].T))
title = "Experiment %i\n Obs/nodes/nseq/gnstep :%i,%i,%i,%i \nnll:%2.3f"%(ee,nnodes,nseq_, gnsteps_,minspacing_,nll)
pplot.title(title)
kk+=1
#p_y.shape
#pplot.show()
pplot.subplots_adjust(hspace = 0.75, wspace = 0.7)
#unseen = inds_exp_source[3:6]#[4:7]#inds_exp_source[0:3] #inds_exp_target[0:3]
model_hash = model_path[len(models_root)+1 :-9]
#model_row
plot_experiments(femto_dataset.inds_exp_target)
print("-"*10)
print(figures_root)
pplot.savefig(os.path.join(figures_root , str(model_hash) + "_target.png")) ;
plot_experiments(femto_dataset.inds_exp_source[0:3])
pplot.savefig(os.path.join(figures_root , str(model_hash) + "_source1.png")) ;
plot_experiments(femto_dataset.inds_exp_source[3:6])
pplot.savefig(os.path.join(figures_root , str(model_hash) + "_source2.png")) ;