/
PlotMatplot.py
1170 lines (1041 loc) · 38.8 KB
/
PlotMatplot.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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2019-2020, RTE (https://www.rte-france.com)
# See AUTHORS.txt
# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
# you can obtain one at http://mozilla.org/MPL/2.0/.
# SPDX-License-Identifier: MPL-2.0
# This file is part of Grid2Op, Grid2Op a testbed platform to model sequential decision making in power systems.
import copy
import io
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from grid2op.PlotGrid.BasePlot import BasePlot
from grid2op.PlotGrid.PlotUtil import PlotUtil as pltu
import matplotlib.patches as patches
from matplotlib.lines import Line2D
from grid2op.PlotGrid.config import * # all colors
class GenDraw(patches.CirclePolygon):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
Empty class to handle the legend
"""
def __init__(self, *args, resolution=5, **kwargs):
patches.CirclePolygon.__init__(self, *args, resolution=resolution, **kwargs)
class LoadDraw(patches.CirclePolygon):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
Empty class to handle the legend
"""
def __init__(self, *args, resolution=3, **kwargs):
patches.CirclePolygon.__init__(self, *args, resolution=resolution, **kwargs)
class StorageDraw(patches.CirclePolygon):
"""
INTERNAL
.. warning:: /!\\\\ Internal, do not use unless you know what you are doing /!\\\\
Empty class to handle the legend
"""
def __init__(self, *args, resolution=4, **kwargs):
patches.CirclePolygon.__init__(self, *args, resolution=resolution, **kwargs)
# TODO refactor this class to make possible some calls like
# plotmatplot.plot_info(...).plot_gentype(...) is possible
# TODO add some transparency when coloring=... is used in plot_info
# TODO code the load part in the plot_info
class PlotMatplot(BasePlot):
"""
This class uses the python library "matplotlib" to draw the powergrid.
Attributes
----------
width: ``int``
Width of the figure in pixels
height: ``int``
Height of the figure in pixel
dpi: ``int``
Dots per inch, to convert pixels dimensions into inches
_scale: ``float``
Scale of the drawing in arbitrary units
_sub_radius: ``int``
Substation circle size
_sub_face_color: ``str``
Substation circle fill color
_sub_edge_color: ``str``
Substation circle edge color
_sub_txt_color: ``str``
Substation info text color
_load_radius: ``int``
Load circle size
_load_name: ``bool``
Show load names (default True)
_load_face_color: ``str``
Load circle fill color
_load_edge_color: ``str``
Load circle edge color
_load_txt_color: ``str``
Load info text color
_load_line_color: ``str``
Color of the line from load to substation
_load_line_width: ``int``
Width of the line from load to substation
_gen_radius: ``int``
Generators circle size
_gen_name: ``bool``
Show generators names (default True)
_gen_face_color: ``str``
Generators circle fill color
_gen_edge_color: ``str``
Generators circle edge color
_gen_txt_color: ``str``
Generators info txt color
_gen_line_color: ``str``
Color of the line form generator to substation
_gen_line_width: ``str``
Width of the line from generator to substation
_line_color_scheme: ``list``
List of color strings to color powerlines based on rho values
_line_color_width: ``int``
Width of the powerlines lines
_line_bus_radius: ``int``
Size of the bus display circle
_line_bus_face_colors: ``list``
List of 3 colors strings, each corresponding to the fill color of the bus circle
_line_arrow_len: ``int``
Length of the arrow on the powerlines
_line_arrow_width: ``int``
Width of the arrow on the powerlines
Examples
--------
You can use it this way:
.. code-block:: python
import grid2op
from grid2op.PlotGrid import PlotMatplot
env = grid2op.make("l2rpn_case14_sandbox")
plot_helper = PlotMatplot(env.observation_space)
# and now plot an observation (for example)
obs = env.reset()
fig = plot_helper.plot_obs(obs)
fig.show()
# more information about it on the `getting_started/8_PlottingCapabilities.ipynb` notebook of grid2op
"""
def __init__(
self,
observation_space,
width=1280,
height=720,
grid_layout=None,
dpi=96,
scale=2000.0,
bus_radius=6,
sub_radius=15,
load_radius=8,
load_name=False,
load_id=False,
load_resolution=3, # number of edges of the polygon representing the generator
gen_radius=8,
gen_name=False,
gen_id=False,
gen_resolution=5, # number of edges of the polygon representing the generator
storage_resolution=4, # number of edges of the polygon representing the generator
line_name=False,
line_id=False,
):
self.dpi = dpi
super().__init__(observation_space, width, height, scale, grid_layout)
self._sub_radius = sub_radius
self._sub_face_color = "w"
self._sub_edge_color = "blue"
self._sub_txt_color = "black"
self._display_sub_name = True
self._load_radius = load_radius
self._load_name = load_name
self._load_id = load_id
self._load_face_color = "w"
self._load_edge_color = "orange"
self._load_resolution = load_resolution
self._load_patch = self._load_patch_default
self._load_txt_color = "black"
self._load_line_color = "black"
self._load_line_width = 1
self._display_load_name = True
self._gen_radius_orig = gen_radius
self._gen_radius = None # init in self.restore_gen_palette()
self._gen_resolution = gen_resolution
self._gen_patch = self._gen_patch_default
self._gen_name = gen_name
self._gen_id = gen_id
self._gen_face_color = "w"
self._gen_edge_color_orig = "green"
self._gen_edge_color = None
self._gen_txt_color = "black"
self._gen_line_color = "black"
self._gen_line_width_orig = 1
self._gen_line_width = None
self._display_gen_value = True
self._display_gen_name = True
self.restore_gen_palette()
self._storage_radius = load_radius
self._storage_name = load_name
self._storage_id = load_id # bool : do i plot the id
self._storage_face_color = "w"
self._storage_edge_color = "purple"
self._storage_resolution = storage_resolution
self._storage_patch = self._storage_patch_default
self._storage_txt_color = "black"
self._storage_line_color = "black"
self._storage_line_width = 1
self._display_storage_name = True
self._line_name = line_name
self._line_id = line_id
self._line_color_scheme_orig = ["blue", "orange", "red"]
self._line_color_scheme = None
self.restore_line_palette()
self._line_color_width = 1
self._line_bus_radius = bus_radius
self._line_bus_face_colors = ["black", "red", "lime"]
self._line_arrow_len = 10
self._line_arrow_width = 10.0
self.xlim = [0, 0]
self.xpad = 5
self.ylim = [0, 0]
self.ypad = 5
# for easize manipulation
self.legend = None
self.figure = None
def _gen_patch_default(self, xy, radius, edgecolor, facecolor):
"""default patch used to draw generator"""
# TODO maybe make a better version of this
patch = GenDraw(
xy,
radius=radius,
edgecolor=edgecolor,
facecolor=facecolor,
resolution=self._gen_resolution,
linewidth=self._gen_line_width,
)
return patch
def _load_patch_default(self, xy, radius, edgecolor, facecolor):
"""default patch used to draw generator"""
# TODO maybe make a better version of this
patch = LoadDraw(
xy,
radius=radius,
edgecolor=edgecolor,
facecolor=facecolor,
resolution=self._load_resolution,
)
return patch
def _storage_patch_default(self, xy, radius, edgecolor, facecolor):
"""default patch used to draw generator"""
# TODO maybe make a better version of this
patch = StorageDraw(
xy,
radius=radius,
edgecolor=edgecolor,
facecolor=facecolor,
resolution=self._storage_resolution,
)
return patch
def _v_textpos_from_dir(self, dirx, diry):
if diry > 0:
return "bottom"
else:
return "top"
def _h_textpos_from_dir(self, dirx, diry):
if dirx == 0:
return "center"
elif dirx > 0:
return "left"
else:
return "right"
def create_figure(self):
# lazy loading of graphics library (reduce loading time)
# and mainly because matplolib has weird impact on argparse
import matplotlib.pyplot as plt
w_inch = self.width / self.dpi
h_inch = self.height / self.dpi
f = plt.figure(figsize=(w_inch, h_inch), dpi=self.dpi)
self.ax = f.subplots()
f.canvas.draw()
return f
def clear_figure(self, figure):
self.xlim = [0, 0]
self.ylim = [0, 0]
figure.clear()
self.ax = figure.subplots()
def convert_figure_to_numpy_HWC(self, figure):
w, h = figure.get_size_inches() * figure.dpi
w = int(w)
h = int(h)
buf = io.BytesIO()
figure.canvas.print_raw(buf)
buf.seek(0)
img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
buf.close()
img_arr = np.reshape(img_arr, (h, w, 4))
return img_arr
def _draw_substation_txt(self, pos_x, pos_y, text):
self.ax.text(
pos_x,
pos_y,
text,
color=self._sub_txt_color,
horizontalalignment="center",
verticalalignment="center",
)
def _draw_substation_circle(self, pos_x, pos_y):
patch = patches.Circle(
(pos_x, pos_y),
radius=self._sub_radius,
facecolor=self._sub_face_color,
edgecolor=self._sub_edge_color,
)
self.ax.add_patch(patch)
def draw_substation(self, figure, observation, sub_id, sub_name, pos_x, pos_y):
self.xlim[0] = min(self.xlim[0], pos_x - self._sub_radius)
self.xlim[1] = max(self.xlim[1], pos_x + self._sub_radius)
self.ylim[0] = min(self.ylim[0], pos_y - self._sub_radius)
self.ylim[1] = max(self.ylim[1], pos_y + self._sub_radius)
self._draw_substation_circle(pos_x, pos_y)
if self._display_sub_name:
self._draw_substation_txt(pos_x, pos_y, str(sub_id))
def _draw_load_txt(self, pos_x, pos_y, sub_x, sub_y, text):
dir_x, dir_y = pltu.vec_from_points(sub_x, sub_y, pos_x, pos_y)
off_x, off_y = pltu.norm_from_points(sub_x, sub_y, pos_x, pos_y)
txt_x = pos_x + off_x * self._gen_radius
txt_y = pos_y + off_y * self._gen_radius
ha = self._h_textpos_from_dir(dir_x, dir_y)
va = self._v_textpos_from_dir(dir_x, dir_y)
self.ax.text(
txt_x,
txt_y,
text,
color=self._load_txt_color,
horizontalalignment=ha,
fontsize="small",
verticalalignment=va,
)
def _draw_load_name(self, pos_x, pos_y, txt):
self.ax.text(
pos_x,
pos_y,
txt,
color=self._load_txt_color,
va="center",
ha="center",
fontsize="x-small",
)
def _draw_load_circle(self, pos_x, pos_y):
patch = self._load_patch(
(pos_x, pos_y),
radius=self._load_radius,
facecolor=self._load_face_color,
edgecolor=self._load_edge_color,
)
self.ax.add_patch(patch)
def _draw_load_line(self, pos_x, pos_y, sub_x, sub_y):
codes = [Path.MOVETO, Path.LINETO]
verts = [(pos_x, pos_y), (sub_x, sub_y)]
path = Path(verts, codes)
patch = patches.PathPatch(
path, color=self._load_line_color, lw=self._load_line_width
)
self.ax.add_patch(patch)
def _draw_load_bus(self, pos_x, pos_y, norm_dir_x, norm_dir_y, bus_id):
center_x = pos_x + norm_dir_x * self._sub_radius
center_y = pos_y + norm_dir_y * self._sub_radius
face_color = self._line_bus_face_colors[bus_id]
patch = patches.Circle(
(center_x, center_y), radius=self._line_bus_radius, facecolor=face_color
)
self.ax.add_patch(patch)
def draw_load(
self,
figure,
observation,
load_id,
load_name,
load_bus,
load_value,
load_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
self.xlim[0] = min(self.xlim[0], pos_x - self._load_radius)
self.xlim[1] = max(self.xlim[1], pos_x + self._load_radius)
self.ylim[0] = min(self.ylim[0], pos_y - self._load_radius)
self.ylim[1] = max(self.ylim[1], pos_y + self._load_radius)
self._draw_load_line(pos_x, pos_y, sub_x, sub_y)
self._draw_load_circle(pos_x, pos_y)
load_txt = ""
if self._load_name:
load_txt += '"{}":\n'.format(load_name)
if self._load_id:
load_txt += "id: {}\n".format(load_id)
if load_value is not None:
load_txt += pltu.format_value_unit(load_value, load_unit)
if load_txt:
self._draw_load_txt(pos_x, pos_y, sub_x, sub_y, load_txt)
if self._display_load_name:
self._draw_load_name(pos_x, pos_y, str(load_id))
load_dir_x, load_dir_y = pltu.norm_from_points(sub_x, sub_y, pos_x, pos_y)
self._draw_load_bus(sub_x, sub_y, load_dir_x, load_dir_y, load_bus)
def update_load(
self,
figure,
observation,
load_id,
load_name,
load_bus,
load_value,
load_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
pass
def draw_storage(
self,
figure,
observation,
load_id,
load_name,
load_bus,
load_value,
load_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
self.xlim[0] = min(self.xlim[0], pos_x - self._load_radius)
self.xlim[1] = max(self.xlim[1], pos_x + self._load_radius)
self.ylim[0] = min(self.ylim[0], pos_y - self._load_radius)
self.ylim[1] = max(self.ylim[1], pos_y + self._load_radius)
self._draw_storage_line(
pos_x, pos_y, sub_x, sub_y
) # line from the storage to the substation
self._draw_storage_circle(pos_x, pos_y) # storage element
load_txt = ""
if self._storage_name:
load_txt += '"{}":\n'.format(load_name)
if self._storage_id:
load_txt += "id: {}\n".format(load_id)
if load_value is not None:
load_txt += pltu.format_value_unit(load_value, load_unit)
if load_txt:
self._draw_load_txt(pos_x, pos_y, sub_x, sub_y, load_txt)
if self._display_load_name:
self._draw_load_name(pos_x, pos_y, str(load_id))
load_dir_x, load_dir_y = pltu.norm_from_points(sub_x, sub_y, pos_x, pos_y)
self._draw_storage_bus(sub_x, sub_y, load_dir_x, load_dir_y, load_bus)
def _draw_storage_circle(self, pos_x, pos_y):
patch = self._storage_patch(
(pos_x, pos_y),
radius=self._storage_radius,
facecolor=self._storage_face_color,
edgecolor=self._storage_edge_color,
)
self.ax.add_patch(patch)
def _draw_storage_line(self, pos_x, pos_y, sub_x, sub_y):
codes = [Path.MOVETO, Path.LINETO]
verts = [(pos_x, pos_y), (sub_x, sub_y)]
path = Path(verts, codes)
patch = patches.PathPatch(
path, color=self._storage_line_color, lw=self._storage_line_width
)
self.ax.add_patch(patch)
def _draw_storage_bus(self, pos_x, pos_y, norm_dir_x, norm_dir_y, bus_id):
center_x = pos_x + norm_dir_x * self._sub_radius
center_y = pos_y + norm_dir_y * self._sub_radius
face_color = self._line_bus_face_colors[bus_id]
patch = patches.Circle(
(center_x, center_y), radius=self._line_bus_radius, facecolor=face_color
)
self.ax.add_patch(patch)
def update_storage(
self,
figure,
observation,
storage_name,
storage_id,
storage_bus,
storage_value,
storage_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
pass
def _draw_gen_txt(self, pos_x, pos_y, sub_x, sub_y, text):
dir_x, dir_y = pltu.vec_from_points(sub_x, sub_y, pos_x, pos_y)
off_x, off_y = pltu.norm_from_points(sub_x, sub_y, pos_x, pos_y)
txt_x = pos_x + off_x * self._gen_radius
txt_y = pos_y + off_y * self._gen_radius
ha = self._h_textpos_from_dir(dir_x, dir_y)
va = self._v_textpos_from_dir(dir_x, dir_y)
self.ax.text(
txt_x,
txt_y,
text,
color=self._gen_txt_color,
wrap=True,
fontsize="small",
horizontalalignment=ha,
verticalalignment=va,
)
def _draw_gen_circle(self, pos_x, pos_y, gen_edgecolor):
patch = self._gen_patch(
(pos_x, pos_y),
radius=self._gen_radius,
edgecolor=gen_edgecolor,
facecolor=self._gen_face_color,
)
self.ax.add_patch(patch)
def _draw_gen_line(self, pos_x, pos_y, sub_x, sub_y):
codes = [Path.MOVETO, Path.LINETO]
verts = [(pos_x, pos_y), (sub_x, sub_y)]
path = Path(verts, codes)
patch = patches.PathPatch(
path, color=self._gen_line_color, lw=self._load_line_width
)
self.ax.add_patch(patch)
def _draw_gen_name(self, pos_x, pos_y, txt):
self.ax.text(
pos_x,
pos_y,
txt,
color=self._gen_txt_color,
va="center",
ha="center",
fontsize="x-small",
)
def _draw_gen_bus(self, pos_x, pos_y, norm_dir_x, norm_dir_y, bus_id):
center_x = pos_x + norm_dir_x * self._sub_radius
center_y = pos_y + norm_dir_y * self._sub_radius
face_color = self._line_bus_face_colors[bus_id]
patch = patches.Circle(
(center_x, center_y), radius=self._line_bus_radius, facecolor=face_color
)
self.ax.add_patch(patch)
def draw_gen(
self,
figure,
observation,
gen_id,
gen_name,
gen_bus,
gen_value,
gen_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
self.xlim[0] = min(self.xlim[0], pos_x - self._gen_radius)
self.xlim[1] = max(self.xlim[1], pos_x + self._gen_radius)
self.ylim[0] = min(self.ylim[0], pos_y - self._gen_radius)
self.ylim[1] = max(self.ylim[1], pos_y + self._gen_radius)
hide = False
if isinstance(self._gen_edge_color, str):
# case where the color of the generator is a string (same color for all generators)
gen_color = self._gen_edge_color
else:
my_val = observation.prod_p[gen_id]
n_colors = len(self._gen_edge_color) - 1
if np.isfinite(my_val):
color_idx = max(0, min(n_colors, int(my_val * n_colors)))
else:
color_idx = 0
hide = True
gen_color = self._gen_edge_color[color_idx]
if not hide:
self._draw_gen_line(pos_x, pos_y, sub_x, sub_y)
self._draw_gen_circle(pos_x, pos_y, gen_color)
gen_txt = ""
if self._gen_name:
gen_txt += '"{}":\n'.format(gen_name)
if self._gen_id:
gen_txt += "id: {}\n".format(gen_id)
if gen_value is not None and self._display_gen_value:
gen_txt += pltu.format_value_unit(gen_value, gen_unit)
if gen_txt:
self._draw_gen_txt(pos_x, pos_y, sub_x, sub_y, gen_txt)
if self._display_gen_name:
self._draw_gen_name(pos_x, pos_y, str(gen_id))
gen_dir_x, gen_dir_y = pltu.norm_from_points(sub_x, sub_y, pos_x, pos_y)
self._draw_gen_bus(sub_x, sub_y, gen_dir_x, gen_dir_y, gen_bus)
def update_gen(
self,
figure,
observation,
gen_id,
gen_name,
gen_bus,
gen_value,
gen_unit,
pos_x,
pos_y,
sub_x,
sub_y,
):
pass
def _draw_powerline_txt(self, pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, text):
pos_x, pos_y = pltu.middle_from_points(pos_or_x, pos_or_y, pos_ex_x, pos_ex_y)
off_x, off_y = pltu.orth_norm_from_points(
pos_or_x, pos_or_y, pos_ex_x, pos_ex_y
)
txt_x = pos_x + off_x * (self._load_radius / 2)
txt_y = pos_y + off_y * (self._load_radius / 2)
ha = self._h_textpos_from_dir(off_x, off_y)
va = self._v_textpos_from_dir(off_x, off_y)
self.ax.text(
txt_x,
txt_y,
text,
color=self._gen_txt_color,
fontsize="small",
horizontalalignment=ha,
verticalalignment=va,
)
def _draw_powerline_line(
self, pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, color, line_style
):
codes = [Path.MOVETO, Path.LINETO]
verts = [(pos_or_x, pos_or_y), (pos_ex_x, pos_ex_y)]
path = Path(verts, codes)
patch = patches.PathPatch(
path, color=color, lw=self._line_color_width, ls=line_style
)
self.ax.add_patch(patch)
def _draw_powerline_bus(self, pos_x, pos_y, norm_dir_x, norm_dir_y, bus_id):
center_x = pos_x + norm_dir_x * self._sub_radius
center_y = pos_y + norm_dir_y * self._sub_radius
face_color = self._line_bus_face_colors[bus_id]
patch = patches.Circle(
(center_x, center_y), radius=self._line_bus_radius, facecolor=face_color
)
self.ax.add_patch(patch)
def _draw_powerline_arrow(
self, pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, color, watt_value
):
sign = 1.0 if watt_value > 0.0 else -1.0
off = 1.0 if watt_value > 0.0 else 2.0
dx, dy = pltu.norm_from_points(pos_or_x, pos_or_y, pos_ex_x, pos_ex_y)
lx = dx * self._line_arrow_len
ly = dy * self._line_arrow_len
arr_x = pos_or_x + dx * self._sub_radius + off * lx
arr_y = pos_or_y + dy * self._sub_radius + off * ly
patch = patches.FancyArrow(
arr_x,
arr_y,
sign * lx,
sign * ly,
length_includes_head=True,
head_length=self._line_arrow_len,
head_width=self._line_arrow_width,
edgecolor=color,
facecolor=color,
)
self.ax.add_patch(patch)
def assign_line_palette(
self, palette_name="YlOrRd", nb_color=10, line_color_scheme=None
):
"""
Assign a new color palette when you want to plot information on the powerline.
Parameters
----------
palette_name: ``str``
Name of the Maplotlib.plyplot palette to use (name forwarded to `plt.get_cmap(palette_name)`)
nb_color: ``int``
Number of color to use
Examples
-------
.. code-block:: python
# color a grid based on the value of the thermal limit
plot_helper.assign_line_palette(nb_color=100)
# plot this grid
_ = plot_helper.plot_info(line_values=env.get_thermal_limit(), line_unit="A", coloring="line")
# restore the default coloring (["blue", "orange", "red"])
plot_helper.restore_line_palette()
Notes
-----
Some palette are available there `colormaps <https://matplotlib.org/tutorials/colors/colormaps.html>`_
"""
if line_color_scheme is None:
palette = plt.get_cmap(palette_name)
cols = []
for i in range(1, nb_color + 1):
cols.append(palette(i / nb_color))
self._line_color_scheme = cols
else:
self._line_color_scheme = copy.deepcopy(line_color_scheme)
def restore_line_palette(self):
self._line_color_scheme = self._line_color_scheme_orig
def assign_gen_palette(
self,
palette_name="YlOrRd",
nb_color=10,
increase_gen_size=None,
gen_line_width=None,
):
"""
Assign a new color palette when you want to plot information on the generator.
Parameters
----------
palette_name: ``str``
Name of the Maplotlib.plyplot palette to use (name forwarded to `plt.get_cmap(palette_name)`)
nb_color: ``int``
Number of color to use
increase_gen_size: ``float``
Whether or not to increase the generator sizes (``None`` to disable this feature, 1 has no effect)
gen_line_width: ``float``
Increase the width of the generator (if not ``None``)
Examples
-------
.. code-block:: python
# color a grid based on the value of the thermal limit
plot_helper.assign_gen_palette(nb_color=100)
# plot this grid
_ = plot_helper.plot_info(gen_values=env.gen_pmax, coloring="gen")
# restore the default coloring (all green)
plot_helper.restore_gen_palette()
Notes
-----
Some palette are available there `colormaps <https://matplotlib.org/tutorials/colors/colormaps.html>`_
"""
if palette_name is not None and nb_color > 0:
# the user changed the palette
palette = plt.get_cmap(palette_name)
cols = []
for i in range(1, nb_color + 1):
cols.append(palette(i / nb_color))
self._gen_edge_color = cols
if increase_gen_size is not None:
# the user changed the generator sizes
self._gen_radius = float(increase_gen_size) * self._gen_radius_orig
if gen_line_width is not None:
# the user changed the generator line width
self._gen_line_width = float(gen_line_width)
def restore_gen_palette(self):
"""restore every properties of the default generator layout"""
self._gen_edge_color = self._gen_edge_color_orig
self._gen_radius = self._gen_radius_orig
self._gen_line_width = self._gen_line_width_orig
def draw_powerline(
self,
figure,
observation,
line_id,
line_name,
connected,
line_value,
line_unit,
or_bus,
pos_or_x,
pos_or_y,
ex_bus,
pos_ex_x,
pos_ex_y,
):
rho = observation.rho[line_id]
n_colors = len(self._line_color_scheme) - 1
hide = False
if np.isfinite(rho):
color_idx = max(0, min(n_colors, int(rho * n_colors)))
else:
color_idx = 0
hide = True
color = "black"
if not hide:
if connected and rho > 0.0:
color = self._line_color_scheme[color_idx]
line_style = "-" if connected else "--"
self._draw_powerline_line(
pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, color, line_style
)
# Deal with line text configurations
txt = ""
if self._line_name:
txt += '"{}"\n'.format(line_name)
if self._line_id:
txt += "id: {}\n".format(str(line_id))
if line_value is not None:
txt += pltu.format_value_unit(line_value, line_unit)
if txt:
self._draw_powerline_txt(pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, txt)
or_dir_x, or_dir_y = pltu.norm_from_points(
pos_or_x, pos_or_y, pos_ex_x, pos_ex_y
)
self._draw_powerline_bus(pos_or_x, pos_or_y, or_dir_x, or_dir_y, or_bus)
ex_dir_x, ex_dir_y = pltu.norm_from_points(
pos_ex_x, pos_ex_y, pos_or_x, pos_or_y
)
self._draw_powerline_bus(pos_ex_x, pos_ex_y, ex_dir_x, ex_dir_y, ex_bus)
watt_value = observation.p_or[line_id]
if rho > 0.0 and np.abs(watt_value) >= 1e-7:
self._draw_powerline_arrow(
pos_or_x, pos_or_y, pos_ex_x, pos_ex_y, color, watt_value
)
def update_powerline(
self,
figure,
observation,
line_id,
line_name,
connected,
line_value,
line_unit,
or_bus,
pos_or_x,
pos_or_y,
ex_bus,
pos_ex_x,
pos_ex_y,
):
pass
def _get_gen_legend(self):
"""super complex function to display the proper shape in the legend"""
if isinstance(self._gen_edge_color, str):
gen_legend_col = self._gen_edge_color
else:
gen_legend_col = self._gen_edge_color[int(len(self._gen_edge_color) / 2)]
my_res = self._gen_resolution
class GenObjectHandler:
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
xdescent, ydescent = handlebox.xdescent, handlebox.ydescent
width, height = handlebox.width, handlebox.height
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
pp_ = GenDraw(
xy=center,
radius=min(width, height),
facecolor="w",
edgecolor=gen_legend_col,
transform=handlebox.get_transform(),
resolution=my_res,
)
handlebox.add_artist(pp_)
return pp_
gen_legend = self._gen_patch(
(0, 0),
facecolor=self._gen_face_color,
edgecolor=gen_legend_col,
radius=self._gen_radius,
)
return gen_legend, GenObjectHandler()
def _get_load_legend(self):
"""super complex function to display the proper shape in the legend"""
if isinstance(self._load_edge_color, str):
load_legend_col = self._load_edge_color
else:
load_legend_col = self._load_edge_color[int(len(self._load_edge_color) / 2)]
my_res = self._load_resolution
class LoadObjectHandler:
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
xdescent, ydescent = handlebox.xdescent, handlebox.ydescent
width, height = handlebox.width, handlebox.height
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
pp_ = LoadDraw(
xy=center,
radius=min(width, height),
facecolor="w",
edgecolor=load_legend_col,
transform=handlebox.get_transform(),
resolution=my_res,
)
handlebox.add_artist(pp_)
return pp_
load_legend = self._load_patch(
(0, 0),
facecolor=self._load_face_color,
edgecolor=load_legend_col,
radius=self._load_radius,
)
return load_legend, LoadObjectHandler()
def _get_storage_legend(self):
"""super complex function to display the proper shape in the legend"""
if isinstance(self._storage_edge_color, str):
storage_legend_col = self._storage_edge_color
else:
storage_legend_col = self._storage_edge_color[
int(len(self._storage_edge_color) / 2)
]
my_res = self._storage_resolution
class StorageObjectHandler:
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
xdescent, ydescent = handlebox.xdescent, handlebox.ydescent
width, height = handlebox.width, handlebox.height
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
pp_ = StorageDraw(
xy=center,
radius=min(width, height),
facecolor="w",
edgecolor=storage_legend_col,
transform=handlebox.get_transform(),
resolution=my_res,
)
handlebox.add_artist(pp_)
return pp_
storage_legend = self._storage_patch(
(0, 0),
facecolor=self._storage_face_color,
edgecolor=storage_legend_col,
radius=self._storage_radius,
)