-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_fig4B.py
383 lines (328 loc) · 13.6 KB
/
plot_fig4B.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import matplotlib as mpl
import collections
from matplotlib.ticker import ScalarFormatter, NullFormatter, PercentFormatter
from matplotlib import gridspec
import matplotlib.cm as cm
import sys, os
import numpy as np
import pylab
import matplotlib.pyplot as plt
import pickle, pickle
import scipy.stats
from scipy.optimize import leastsq
import scipy
import statsmodels.api as sm
import pandas as pd
from statsmodels.formula.api import ols
from copy import deepcopy
import matplotlib.pyplot as plt
import metrics_mod
params = { 'figure.figsize': (8, 10),
'legend.fontsize': 8,
# 'title.fontsize': 8,
'lines.color':'black',
'lines.linewidth':1,
'xtick.labelsize':4,
'xtick.major.pad' : 3,
'xtick.major.size' : 2,
'ytick.major.pad' : 3,
'ytick.major.size' : 2,
'ytick.labelsize':7,
'axes.labelsize':8,
'font.size':8,
'axes.labelpad':3,
# 'text.usetex' : True,
# 'legend.labelsep': 0.0005
'pdf.fonttype' : 42,
'figure.dpi': 600,
}
mpl.rcParams.update(params)
def ylabl( text, axtrans ):
pylab.text(-0.15, 0.5,text, fontsize = 10, horizontalalignment='center',
verticalalignment='center',rotation='vertical',
transform = axtrans)
def ylablsup( text, axtrans ):
pylab.text(-0.10, 0.5,text, fontsize = 8, horizontalalignment='center',
verticalalignment='center',rotation='vertical',
transform = axtrans)
def ylabr( text, axtrans ):
pylab.text(1.15, 0.5,text,fontsize = 10, horizontalalignment='center',
verticalalignment='center',rotation='vertical',
transform = axtrans)
def AU_ROC(data1, data2):
""" AUROC along first axis for two samples of equal length as computed from MannWhitneyU/N^2,
see http://en.wikipedia.org/wiki/Mann-Whitney_U"""
samples = float(len(data1))
if np.rank(data1) == 1:
data1 = data1.reshape(samples,1)
data2 = data2.reshape(samples,1)
auroc = 1. - (np.vstack((data1,data2)).argsort(axis=0).argsort(axis=0)[:samples].sum(axis=0) - samples*(samples+1.)/2.)/samples**2
auroc[np.any(np.isnan(data1), axis=0)*np.any(np.isnan(data2), axis=0)] = np.nan
return auroc
save_type = 'pdf'
folder_name = './' #SAVE PATH OF NUMERICAL EXP
save_folder = './' #FOLDER OF OUTPUT FIGURES
test = False
try:
arg = sys.argv
ci_test = str(arg[1])
variant = str(arg[2])
except:
arg = ''
ci_test = 'par_corr'
variant = 'boot_rea_highdegree'
print(variant)
name = {'par_corr':r'ParCorr', 'gp_dc':r'GPDC', 'cmi_knn':r'CMIknn'}
def get_metrics_from_file(para_setup):
name_string = '%s-'*len(para_setup) # % para_setup
name_string = name_string[:-1]
try:
print("load from metrics file %s_link_frequency_metrics.dat " % (folder_name + name_string % tuple(para_setup)))
results = pickle.load(open(folder_name + name_string % tuple(para_setup) + '_link_frequency_metrics.dat', 'rb'), encoding='latin1')
except Exception as e:
print('failed from metrics file ' , tuple(para_setup))
print(e)
return None
return results
def draw_it(paras, which, suffix=""):
figsize = (3, 2.5) #(4, 2.5)
capsize = .5
marker1 = 'o'
marker2 = 's'
marker3 = '+'
alpha_marker = 1.
params = {
'legend.fontsize': 5,
'legend.handletextpad': .05,
# 'title.fontsize': 8,
'lines.color':'black',
'lines.linewidth':.5,
'lines.markersize':2,
# 'lines.capsize':4,
'xtick.labelsize':4,
'xtick.major.pad' : 1,
'xtick.major.size' : 2,
'ytick.major.pad' : 1,
'ytick.major.size' : 2,
'ytick.labelsize':4,
'axes.labelsize':8,
'font.size':8,
'axes.labelpad':2,
# 'axes.grid': True,
'axes.spines.right' : False,
'axes.spines.top' : False,
}
mpl.rcParams.update(params)
fig = plt.figure(figsize=figsize)
gs = fig.add_gridspec(2, 2)
ax1a = fig.add_subplot(gs[0, 0])
ax1b = fig.add_subplot(gs[1, 0])
ax2a = fig.add_subplot(gs[0, 1])
ax2b = fig.add_subplot(gs[1, 1])
for method in methods:
for idx,para in enumerate(paras):
para_plot = para
N_here = N
auto_here = auto
tau_max_here = tau_max
frac_unobserved_here = frac_unobserved
pc_alpha_here = pc_alpha
T_here = T
n_links_here = links_from_N(N_here)
para_setup = (model, N_here, n_links_here, min_coeff, coeff, auto_here, contemp_fraction, frac_unobserved_here,
max_true_lag, T_here, ci_test, method, pc_alpha_here, tau_max_here, para, N_draw_bs)
metrics_dict = get_metrics_from_file(para_setup)
if metrics_dict is not None:
color_ = color_picker(method,idx)
ax1a.errorbar(para_plot, metrics_dict['adj_lagged_abs_freq_diff'+suffix][0],
yerr= metrics_dict['adj_lagged_abs_freq_diff'+suffix][1],
capsize=capsize, alpha=alpha_marker,
color=color_, marker=marker2, linestyle='solid',elinewidth=1)
print(metrics_dict['adj_lagged_abs_freq_diff'+suffix][0])
ax1b.errorbar(para_plot, metrics_dict['adj_contemp_abs_freq_diff'+suffix][0],
yerr = metrics_dict['adj_contemp_abs_freq_diff'+suffix][1],
capsize=capsize, alpha=alpha_marker,
color=color_, marker=marker2, linestyle='solid',elinewidth=1)
ax2a.errorbar(para_plot, metrics_dict['adj_anylink_abs_freq_diff'+suffix][0],
yerr = metrics_dict['adj_anylink_abs_freq_diff'+suffix][1],
capsize=capsize, alpha=alpha_marker,
color=color_, marker=marker2, linestyle='solid',elinewidth=1)
ax2b.errorbar(para_plot, metrics_dict['computation_time'][0], 0.5*(metrics_dict['computation_time'][1][1]-metrics_dict['computation_time'][1][0]), capsize=capsize, alpha=alpha_marker,
color=color_, marker='p', linestyle='solid',elinewidth=1)
axes = {'ax1a':ax1a, 'ax1b':ax1b, 'ax2a':ax2a, 'ax2b':ax2b}
for axname in axes:
ax = axes[axname]
ax.set_xlim(0, paras[-1])
ax.xaxis.set_ticks([p for p in paras])
ax.xaxis.set_ticklabels([str(p) for p in paras])
for line in ax.get_lines():
line.set_clip_on(False)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['left'].set_position(('outward', 3))
ax.spines['bottom'].set_position(('outward', 3))
ax.grid(axis='y', linewidth=0.3)
pad = 2
xmin= min(paras)-5
xmax=max(paras)+25
if axname == 'ax1a':
ax.set_title('Lagged Abs. Freq. Error', fontsize=6, pad=pad)
#ax.set_ylim(0., 1.)
ax.set_xlim(xmin, xmax)
ax.tick_params(labelbottom=False)
vals = ax.get_yticks()
ax.set_yticklabels(['{:,.1%}'.format(x/100) for x in vals])
elif axname == 'ax1b':
ax.set_title('Contemp. Abs. Freq. Error', fontsize=6, pad=pad)
#ax.set_ylim(0., 1.)
ax.set_xlim(xmin, xmax)
vals = ax.get_yticks()
ax.set_yticklabels(['{:,.1%}'.format(x/100) for x in vals])
xlabels = ax.get_xticklabels()
ax.set_xticklabels(xlabels, rotation=75, horizontalalignment='center')
elif axname == 'ax2a':
ax.set_title('All links Abs. Freq. Error', fontsize=6, pad=pad)
#ax.set_ylim(0., 1.)
ax.set_xlim(xmin, xmax)
vals = ax.get_yticks()
ax.set_yticklabels(['{:,.1%}'.format(x/100) for x in vals])
ax.tick_params(labelbottom=False)
elif axname == 'ax2b':
ax.set_title('Runtime [s]', fontsize=6, pad=pad)
# ax.set_ylim(0., 1.)
ax.set_xlim(xmin, xmax)
#ax.tick_params(labelbottom=False)
#ax.set_yscale('log')
xlabels = ax.get_xticklabels()
ax.set_xticklabels(xlabels, rotation=75, horizontalalignment='center')
axlegend = fig.add_axes([0.05, .89, 1., .05])
axlegend.axis('off')
if 'random_lineargaussian' in model:
model_name = r"$\mathcal{N}$"
elif 'random_linearmixed' in model:
model_name = r"$\mathcal{N{-}W}$"
elif 'random_nonlinearmixed' in model:
model_name = r"$\mathcal{N{-}W}^2$"
elif 'random_nonlineargaussian' in model:
model_name = r"$\mathcal{N}^2$"
else:
model_name = model
if 'fixeddensity' in model:
model_name += r"$_{d{=}0.3}$"
elif 'highdegree' in model:
model_name += r"$_{d{=}1.5}$"
if which == 'n_bs':
plt.figtext(0.5, 0.0, r"Number of bootstrap realizations", fontsize=6,
horizontalalignment='center', va='bottom')
plt.figtext(1., 1., r"%s: N=%d, $T=%d, a=%s$" %(model_name, N, T, auto)
+"\n" + r"%s, $\alpha=%s, \tau_{\max}=%d$" %(name[ci_test],pc_alpha,tau_max),
fontsize=6, ha='right', va='top')
if suffix=="_existing":
plt.title("Mean Absolute Frequency Error (Exising links)",loc="left",fontsize=7,y=0.5)
elif suffix=="_absent":
plt.title("Mean Absolute Frequency Error (Absent links)",loc="left",fontsize=7,y=0.5)
else:
plt.title("Mean Absolute Frequency Error",loc="left",fontsize=7,y=0.5)
plt.figtext(0., 1., "B", fontsize=12, fontweight='bold',ha='left', va='top')
fig.subplots_adjust(left=0.08, right=0.97, hspace=.3, bottom=0.12, top=0.85, wspace=.3)
print("Saving plot to %s" %(save_folder + '%s_mean_abs_freq_error%s.%s' %(save_suffix, suffix, save_type)))
fig.savefig(save_folder + '%s_mean_abs_freq_error%s.%s' %(save_suffix, suffix, save_type))
plot_files.append(save_folder + '%s_mean_abs_freq_error%s.%s' %(save_suffix, suffix, save_type))
def adjust_lightness(color, amount=0.5):
import matplotlib.colors as mc
import colorsys
try:
c = mc.cnames[color]
except:
c = color
c = colorsys.rgb_to_hls(*mc.to_rgb(c))
return colorsys.hls_to_rgb(c[0], max(0, min(1, amount * c[1])), c[2])
def color_picker(method,idx=0):
if "bootstrap" in method:
color= adjust_lightness('green', amount=2.5)
#color_scaling = [adjust_lightness('green', amount=i) for i in [3.5,2.9,2.3,1.7,1.1,0.5,0]]
return color
else: return 'orange'
def method_label(method):
# return method
if not 'paper' in variant:
return method
if 'standard_pcmci' in method:
if 'allpx0' in method:
return r'PCMCI$^+_0$'
elif 'laggedpx0' in method:
return r'PCMCI$^+_{0-}$'
elif 'resetlagged' in method:
return r'PCMCI$^+_{\rm reset}$'
else:
return r'PCMCI$^+$'
elif 'residualPC' in method or 'GCresPC' in method:
return r"GCresPC"
elif 'lingam' in method:
return r"LiNGAM"
elif 'pcalg' in method:
return r"PC"
else:
return method
def links_from_N(num_nodes):
if num_nodes == 2:
return 1
if 'fixeddensity' in model:
return max(num_nodes, int(0.2*num_nodes*(num_nodes-1.)/2.))
elif 'highdegree' in model:
return int(1.5*num_nodes)
else:
return num_nodes
if __name__ == '__main__':
save_type = 'pdf'
plot_files = []
fpr_precision = 'fpr'
if 'versions' in variant:
methods = [
'bootstrap_pcmci+'
]
else:
methods = []
methods += [
'bootstrap_pcmci+',
]
if ci_test == 'par_corr':
if 'mixed' in variant:
model = 'random_linearmixed' # random_lineargaussian random_linearmixed random_nonlinearmixed
else:
model = 'random_lineargaussian' # random_lineargaussian random_linearmixed random_nonlinearmixed
else:
if 'mixed' in variant:
model = 'random_nonlinearmixed' # random_lineargaussian random_linearmixed random_nonlinearmixed
else:
model = 'random_nonlineargaussian' # random_lineargaussian random_linearmixed random_nonlinearmixed
if 'fixeddensity' in variant:
model += '_fixeddensity'
elif 'highdegree' in variant:
model += '_highdegree'
if 'boot_rea' in variant:
T_here= [500]
N_here= [3]
num_rows = 3
tau_max = 2
auto_here= [0.95] #[0.95]
min_coeff = 0.1
coeff = 0.5
N_draw_bs= 1
frac_unobserved = 0.
contemp_fraction = 0.3
max_true_lag = 2
vary_n_bs= [25,100,250,500,750,1000,1500,2000,2500]
pc_alpha_here = [0.01]
for auto in auto_here:
for T in T_here:
for N in N_here:
n_links = links_from_N(N)
for pc_alpha in pc_alpha_here:
for suffix in ["","_existing","_absent"]:
para_setup_name = (variant, N, n_links, min_coeff, coeff, contemp_fraction, frac_unobserved,
max_true_lag, T, ci_test, pc_alpha, tau_max, N_draw_bs)
save_suffix = '%s-'*len(para_setup_name) % para_setup_name
save_suffix = save_suffix[:-1]
print(save_suffix)
draw_it(paras=vary_n_bs, which='n_bs', suffix=suffix)