/
vector.py
819 lines (716 loc) · 33.7 KB
/
vector.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import os
import sys
import numpy as np
from multiprocessing import Pool
import networkx as nx
from pylab import plt
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
from aurespf.tools import *
from EUgrid import EU_Nodes_usage, EU_Nodes_regions, EU_Nodes_superRegions
from link_namer import *
from functions import *
from figutils import *
"""
Vector flow tracing on top of the up/down stream flow tracing algorithm.
This script can be called from command line with one of the following inputs:
- trace: vectorize the flow tracing and save results
- plot network: load results from above and plot network figures for each color
- plot network day: same as above but split for daytime and nighttime
- plot network total: plot total network usage for each color
- plot usage: plot average usage for each color
- plot levels: Bar plots of nodes' link usage at different levels for each color
- plot hour: Same as above but for different hours of the day for each ccolor
- sanity: check whether the individual colors add to the scalar flow tracing
Results from this script go to the folder: ./results/vector/
Figures go to the folder: ./figures/vector.
"""
"""
Initialisation
"""
if len(sys.argv) < 2:
raise Exception('Not enough inputs!')
else:
task = str(sys.argv[1:])
modes = ['linear', 'square']
directions = ['import', 'export', 'combined']
outPath = './results/vector/'
figPath = './figures/vector/'
Oranges_cmap = LinearSegmentedColormap('Oranges_data', Oranges_data, 1000)
Blues_cmap = LinearSegmentedColormap('Blues_data', Blues_data, 1000)
def usageCalc(F, quantiles, Usages, nodes, links, name):
"""
Calculate usages and save to file
"""
Node_contributions = np.zeros((nodes, links)) # empty array for calculated usages
for node in range(nodes):
for link in range(links):
# Stacking and sorting data
F_vert = np.reshape(F[link, :lapse], (len(F[link, :lapse]), 1))
exp_vert = np.reshape(Usages[link, node, :lapse], (len(Usages[link, node, :lapse]), 1))
F_matrix = np.hstack([F_vert, exp_vert])
F_matrix[F_matrix[:, 0].argsort()]
H, bin_edges = binMaker(F_matrix, quantiles[link], lapse)
Node_contributions[node, link] = node_contrib(H, bin_edges, linkID=link)
np.save(outPath + 'Node_contrib_' + mode + '_' + direction + '_' + str(name) + '.npy', Node_contributions)
return
def usageCalcDaily(iF, quantiles, iUsages, nodes, links, name):
"""
Calculate usages for daytime and nighttime and save to file
"""
days = lapse / 24
hours = np.append(range(6, days * 24), range(6))
splitHours = np.split(hours, days * 2)
dayTime = np.concatenate(splitHours[0::2])
nightTime = np.concatenate(splitHours[1::2])
for d in ['day', 'night']:
if d == 'day':
Usages = iUsages[:, :, dayTime]
F = iF[:, dayTime]
elif d == 'night':
Usages = iUsages[:, :, nightTime]
F = iF[:, nightTime]
Node_contributions = np.zeros((nodes, links)) # empty array for calculated usages
for node in range(nodes):
for link in range(links):
# Stacking and sorting data
F_vert = np.reshape(F[link, :lapse / 2], (len(F[link, :lapse / 2]), 1))
exp_vert = np.reshape(Usages[link, node, :lapse / 2], (len(Usages[link, node, :lapse / 2]), 1))
F_matrix = np.hstack([F_vert, exp_vert])
F_matrix[F_matrix[:, 0].argsort()]
H, bin_edges = binMaker(F_matrix, quantiles[link], lapse / 2)
Node_contributions[node, link] = node_contrib(H, bin_edges, linkID=link)
np.save(outPath + 'Node_contrib_' + mode + '_' + direction + '_' + d + '_' + str(name) + '.npy', Node_contributions)
return
def drawnet_usage(N=None, scheme='linear', direction='combined', color='solar'):
"""
Make network figures of a node's usage of links for both import, export and
combined. A figure for each color is created.
Adapted from drawnet() in aurespf.plotting
"""
colwidth = (3.425)
dcolwidth = (2 * 3.425 + 0.236)
if not N:
N = EU_Nodes_usage()
G = nx.Graph()
nodelist = []
# Add nodes and labels to networkx object for plotting
for n in N:
G.add_node(str(n.label))
nodelist.append(str(n.label))
# LF is a list of links
K, h, LF = AtoKh_old(N)
for l in LF:
G.add_edge(l[0], l[1], id=l[2])
if color == 'wind':
cmap = LinearSegmentedColormap('blue', blueDict, 1000)
elif color == 'solar':
cmap = LinearSegmentedColormap('orange', orangeDict, 1000)
else:
cmap = LinearSegmentedColormap('brown', brownDict, 1000)
# Load usages for given scheme and direction
N_usages = np.load(outPath + '/Node_contrib_' + scheme + '_' + direction + '_' + color + '.npy')
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
# Pick a particular node
for n in N:
# Calculate colors of links
N_usages[n.id] = N_usages[n.id] / quantiles
col = [(cmap(l)) for l in N_usages[n.id]]
# Create a new figure and plot network below
fig = plt.figure(dpi=400, figsize=(0.85 * dcolwidth, 0.85 * 0.8 * dcolwidth))
# color bar in bottom of figure
ax1 = fig.add_axes([0.05, 0.08, 0.9, .08])
cbl = mpl.colorbar.ColorbarBase(ax1, cmap, orientation='horizontal')
cbl.solids.set_edgecolor('face')
# Label just above color bar
if scheme == 'linear':
xlabel = 'Most localised'
elif scheme == 'square':
xlabel = 'Synchronised'
else:
xlabel = 'Market'
# ax1.set_xlabel(xlabel+' '+direction+r" usage $C_n/C^{\,99\%}$")
ax1.set_xlabel(r'$\mathcal{K}_{ln}/\mathcal{K}^T_l$')
ax1.xaxis.set_label_position('top')
ax2 = fig.add_axes([-0.05, 0.15, 1.1, 0.95])
# Set color of nodes, highlight one and draw all
node_c = ["#000000" for node in N]
node_c[n.id] = "#B30000"
nx.draw_networkx_nodes(G, pos, node_size=500, nodelist=nodelist, node_color=node_c, facecolor=(1, 1, 1))
# Draw links colored by usage of node n
edges = [(u, v) for (u, v, d) in G.edges(data=True)]
edge_id = [d['id'] for (u, v, d) in G.edges(data=True)]
color_sort = []
for i in range(len(col)):
color_sort.append(col[edge_id[i]])
nx.draw_networkx_edges(G, pos, edgelist=edges, width=3.5, edge_color=color_sort, alpha=1)
# Draw country names
nx.draw_networkx_labels(G, pos, font_size=12, font_color='w', font_family='sans-serif')
ax2.axis('off')
# Save figure
plt.savefig(figPath + "network/" + scheme + "/" + str(n.id) + '_' + color + '_' + direction + ".pdf")
plt.close()
def drawnet_total(N=None, scheme='linear', direction='combined', color='solar'):
"""
Make network figures for each color of the total network usage.
"""
colwidth = (3.425)
dcolwidth = (2 * 3.425 + 0.236)
if not N:
N = EU_Nodes_usage()
G = nx.Graph()
nodelist = []
# Add nodes and labels to networkx object for plotting
for n in N:
G.add_node(str(n.label))
nodelist.append(str(n.label))
# LF is a list of links
K, h, LF = AtoKh_old(N)
for l in LF:
G.add_edge(l[0], l[1], id=l[2])
if color == 'wind':
cmap = LinearSegmentedColormap('blue', blueDict, 1000)
elif color == 'solar':
cmap = LinearSegmentedColormap('orange', orangeDict, 1000)
else:
cmap = LinearSegmentedColormap('brown', brownDict, 1000)
# Load usages for given scheme and direction
N_usages = np.load(outPath + '/Node_contrib_' + scheme + '_' + direction + '_' + color + '.npy')
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
# Calculate colors of links
linkUsages = np.sum(N_usages, 0) / quantiles
col = [(cmap(l)) for l in linkUsages]
# Create a new figure and plot network below
fig = plt.figure(dpi=400, figsize=(0.85 * dcolwidth, 0.85 * 0.8 * dcolwidth))
# color bar in bottom of figure
ax1 = fig.add_axes([0.05, 0.08, 0.9, .08])
cbl = mpl.colorbar.ColorbarBase(ax1, cmap, orientation='horizontal')
cbl.solids.set_edgecolor('face')
# Label just above color bar
if scheme == 'linear':
xlabel = 'Most localised'
elif scheme == 'square':
xlabel = 'Synchronised'
else:
xlabel = 'Market'
# ax1.set_xlabel(xlabel+' '+direction+r" usage $C_n/C^{\,99\%}$")
ax1.set_xlabel(r'$\mathcal{K}_{l}/\mathcal{K}^T_l$')
ax1.xaxis.set_label_position('top')
ax2 = fig.add_axes([-0.05, 0.15, 1.1, 0.95])
# Set color of nodes and draw all
node_c = ["#000000" for node in N]
nx.draw_networkx_nodes(G, pos, node_size=500, nodelist=nodelist, node_color=node_c, facecolor=(1, 1, 1))
# Draw links colored by usage of node n
edges = [(u, v) for (u, v, d) in G.edges(data=True)]
edge_id = [d['id'] for (u, v, d) in G.edges(data=True)]
color_sort = []
for i in range(len(col)):
color_sort.append(col[edge_id[i]])
nx.draw_networkx_edges(G, pos, edgelist=edges, width=3.5, edge_color=color_sort, alpha=1)
# Draw country names
nx.draw_networkx_labels(G, pos, font_size=12, font_color='w', font_family='sans-serif')
ax2.axis('off')
# Save figure
plt.savefig(figPath + "network/" + scheme + "/" + 'total_' + color + '_' + direction + ".pdf")
plt.close()
def drawnet_day(N=None, scheme='linear', direction='combined', color='solar'):
"""
Make network figures for each color of the total network usage during the day and night.
"""
colwidth = (3.425)
dcolwidth = (2 * 3.425 + 0.236)
if not N:
N = EU_Nodes_usage()
G = nx.Graph()
nodelist = []
# Add nodes and labels to networkx object for plotting
for n in N:
G.add_node(str(n.label))
nodelist.append(str(n.label))
# LF is a list of links
K, h, LF = AtoKh_old(N)
for l in LF:
G.add_edge(l[0], l[1], id=l[2])
if color == 'wind':
cmap = LinearSegmentedColormap('blue', blueDict, 1000)
elif color == 'solar':
cmap = LinearSegmentedColormap('orange', orangeDict, 1000)
else:
cmap = LinearSegmentedColormap('brown', brownDict, 1000)
for time in ['day', 'night']:
# Load usages for given scheme and direction
N_usages = np.load(outPath + '/Node_contrib_' + scheme + '_' + direction + '_' + time + '_' + color + '.npy')
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
# Calculate colors of links
linkUsages = np.sum(N_usages, 0) / quantiles
col = [(cmap(l)) for l in linkUsages]
# Create a new figure and plot network below
fig = plt.figure(dpi=400, figsize=(0.85 * dcolwidth, 0.85 * 0.8 * dcolwidth))
# color bar in bottom of figure
ax1 = fig.add_axes([0.05, 0.08, 0.9, .08])
cbl = mpl.colorbar.ColorbarBase(ax1, cmap, orientation='horizontal')
cbl.solids.set_edgecolor('face')
# Label just above color bar
if scheme == 'linear':
xlabel = 'Most localised'
elif scheme == 'square':
xlabel = 'Synchronised'
else:
xlabel = 'Market'
# ax1.set_xlabel(xlabel+' '+direction+r" usage $C_n/C^{\,99\%}$")
ax1.set_xlabel(r'$\mathcal{K}_{l}/\mathcal{K}^T_l$')
ax1.xaxis.set_label_position('top')
ax2 = fig.add_axes([-0.05, 0.15, 1.1, 0.95])
# Set color of nodes and draw all
node_c = ["#000000" for node in N]
nx.draw_networkx_nodes(G, pos, node_size=500, nodelist=nodelist, node_color=node_c, facecolor=(1, 1, 1))
# Draw links colored by usage of node n
edges = [(u, v) for (u, v, d) in G.edges(data=True)]
edge_id = [d['id'] for (u, v, d) in G.edges(data=True)]
color_sort = []
for i in range(len(col)):
color_sort.append(col[edge_id[i]])
nx.draw_networkx_edges(G, pos, edgelist=edges, width=3.5, edge_color=color_sort, alpha=1)
# Draw country names
nx.draw_networkx_labels(G, pos, font_size=12, font_color='w', font_family='sans-serif')
ax2.axis('off')
# Save figure
plt.savefig(figPath + "day/" + scheme + "/" + color + '_' + direction + '_' + time + ".pdf")
plt.close()
def usagePlotter(direction):
"""
Scatter plots of nodes' import/export usages of links saved to ./figures/.
"""
legendNames = ['diagonal', r'$99\%$ quantile', 'avg. usage', 'usage']
modes = ['linear', 'square']
modeNames = ['localised', 'synchronised']
names = ['usageS', 'usageW']
colors = ['#ffa500', '#0000aa']
for mode in modes:
N = EU_Nodes_usage(mode + '.npz')
F = np.load('./results/' + mode + '-flows.npy')
Fmax = np.max(np.abs(F), 1)
nodes = len(N)
links = F.shape[0]
usageS = np.load(outPath + mode + '_' + direction + '_' + 'usageS.npy')
usageW = np.load(outPath + mode + '_' + direction + '_' + 'usageW.npy')
if mode == 'square':
usageB = np.load(outPath + mode + '_' + direction + '_' + 'usageB.npy')
names.append('usageB')
colors.append('#874a2b')
for node in xrange(nodes):
nodeLabel = N[node].label
nodePath = figPath + 'usage/' + nodeLabel.tostring()
if not os.path.exists(nodePath):
os.makedirs(nodePath)
for link in xrange(links):
linkLabel = link_label(link, N)
linkflow = abs(F[link, :])
qq = get_q(abs(F[link]), .99)
plt.figure()
ax = plt.subplot()
nBins = 90
totUsage = np.zeros((nBins))
for i, color in enumerate(names):
usages = eval(color)
usages = usages[link, node, :] / linkflow
F_vert = np.reshape(linkflow, (len(linkflow), 1))
exp_vert = np.reshape(usages, (len(usages), 1))
F_matrix = np.hstack([F_vert, exp_vert])
F_matrix[F_matrix[:, 0].argsort()]
H, bin_edges = binMaker(F_matrix, qq, lapse=70128, nbins=nBins)
plt.plot(bin_edges / qq, H[:, 1], '-', c=colors[i], lw=2)
totUsage += H[:, 1]
plt.plot(bin_edges / qq, totUsage, '-', c="#aa0000", lw=2)
plt.axis([0, 1, 0, 1])
ax.set_xticks(np.linspace(0, 1, 11))
plt.xlabel(r'$|F_l|/\mathcal{K}_l^T$')
plt.ylabel(r'$\left\langle H_{ln} \right\rangle /|F_l|$')
if mode == 'square':
modeName = modeNames[1]
plt.legend(('solar usage', 'wind usage', 'backup usage', 'total usage'), loc=1)
else:
modeName = modeNames[0]
plt.legend(('solar usage', 'wind usage', 'total usage'), loc=1)
plt.title(nodeLabel.tostring() + ' ' + modeName + ' ' + direction + ' flows on link ' + linkLabel)
plt.savefig(nodePath + '/' + str(link) + '_' + modeName + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()
def link_level_bars(levels, usages, quantiles, scheme, direction, color, nnames, lnames, admat=None):
"""
Bar plots of nodes' link usage of links at different levels.
"""
if not admat:
admat = np.genfromtxt('./settings/eadmat.txt')
if color == 'solar':
cmap = Oranges_cmap
elif color == 'wind':
cmap = Blues_cmap
elif color == 'backup':
cmap = 'Greys'
nodes, links = usages.shape
usageLevels = np.zeros((nodes, levels))
usageLevelsNorm = np.zeros((nodes, levels))
for node in range(nodes):
nl = neighbor_levels(node, levels, admat)
for lvl in range(levels):
ll = link_level(nl, lvl, nnames, lnames)
ll = np.array(ll, dtype='int')
usageSum = sum(usages[node, ll])
linkSum = sum(quantiles[ll])
usageLevels[node, lvl] = usageSum / linkSum
if lvl == 0:
usageLevelsNorm[node, lvl] = usageSum
else:
usageLevelsNorm[node, lvl] = usageSum / usageLevelsNorm[node, 0]
usageLevelsNorm[:, 0] = 1
# plot all nodes
usages = usageLevels.transpose()
plt.figure(figsize=(11, 3))
ax = plt.subplot()
plt.pcolormesh(usages[:, loadOrder], cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(1, nodes, nodes))
ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=10)
plt.ylabel('Link level')
plt.savefig(figPath + '/levels/' + str(scheme) + '/' + 'total' + '_' + str(direction) + '_' + color + '.pdf', bbox_inches='tight')
plt.close()
# plot all nodes normalised to usage of first level
usages = usageLevelsNorm.transpose()
plt.figure(figsize=(11, 3))
ax = plt.subplot()
plt.pcolormesh(usages[:, loadOrder], cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(1, nodes, nodes))
ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=10)
plt.ylabel('Link level')
plt.savefig(figPath + '/levels/' + str(scheme) + '/' + 'total_norm_cont_' + str(direction) + '_' + color + '.pdf', bbox_inches='tight')
plt.close()
def link_level_norm(levels, usages, quantiles, scheme, direction, color, nnames, lnames, admat=None):
"""
Bar plots of nodes' link usage of links at different levels normed to the
usage at the first level.
"""
if not admat:
admat = np.genfromtxt('./settings/eadmat.txt')
if color == 'solar':
cmap = Oranges_cmap
elif color == 'wind':
cmap = Blues_cmap
elif color == 'backup':
cmap = 'Greys'
links, nodes, lapse = usages.shape
usageLevels = np.zeros((nodes, levels))
usageLevelsNorm = np.zeros((nodes, levels))
for node in range(nodes):
nl = neighbor_levels(node, levels, admat)
for lvl in range(levels):
ll = link_level(nl, lvl, nnames, lnames)
ll = np.array(ll, dtype='int')
usageSum = sum(sum(usages[ll, node, :]))
linkSum = sum(quantiles[ll])
usageLevels[node, lvl] = usageSum / linkSum
if lvl == 0:
usageLevelsNorm[node, lvl] = usageSum
else:
usageLevelsNorm[node, lvl] = usageSum / usageLevelsNorm[node, 0]
usageLevelsNorm[:, 0] = 1
# plot all nodes normalised to usage of first level
usages = usageLevelsNorm.transpose()
plt.figure(figsize=(11, 3))
ax = plt.subplot()
plt.pcolormesh(usages[:, loadOrder], cmap=cmap)
plt.colorbar().set_label(label=r'$ \sum_l\, (H_{ln}(t)) / \sum_l\, (\mathcal{K}^T_l)$', size=10)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(1, nodes, nodes))
ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=10)
plt.ylabel('Link level')
plt.savefig(figPath + '/levels/' + str(scheme) + '/' + 'total_norm' + '_' + str(direction) + '_' + color + '.pdf', bbox_inches='tight')
plt.close()
def link_level_hour(levels, usages, quantiles, scheme, direction, color, nnames, lnames, admat=None):
"""
Make a color mesh of a node's average hourly usage of links at different
levels.
"""
if not admat:
admat = np.genfromtxt('./settings/eadmat.txt')
if color == 'solar':
cmap = Oranges_cmap
elif color == 'wind':
cmap = Blues_cmap
elif color == 'backup':
cmap = 'Greys'
links, nodes, lapse = usages.shape
usages = np.reshape(usages, (links, nodes, lapse / 24, 24))
totalHour = np.zeros((levels, 24))
totalNormed = np.zeros((levels, 24))
for node in range(nodes):
nl = neighbor_levels(node, levels, admat)
hourSums = np.zeros((levels, 24))
for lvl in range(levels):
ll = link_level(nl, lvl, nnames, lnames)
ll = np.array(ll, dtype='int')
meanSum = np.sum(np.mean(usages[ll, node], axis=1), axis=0)
linkSum = sum(quantiles[ll])
hourSums[lvl] = meanSum / linkSum
totalHour += hourSums
plt.figure(figsize=(9, 3))
ax = plt.subplot()
plt.pcolormesh(hourSums, cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(.5, 23.5, 24))
ax.set_xticklabels(np.array(np.linspace(1, 24, 24), dtype='int'), ha="center", va="top", fontsize=10)
plt.ylabel('Link level')
plt.axis([0, 24, 0, levels])
plt.title(nnames[node] + ' ' + direction + ' ' + color)
plt.savefig(figPath + '/hourly/' + str(scheme) + '/' + str(node) + '_' + color + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()
hourSums = hourSums / np.sum(hourSums, axis=1)[:, None]
totalNormed += hourSums
plt.figure(figsize=(9, 3))
ax = plt.subplot()
plt.pcolormesh(hourSums, cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(.5, 23.5, 24))
ax.set_xticklabels(np.array(np.linspace(1, 24, 24), dtype='int'), ha="center", va="top", fontsize=10)
plt.ylabel('Link level')
plt.axis([0, 24, 0, levels])
plt.title(nnames[node] + ' ' + direction + ' ' + color)
plt.savefig(figPath + '/hourly/' + str(scheme) + '/normed/' + str(node) + '_' + color + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()
# Plot average hourly usage
totalHour /= nodes
plt.figure(figsize=(9, 3))
ax = plt.subplot()
plt.pcolormesh(totalHour, cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(.5, 23.5, 24))
ax.set_xticklabels(np.array(np.linspace(1, 24, 24), dtype='int'), ha="center", va="top", fontsize=10)
plt.ylabel('Link level')
plt.axis([0, 24, 0, levels])
plt.savefig(figPath + '/hourly/' + str(scheme) + '/total_' + color + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()
totalNormed /= nodes
plt.figure(figsize=(9, 3))
ax = plt.subplot()
plt.pcolormesh(totalNormed, cmap=cmap)
plt.colorbar().set_label(label=r'$U_n^{(l)}$', size=11)
ax.set_yticks(np.linspace(.5, levels - .5, levels))
ax.set_yticklabels(range(1, levels + 1))
ax.yaxis.set_tick_params(width=0)
ax.xaxis.set_tick_params(width=0)
ax.set_xticks(np.linspace(.5, 23.5, 24))
ax.set_xticklabels(np.array(np.linspace(1, 24, 24), dtype='int'), ha="center", va="top", fontsize=10)
plt.ylabel('Link level')
plt.axis([0, 24, 0, levels])
plt.savefig(figPath + '/hourly/' + str(scheme) + '/normed/total_' + color + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()
if 'trace' in task:
print('tracing')
for mode in modes:
print(str(mode))
N = np.load('./results/' + mode + '_pm.npz', mmap_mode='r')
F = abs(np.load('./results/' + mode + '-flows.npy'))
quantiles = [get_q(abs(F[link]), .99) for link in range(len(F))]
nodes = 30
names = ['solar', 'wind']
meanLoads = np.reshape(N['mean'], (nodes, 1))
genS = N['normsolar']
genW = N['normwind']
genSum = genS + genW
genSum[np.where(genSum == 0)] = 1
if mode == 'square':
genB = np.divide(N['balancing'], meanLoads)
genSum += genB
normGenB = genB / genSum
names.append('backup')
normGenS = genS / genSum
normGenW = genW / genSum
for direction in directions:
print(str(direction))
if not os.path.exists(outPath + mode + '_' + direction + '_' + 'usageS.npy'):
if direction == 'combined':
Usages = np.load('./linkcolouring/old_' + mode + '_copper_link_mix_import_all_alpha=same.npy')
Usages += np.load('./linkcolouring/old_' + mode + '_copper_link_mix_export_all_alpha=same.npy')
Usages /= 2
else:
Usages = np.load('./linkcolouring/old_' + mode + '_copper_link_mix_' + direction + '_all_alpha=same.npy')
links, nodes, lapse = Usages.shape
usageS = np.zeros((links, nodes, lapse))
usageW = np.zeros((links, nodes, lapse))
for l in xrange(links):
usageS[l] = Usages[l] * normGenS
usageW[l] = Usages[l] * normGenW
np.save(outPath + mode + '_' + direction + '_' + 'usageS.npy', usageS)
np.save(outPath + mode + '_' + direction + '_' + 'usageW.npy', usageW)
if mode == 'square':
usageB = np.zeros((links, nodes, lapse))
for l in range(links):
usageB[l] = Usages[l] * normGenB
np.save(outPath + mode + '_' + direction + '_' + 'usageB.npy', usageB)
Usages = None
print('Solar')
usageCalc(F, quantiles, usageS, nodes, links, 'solar')
usageCalcDaily(F, quantiles, usageS, nodes, links, 'solar')
print('Wind')
usageCalc(F, quantiles, usageW, nodes, links, 'wind')
usageCalcDaily(F, quantiles, usageW, nodes, links, 'wind')
if mode == 'square':
print('Backup')
usageCalc(F, quantiles, usageB, nodes, links, 'backup')
usageCalcDaily(F, quantiles, usageB, nodes, links, 'backup')
else:
print('Solar')
usage = np.load(outPath + mode + '_' + direction + '_' + 'usageS.npy')
links, nodes, lapse = usage.shape
usageCalc(F, quantiles, usage, nodes, links, 'solar')
usageCalcDaily(F, quantiles, usage, nodes, links, 'solar')
print('Wind')
usage = np.load(outPath + mode + '_' + direction + '_' + 'usageW.npy')
links, nodes, lapse = usage.shape
usageCalc(F, quantiles, usage, nodes, links, 'wind')
usageCalcDaily(F, quantiles, usage, nodes, links, 'wind')
if mode == 'square':
print('Backup')
usage = np.load(outPath + mode + '_' + direction + '_' + 'usageB.npy')
links, nodes, lapse = usage.shape
usageCalc(F, quantiles, usage, nodes, links, 'backup')
usageCalcDaily(F, quantiles, usage, nodes, links, 'backup')
if 'plot' in task:
if 'network' in task:
N = EU_Nodes_usage()
colors = ['solar', 'wind']
for mode in modes:
print('Mode: ' + mode)
if mode == 'square':
colors.append('backup')
for direction in directions:
print('Direction: ' + direction)
for color in colors:
if 'total' in task:
print('Plotting total network figures')
drawnet_total(N, mode, direction, color)
elif 'day' in task:
print('Plotting day/night network figures')
drawnet_day(N, mode, direction, color)
else:
print('Plotting network figures')
drawnet_usage(N, mode, direction, color)
if 'usage' in task:
print('Plotting usage figures')
p = Pool(len(directions))
p.map(usagePlotter, directions)
if 'levels' in task:
levels = 4
N = EU_Nodes_usage()
colors = ['solar', 'wind']
c = ['S', 'W']
lnames = np.array(link_namer(N))
nnames = np.array(node_namer(N))
schemeNames = ['localised', 'synchronised']
if 'norm' not in task:
print('Plotting link levels')
for i, scheme in enumerate(modes):
if scheme == 'square':
colors.append('backup')
name = schemeNames[i]
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
for direction in directions:
for color in colors:
N_usages = np.load(outPath + 'Node_contrib_' + scheme + '_' + direction + '_' + color + '.npy')
link_level_bars(levels, N_usages, quantiles, name, direction, color, nnames, lnames)
else:
print('Plotting normed link levels')
for i, scheme in enumerate(modes):
if scheme == 'square':
colors.append('backup')
c.append('B')
name = schemeNames[i]
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
for direction in directions:
for j, color in enumerate(colors):
Usages = np.load(outPath + scheme + '_' + direction + '_' + 'usage' + c[j] + '.npy')
link_level_norm(levels, Usages, quantiles, name, direction, color, nnames, lnames)
if 'hour' in task:
print('Plotting hourly link levels')
levels = 4
N = EU_Nodes_usage()
colors = ['solar', 'wind']
c = ['S', 'W']
lnames = np.array(link_namer(N))
nnames = np.array(node_namer(N))
schemeNames = ['localised', 'synchronised']
for i, scheme in enumerate(modes):
if scheme == 'square':
colors.append('backup')
c.append('B')
name = schemeNames[i]
quantiles = np.load('./results/quantiles_' + str(scheme) + '_70128.npy')
for direction in directions:
for j, color in enumerate(colors):
Usages = np.load(outPath + scheme + '_' + direction + '_' + 'usage' + c[j] + '.npy')
link_level_hour(levels, Usages, quantiles, name, direction, color, nnames, lnames)
if 'sanity' in task:
for mode in modes:
quantiles = np.load('./results/quantiles_' + str(mode) + '_70128.npy')
for direction in directions:
S = np.load('./results/vector/Node_contrib_' + mode + '_' + direction + '_solar.npy')
S += np.load('./results/vector/Node_contrib_' + mode + '_' + direction + '_wind.npy')
if mode == 'square':
S += np.load('./results/vector/Node_contrib_' + mode + '_' + direction + '_backup.npy')
N = np.load('./results/Node_contrib_' + mode + '_' + direction + '_70128.npy')
error = abs(S - N) / N * 100
weightedError = abs(S - N) / N * quantiles / np.mean(quantiles) * 100
means = np.mean(error, axis=1)
weightedMeans = np.mean(weightedError, axis=1)
stds = np.std(error, axis=1)
nodeMean = np.mean(means)
weightedNodeMean = np.mean(weightedMeans)
x = np.linspace(.5, 29.5, 30)
if mode == 'linear': title = 'localised'
if mode == 'square': title = 'synchronised'
plt.figure()
ax = plt.subplot()
plt.errorbar(x, means[loadOrder], yerr=stds * 0, marker='s', lw=0, elinewidth=1)
plt.plot([0, 30], [nodeMean, nodeMean], '--k', lw=2)
plt.title(title + ' ' + direction + ', sum of colors vs. total network usage')
plt.ylabel('Mean link deviation in %')
ax.set_xticks(np.linspace(1, 30, 30))
ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=9)
plt.axis([0, 30, min(means) - (.1 * min(means)), max(means) + (.1 * max(means))])
plt.legend(('individual country', 'mean of countries'), loc=2, ncol=2)
plt.savefig(figPath + 'error/' + title + '_' + direction + '.pdf', bbox_inches='tight')
plt.figure()
ax = plt.subplot()
plt.errorbar(x, weightedMeans[loadOrder], yerr=stds * 0, marker='s', lw=0, elinewidth=1)
plt.plot([0, 30], [weightedNodeMean, weightedNodeMean], '--k', lw=2)
plt.title(title + ' ' + direction + ', sum of colors vs. total network usage')
plt.ylabel(r'Weighed mean link deviation in % normalised to $\left\langle \mathcal{K}^T \right\rangle$')
ax.set_xticks(np.linspace(1, 30, 30))
ax.set_xticklabels(loadNames, rotation=60, ha="right", va="top", fontsize=9)
plt.axis([0, 30, min(weightedMeans) - (.1 * min(weightedMeans)), max(weightedMeans) + (.1 * max(weightedMeans))])
plt.legend(('individual country', 'mean of countries'), loc=2, ncol=2)
plt.savefig(figPath + 'error/' + 'weighted_' + title + '_' + direction + '.pdf', bbox_inches='tight')
plt.close()