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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
|
"""ABCs."""
# Authors: Guillaume Favelier <guillaume.favelier@gmail.com
# Eric Larson <larson.eric.d@gmail.com>
# Alex Rockhill <aprockhill@mailbox.org>
#
# License: Simplified BSD
from abc import ABC, abstractmethod, abstractclassmethod
from contextlib import nullcontext
import warnings
from ..utils import tight_layout
class Figure3D(ABC):
"""Class that refers to a 3D figure.
.. note::
This class should not be instantiated directly via
``mne.viz.Figure3D(...)``. Instead, use
:func:`mne.viz.create_3d_figure`.
See Also
--------
mne.viz.create_3d_figure
"""
# Here we use _init rather than __init__ so that users are less tempted to
# instantiate the class directly. It also helps us
# document the class more easily, as we don't have to say what all the
# params are in public docs.
@abstractclassmethod
def _init(self, fig=None, size=(600, 600), bgcolor=(0., 0., 0.),
name=None, show=False, shape=(1, 1), splash=False):
pass
@property
def plotter(self):
"""The native 3D plotting widget.
Returns
-------
plotter : instance of pyvista.Plotter
The plotter. Useful for interacting with the native 3D library.
"""
return self._plotter
class _AbstractRenderer(ABC):
@abstractclassmethod
def __init__(self, fig=None, size=(600, 600), bgcolor=(0., 0., 0.),
name=None, show=False, shape=(1, 1), splash=False):
"""Set up the scene."""
pass
@property
@abstractmethod
def _kind(self):
pass
@abstractclassmethod
def subplot(self, x, y):
"""Set the active subplot."""
pass
@abstractclassmethod
def scene(self):
"""Return scene handle."""
pass
@abstractclassmethod
def set_interaction(self, interaction):
"""Set interaction mode."""
pass
@abstractclassmethod
def legend(self, labels, border=False, size=0.1, face='triangle',
loc='upper left'):
"""Add a legend to the scene.
Parameters
----------
labels : list of tuples
Each entry must contain two strings, (label, color),
where ``label`` is the name of the item to add, and
``color`` is the color of the label to add.
border : bool
Controls if there will be a border around the legend.
The default is False.
size : float
The size of the entire figure window.
loc : str
The location of the legend.
face : str
Face shape of legend face. One of the following:
* None: ``None``
* Line: ``"-"`` or ``"line"``
* Triangle: ``"^"`` or ``'triangle'``
* Circle: ``"o"`` or ``'circle'``
* Rectangle: ``"r"`` or ``'rectangle'``
"""
pass
@abstractclassmethod
def mesh(self, x, y, z, triangles, color, opacity=1.0, shading=False,
backface_culling=False, scalars=None, colormap=None,
vmin=None, vmax=None, interpolate_before_map=True,
representation='surface', line_width=1., normals=None,
polygon_offset=None, **kwargs):
"""Add a mesh in the scene.
Parameters
----------
x : array, shape (n_vertices,)
The array containing the X component of the vertices.
y : array, shape (n_vertices,)
The array containing the Y component of the vertices.
z : array, shape (n_vertices,)
The array containing the Z component of the vertices.
triangles : array, shape (n_polygons, 3)
The array containing the indices of the polygons.
color : tuple | str
The color of the mesh as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
opacity : float
The opacity of the mesh.
shading : bool
If True, enable the mesh shading.
backface_culling : bool
If True, enable backface culling on the mesh.
scalars : ndarray, shape (n_vertices,)
The scalar valued associated to the vertices.
vmin : float | None
vmin is used to scale the colormap.
If None, the min of the data will be used
vmax : float | None
vmax is used to scale the colormap.
If None, the max of the data will be used
colormap :
The colormap to use.
interpolate_before_map :
Enabling makes for a smoother scalars display. Default is True.
When False, OpenGL will interpolate the mapped colors which can
result is showing colors that are not present in the color map.
representation : str
The representation of the mesh: either 'surface' or 'wireframe'.
line_width : int
The width of the lines when representation='wireframe'.
normals : array, shape (n_vertices, 3)
The array containing the normal of each vertex.
polygon_offset : float
If not None, the factor used to resolve coincident topology.
kwargs : args
The arguments to pass to triangular_mesh
Returns
-------
surface :
Handle of the mesh in the scene.
"""
pass
@abstractclassmethod
def contour(self, surface, scalars, contours, width=1.0, opacity=1.0,
vmin=None, vmax=None, colormap=None,
normalized_colormap=False, kind='line', color=None):
"""Add a contour in the scene.
Parameters
----------
surface : surface object
The mesh to use as support for contour.
scalars : ndarray, shape (n_vertices,)
The scalar valued associated to the vertices.
contours : int | list
Specifying a list of values will only give the requested contours.
width : float
The width of the lines or radius of the tubes.
opacity : float
The opacity of the contour.
vmin : float | None
vmin is used to scale the colormap.
If None, the min of the data will be used
vmax : float | None
vmax is used to scale the colormap.
If None, the max of the data will be used
colormap :
The colormap to use.
normalized_colormap : bool
Specify if the values of the colormap are between 0 and 1.
kind : 'line' | 'tube'
The type of the primitives to use to display the contours.
color :
The color of the mesh as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
"""
pass
@abstractclassmethod
def surface(self, surface, color=None, opacity=1.0,
vmin=None, vmax=None, colormap=None,
normalized_colormap=False, scalars=None,
backface_culling=False, polygon_offset=None):
"""Add a surface in the scene.
Parameters
----------
surface : surface object
The information describing the surface.
color : tuple | str
The color of the surface as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
opacity : float
The opacity of the surface.
vmin : float | None
vmin is used to scale the colormap.
If None, the min of the data will be used
vmax : float | None
vmax is used to scale the colormap.
If None, the max of the data will be used
colormap :
The colormap to use.
scalars : ndarray, shape (n_vertices,)
The scalar valued associated to the vertices.
backface_culling : bool
If True, enable backface culling on the surface.
polygon_offset : float
If not None, the factor used to resolve coincident topology.
"""
pass
@abstractclassmethod
def sphere(self, center, color, scale, opacity=1.0,
resolution=8, backface_culling=False,
radius=None):
"""Add sphere in the scene.
Parameters
----------
center : ndarray, shape(n_center, 3)
The list of centers to use for the sphere(s).
color : tuple | str
The color of the sphere as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
scale : float
The scaling applied to the spheres. The given value specifies
the maximum size in drawing units.
opacity : float
The opacity of the sphere(s).
resolution : int
The resolution of the sphere created. This is the number
of divisions along theta and phi.
backface_culling : bool
If True, enable backface culling on the sphere(s).
radius : float | None
Replace the glyph scaling by a fixed radius value for each
sphere.
"""
pass
@abstractclassmethod
def tube(self, origin, destination, radius=0.001, color='white',
scalars=None, vmin=None, vmax=None, colormap='RdBu',
normalized_colormap=False, reverse_lut=False):
"""Add tube in the scene.
Parameters
----------
origin : array, shape(n_lines, 3)
The coordinates of the first end of the tube(s).
destination : array, shape(n_lines, 3)
The coordinates of the other end of the tube(s).
radius : float
The radius of the tube(s).
color : tuple | str
The color of the tube as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
scalars : array, shape (n_quivers,) | None
The optional scalar data to use.
vmin : float | None
vmin is used to scale the colormap.
If None, the min of the data will be used
vmax : float | None
vmax is used to scale the colormap.
If None, the max of the data will be used
colormap :
The colormap to use.
opacity : float
The opacity of the tube(s).
backface_culling : bool
If True, enable backface culling on the tube(s).
reverse_lut : bool
If True, reverse the lookup table.
Returns
-------
actor :
The actor in the scene.
surface :
Handle of the tube in the scene.
"""
pass
@abstractclassmethod
def quiver3d(self, x, y, z, u, v, w, color, scale, mode, resolution=8,
glyph_height=None, glyph_center=None, glyph_resolution=None,
opacity=1.0, scale_mode='none', scalars=None,
backface_culling=False, colormap=None, vmin=None, vmax=None,
line_width=2., name=None):
"""Add quiver3d in the scene.
Parameters
----------
x : array, shape (n_quivers,)
The X component of the position of the quiver.
y : array, shape (n_quivers,)
The Y component of the position of the quiver.
z : array, shape (n_quivers,)
The Z component of the position of the quiver.
u : array, shape (n_quivers,)
The last X component of the quiver.
v : array, shape (n_quivers,)
The last Y component of the quiver.
w : array, shape (n_quivers,)
The last Z component of the quiver.
color : tuple | str
The color of the quiver as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
scale : float
The scaling applied to the glyphs. The size of the glyph
is by default calculated from the inter-glyph spacing.
The given value specifies the maximum glyph size in drawing units.
mode : 'arrow', 'cone' or 'cylinder'
The type of the quiver.
resolution : int
The resolution of the glyph created. Depending on the type of
glyph, it represents the number of divisions in its geometric
representation.
glyph_height : float
The height of the glyph used with the quiver.
glyph_center : tuple
The center of the glyph used with the quiver: (x, y, z).
glyph_resolution : float
The resolution of the glyph used with the quiver.
opacity : float
The opacity of the quiver.
scale_mode : 'vector', 'scalar' or 'none'
The scaling mode for the glyph.
scalars : array, shape (n_quivers,) | None
The optional scalar data to use.
backface_culling : bool
If True, enable backface culling on the quiver.
colormap :
The colormap to use.
vmin : float | None
vmin is used to scale the colormap.
If None, the min of the data will be used
vmax : float | None
vmax is used to scale the colormap.
If None, the max of the data will be used
line_width : float
The width of the 2d arrows.
Returns
-------
actor :
The actor in the scene.
surface :
Handle of the quiver in the scene.
"""
pass
@abstractclassmethod
def text2d(self, x_window, y_window, text, size=14, color='white'):
"""Add 2d text in the scene.
Parameters
----------
x : float
The X component to use as position of the text in the
window coordinates system (window_width, window_height).
y : float
The Y component to use as position of the text in the
window coordinates system (window_width, window_height).
text : str
The content of the text.
size : int
The size of the font.
color : tuple | str
The color of the text as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
"""
pass
@abstractclassmethod
def text3d(self, x, y, z, text, width, color='white'):
"""Add 2d text in the scene.
Parameters
----------
x : float
The X component to use as position of the text.
y : float
The Y component to use as position of the text.
z : float
The Z component to use as position of the text.
text : str
The content of the text.
width : float
The width of the text.
color : tuple | str
The color of the text as a tuple (red, green, blue) of float
values between 0 and 1 or a valid color name (i.e. 'white'
or 'w').
"""
pass
@abstractclassmethod
def scalarbar(self, source, color="white", title=None, n_labels=4,
bgcolor=None):
"""Add a scalar bar in the scene.
Parameters
----------
source :
The object of the scene used for the colormap.
color :
The color of the label text.
title : str | None
The title of the scalar bar.
n_labels : int | None
The number of labels to display on the scalar bar.
bgcolor :
The color of the background when there is transparency.
"""
pass
@abstractclassmethod
def show(self):
"""Render the scene."""
pass
@abstractclassmethod
def close(self):
"""Close the scene."""
pass
@abstractclassmethod
def set_camera(self, azimuth=None, elevation=None, distance=None,
focalpoint=None, roll=None, reset_camera=True):
"""Configure the camera of the scene.
Parameters
----------
azimuth : float
The azimuthal angle of the camera.
elevation : float
The zenith angle of the camera.
distance : float
The distance to the focal point.
focalpoint : tuple
The focal point of the camera: (x, y, z).
roll : float
The rotation of the camera along its axis.
reset_camera : bool
If True, reset the camera properties beforehand.
"""
pass
@abstractclassmethod
def reset_camera(self):
"""Reset the camera properties."""
pass
@abstractclassmethod
def screenshot(self, mode='rgb', filename=None):
"""Take a screenshot of the scene.
Parameters
----------
mode : str
Either 'rgb' or 'rgba' for values to return.
Default is 'rgb'.
filename : str | None
If not None, save the figure to the disk.
"""
pass
@abstractclassmethod
def project(self, xyz, ch_names):
"""Convert 3d points to a 2d perspective.
Parameters
----------
xyz : array, shape(n_points, 3)
The points to project.
ch_names : array, shape(_n_points,)
Names of the channels.
"""
pass
@abstractclassmethod
def remove_mesh(self, mesh_data):
"""Remove the given mesh from the scene.
Parameters
----------
mesh_data : tuple | Surface
The mesh to remove.
"""
pass
# -------------------
# Widget Abstractions
# -------------------
class _AbstractWidget(ABC):
@abstractclassmethod
def __init__(self):
pass
@abstractmethod
def _show(self):
pass
@abstractmethod
def _hide(self):
pass
@abstractmethod
def _set_enabled(self, state):
pass
@abstractmethod
def _is_enabled(self):
pass
@abstractmethod
def _update(self, repaint=True):
pass
@abstractmethod
def _set_style(self, style):
pass
@abstractmethod
def _get_tooltip(self):
pass
@abstractmethod
def _set_tooltip(self, tooltip: str):
pass
@abstractmethod
def _add_keypress(self, callback):
pass
@abstractmethod
def _trigger_keypress(self, key):
pass
@abstractmethod
def _set_focus(self):
pass
@abstractmethod
def _set_layout(self, layout):
pass
@abstractmethod
def _set_theme(self, theme):
pass
@abstractmethod
def _set_size(self, width=None, height=None):
pass
class _AbstractLabel(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, center=False, selectable=False):
pass
class _AbstractText(_AbstractWidget):
@abstractclassmethod
def __init__(self, value=None, placeholder=None, callback=None):
pass
@abstractmethod
def _set_value(self, value):
pass
class _AbstractButton(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, callback, icon=None):
pass
@abstractmethod
def _click(self):
pass
@abstractmethod
def _set_icon(self, icon):
pass
class _AbstractSlider(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, rng, callback, horizontal=True):
pass
@abstractmethod
def _set_value(self, value):
pass
@abstractmethod
def _get_value(self):
pass
@abstractmethod
def _set_range(self, rng):
pass
class _AbstractProgressBar(_AbstractWidget):
@abstractclassmethod
def __init__(self, count):
pass
@abstractmethod
def _increment(self):
pass
class _AbstractCheckBox(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, callback):
pass
@abstractmethod
def _set_checked(self, checked):
pass
@abstractmethod
def _get_checked(self):
pass
class _AbstractSpinBox(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, rng, callback, step=None):
pass
@abstractmethod
def _set_value(self, value):
pass
@abstractmethod
def _get_value(self):
pass
class _AbstractComboBox(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, items, callback):
pass
@abstractmethod
def _set_value(self, value):
pass
@abstractmethod
def _get_value(self):
pass
class _AbstractRadioButtons(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, items, callback):
pass
@abstractmethod
def _set_value(self, value):
pass
@abstractmethod
def _get_value(self):
pass
class _AbstractGroupBox(_AbstractWidget):
@abstractclassmethod
def __init__(self, name, items):
pass
class _AbstractFileButton(_AbstractWidget):
@abstractclassmethod
def __init__(self, callback, content_filter=None, initial_directory=None,
save=False, is_directory=False, icon='folder', window=None):
pass
class _AbstractPlayMenu(_AbstractWidget):
@abstractclassmethod
def __init__(self, value, rng, callback):
pass
@abstractmethod
def _play(self):
pass
@abstractmethod
def _pause(self):
pass
@abstractmethod
def _reset(self):
pass
@abstractmethod
def _loop(self):
pass
@abstractmethod
def _set_value(self, value):
pass
class _AbstractPopup(_AbstractWidget):
_supported_button_names = ['Ok']
# TODO: Add back support for below, file browser takes care of most
# so no big need currently
'''
# from QMessageBox.StandardButtons
['Ok', 'Open', 'Save', 'Cancel', 'Close', 'Discard', 'Apply',
'Reset', 'RestoreDefaults', 'Help', 'SaveAll', 'Yes',
'YesToAll', 'No', 'NoToAll', 'Abort', 'Retry', 'Ignore']
'''
_supported_icon_names = ['question', 'information', 'warning', 'critical']
@abstractmethod
def __init__(self, title, text, info_text=None, callback=None,
icon='Warning', buttons=None, window=None):
pass
@abstractmethod
def _click(self, value):
pass
# -------
# Layouts
# -------
class _AbstractBoxLayout(ABC):
@abstractmethod
def _add_widget(self, widget):
pass
@abstractmethod
def _add_stretch(self, amount=1):
pass
class _AbstractHBoxLayout(_AbstractBoxLayout):
@abstractmethod
def __init__(self, height=None, scroll=None):
pass
class _AbstractVBoxLayout(_AbstractBoxLayout):
@abstractmethod
def __init__(self, width=None, scroll=None):
pass
class _AbstractGridLayout(ABC):
@abstractmethod
def __init__(self, height=None, width=None, scroll=None):
pass
@abstractmethod
def _add_widget(self, widget, row=None, col=None):
pass
class _AbstractAppWindow(ABC):
def __init__(self, size=None, fullscreen=False):
pass
@abstractmethod
def _set_central_layout(self, central_layout):
pass
@abstractmethod
def _get_dpi(self):
pass
@abstractmethod
def _get_size(self):
pass
@abstractmethod
def _get_cursor(self):
pass
@abstractmethod
def _set_cursor(self, cursor):
pass
@abstractmethod
def _new_cursor(self, name):
pass
@abstractmethod
def _close_connect(self, func, *, after=True):
pass
@abstractmethod
def _close_disconnect(self, after=True):
pass
@abstractmethod
def _clean(self):
pass
@abstractmethod
def _show(self, block=False):
pass
@abstractmethod
def _close(self):
pass
# -------------------
# Matplotlib Canvases
# -------------------
class _AbstractCanvas(ABC):
def __init__(self, width=None, height=None, dpi=None):
"""Initialize the matplotlib Canvas."""
pass
def show(self):
"""Show the canvas."""
if self.manager is None:
self.show()
else:
self.manager.show()
def close(self):
"""Close the canvas."""
self.close()
def update(self):
"""Update the canvas."""
self.fig.canvas.draw()
self.fig.canvas.flush_events()
def clear(self):
"""Clear internal variables."""
self.close()
self.ax.clear()
self.fig.clear()
self.manager = None
@abstractmethod
def _set_size(self, width=None, height=None):
pass
# ------------------------------------
# Non-object-based Widget Abstractions
# ------------------------------------
# These are planned to be deprecated in favor of the simpler, object-
# oriented abstractions above when time allows.
class _AbstractToolBar(ABC):
@abstractmethod
def _tool_bar_initialize(self, name="default", window=None):
pass
@abstractmethod
def _tool_bar_add_button(self, name, desc, func, *, icon_name=None,
shortcut=None):
pass
@abstractmethod
def _tool_bar_update_button_icon(self, name, icon_name):
pass
@abstractmethod
def _tool_bar_add_text(self, name, value, placeholder):
pass
@abstractmethod
def _tool_bar_add_spacer(self):
pass
@abstractmethod
def _tool_bar_add_file_button(self, name, desc, func, *, shortcut=None):
pass
@abstractmethod
def _tool_bar_add_play_button(self, name, desc, func, *, shortcut=None):
pass
class _AbstractDock(ABC):
@abstractmethod
def _dock_initialize(self, window=None, name="Controls",
area="left", max_width=None):
pass
@abstractmethod
def _dock_finalize(self):
pass
@abstractmethod
def _dock_show(self):
pass
@abstractmethod
def _dock_hide(self):
pass
@abstractmethod
def _dock_add_stretch(self, layout=None):
pass
@abstractmethod
def _dock_add_layout(self, vertical=True):
pass
@abstractmethod
def _dock_add_label(
self, value, *, align=False, layout=None, selectable=False
):
pass
@abstractmethod
def _dock_add_button(
self, name, callback, *, style='pushbutton', icon=None, tooltip=None,
layout=None
):
pass
@abstractmethod
def _dock_named_layout(self, name, *, layout=None, compact=True):
pass
@abstractmethod
def _dock_add_slider(self, name, value, rng, callback, *,
compact=True, double=False, tooltip=None,
layout=None):
pass
@abstractmethod
def _dock_add_check_box(self, name, value, callback, *, tooltip=None,
layout=None):
pass
@abstractmethod
def _dock_add_spin_box(self, name, value, rng, callback, *,
compact=True, double=True, step=None,
tooltip=None, layout=None):
pass
@abstractmethod
def _dock_add_combo_box(self, name, value, rng, callback, *, compact=True,
tooltip=None, layout=None):
pass
@abstractmethod
def _dock_add_radio_buttons(self, value, rng, callback, *, vertical=True,
layout=None):
pass
@abstractmethod
def _dock_add_group_box(self, name, *, collapse=None, layout=None):
pass
@abstractmethod
def _dock_add_text(self, name, value, placeholder, *, callback=None,
layout=None):
pass
@abstractmethod
def _dock_add_file_button(
self, name, desc, func, *, filter=None, initial_directory=None,
save=False, is_directory=False, icon=False, tooltip=None, layout=None
):
pass
class _AbstractMenuBar(ABC):
@abstractmethod
def _menu_initialize(self, window=None):
pass
@abstractmethod
def _menu_add_submenu(self, name, desc):
pass
@abstractmethod
def _menu_add_button(self, menu_name, name, desc, func):
pass
class _AbstractStatusBar(ABC):
@abstractmethod
def _status_bar_initialize(self, window=None):
pass
@abstractmethod
def _status_bar_add_label(self, value, *, stretch=0):
pass
@abstractmethod
def _status_bar_add_progress_bar(self, stretch=0):
pass
@abstractmethod
def _status_bar_update(self):
pass
class _AbstractPlayback(ABC):
@abstractmethod
def _playback_initialize(self, func, timeout, value, rng,
time_widget, play_widget):
pass
class _AbstractKeyPress(ABC):
@abstractmethod
def _keypress_initialize(self, widget=None):
pass
@abstractmethod
def _keypress_add(self, shortcut, callback):
pass
@abstractmethod
def _keypress_trigger(self, shortcut):
pass
class _AbstractDialog(ABC):
@abstractmethod
def _dialog_create(self, title, text, info_text, callback, *,
icon='Warning', buttons=[], modal=True, window=None):
pass
class _AbstractLayout(ABC):
@abstractmethod
def _layout_initialize(self, max_width):
pass
@abstractmethod
def _layout_add_widget(self, layout, widget, stretch=0,
*, row=None, col=None):
pass
@abstractmethod
def _layout_create(self, orientation='vertical'):
pass
class _AbstractWidgetList(ABC):
@abstractmethod
def set_enabled(self, state):
pass
@abstractmethod
def get_value(self, idx):
pass
@abstractmethod
def set_value(self, idx, value):
pass
class _AbstractWdgt(ABC):
def __init__(self, widget):
self._widget = widget
@property
def widget(self):
return self._widget
@abstractmethod
def set_value(self, value):
pass
@abstractmethod
def get_value(self):
pass
@abstractmethod
def set_range(self, rng):
pass
@abstractmethod
def show(self):
pass
@abstractmethod
def hide(self):
pass
@abstractmethod
def set_enabled(self, state):
pass
@abstractmethod
def is_enabled(self):
pass
@abstractmethod
def update(self, repaint=True):
pass
@abstractmethod
def get_tooltip(self):
pass
@abstractmethod
def set_tooltip(self, tooltip: str):
pass
@abstractmethod
def set_style(self, style):
pass
class _AbstractAction(ABC):
def __init__(self, action):
self._action = action
@abstractmethod
def trigger(self):
pass
@abstractmethod
def set_icon(self):
pass
@abstractmethod
def set_shortcut(self):
pass
class _AbstractMplInterface(ABC):
@abstractmethod
def _mpl_initialize():
pass
class _AbstractMplCanvas(ABC):
def __init__(self, width, height, dpi):
"""Initialize the MplCanvas."""
from matplotlib import rc_context
from matplotlib.figure import Figure
# prefer constrained layout here but live with tight_layout otherwise
context = nullcontext
self._extra_events = ('resize',)
try:
context = rc_context({'figure.constrained_layout.use': True})
self._extra_events = ()
except KeyError:
pass
with context:
self.fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = self.fig.add_subplot(111)
self.axes.set(xlabel='Time (sec)', ylabel='Activation (AU)')
self.manager = None
def _connect(self):
for event in ('button_press', 'motion_notify') + self._extra_events:
self.canvas.mpl_connect(
event + '_event', getattr(self, 'on_' + event))
def plot(self, x, y, label, update=True, **kwargs):
"""Plot a curve."""
line, = self.axes.plot(
x, y, label=label, **kwargs)
if update:
self.update_plot()
return line
def plot_time_line(self, x, label, update=True, **kwargs):
"""Plot the vertical line."""
line = self.axes.axvline(x, label=label, **kwargs)
if update:
self.update_plot()
return line
def update_plot(self):
"""Update the plot."""
with warnings.catch_warnings(record=True):
warnings.filterwarnings('ignore', 'constrained_layout')
self.canvas.draw()
def set_color(self, bg_color, fg_color):
"""Set the widget colors."""
self.axes.set_facecolor(bg_color)
self.axes.xaxis.label.set_color(fg_color)
self.axes.yaxis.label.set_color(fg_color)
self.axes.spines['top'].set_color(fg_color)
self.axes.spines['bottom'].set_color(fg_color)
self.axes.spines['left'].set_color(fg_color)
self.axes.spines['right'].set_color(fg_color)
self.axes.tick_params(axis='x', colors=fg_color)
self.axes.tick_params(axis='y', colors=fg_color)
self.fig.patch.set_facecolor(bg_color)
def show(self):
"""Show the canvas."""
if self.manager is None:
self.canvas.show()
else:
self.manager.show()
def close(self):
"""Close the canvas."""
self.canvas.close()
def clear(self):
"""Clear internal variables."""
self.close()
self.axes.clear()
self.fig.clear()
self.canvas = None
self.manager = None
def on_resize(self, event):
"""Handle resize events."""
tight_layout(fig=self.axes.figure)
class _AbstractBrainMplCanvas(_AbstractMplCanvas):
def __init__(self, brain, width, height, dpi):
"""Initialize the MplCanvas."""
super().__init__(width, height, dpi)
self.brain = brain
self.time_func = brain.callbacks["time"]
def update_plot(self):
"""Update the plot."""
leg = self.axes.legend(
prop={'family': 'monospace', 'size': 'small'},
framealpha=0.5, handlelength=1.,
facecolor=self.brain._bg_color)
for text in leg.get_texts():
text.set_color(self.brain._fg_color)
super().update_plot()
def on_button_press(self, event):
"""Handle button presses."""
# left click (and maybe drag) in progress in axes
if (event.inaxes != self.axes or
event.button != 1):
return
self.time_func(
event.xdata, update_widget=True, time_as_index=False)
on_motion_notify = on_button_press # for now they can be the same
def clear(self):
"""Clear internal variables."""
super().clear()
self.brain = None
class _AbstractWindow(ABC):
def _window_initialize(
self, *, window=None, central_layout=None, fullscreen=False
):
self._icons = dict()
self._window = None
self._interactor = None
self._mplcanvas = None
self._show_traces = None
self._separate_canvas = None
self._interactor_fraction = None
@abstractmethod
def _window_load_icons(self):
pass
@abstractmethod
def _window_close_connect(self, func, *, after=True):
pass
@abstractmethod
def _window_close_disconnect(self, after=True):
pass
@abstractmethod
def _window_get_dpi(self):
pass
@abstractmethod
def _window_get_size(self):
pass
def _window_get_mplcanvas_size(self, fraction):
ratio = (1 - fraction) / fraction
dpi = self._window_get_dpi()
w, h = self._window_get_size()
h /= ratio
return (w / dpi, h / dpi)
@abstractmethod
def _window_get_simple_canvas(self, width, height, dpi):
pass
@abstractmethod
def _window_get_mplcanvas(self, brain, interactor_fraction, show_traces,
separate_canvas):
pass
@abstractmethod
def _window_adjust_mplcanvas_layout(self):
pass
@abstractmethod
def _window_get_cursor(self):
pass
@abstractmethod
def _window_set_cursor(self, cursor):
pass
@abstractmethod
def _window_new_cursor(self, name):
pass
@abstractmethod
def _window_ensure_minimum_sizes(self):
pass
@abstractmethod
def _window_set_theme(self, theme):
pass
@abstractmethod
def _window_create(self):
pass
|