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 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "08/01/2021"
import logging
import numpy
import functools
import os
from silx.gui import qt
from silx.gui import icons
from silx.gui import colors
import silx.gui.plot
from silx.gui.plot.tools import PositionInfo
from silx.gui.plot.items.shape import Shape
import pyFAI.utils
import pyFAI.massif
import pyFAI.control_points
from .AbstractCalibrationTask import AbstractCalibrationTask
from ..helper.RingExtractor import RingExtractorThread
from ..helper.SynchronizeRawView import SynchronizeRawView
from ..helper.SynchronizePlotBackground import SynchronizePlotBackground
from ..CalibrationContext import CalibrationContext
from ..helper.MarkerManager import MarkerManager
from ..helper import ProcessingWidget
from ..utils import FilterBuilder
from ..utils import validators
from ..helper import model_transform
from ..widgets.ColoredCheckBox import ColoredCheckBox
from ..widgets.AdvancedSpinBox import AdvancedSpinBox
from ..dialog import MessageBox
_logger = logging.getLogger(__name__)
class _PeakSelectionUndoCommand(qt.QUndoCommand):
def __init__(self, parent, model, oldState, newState):
super(_PeakSelectionUndoCommand, self).__init__(parent=parent)
self.__peakPickingModel = model
self.__oldState = list(oldState)
self.__newState = list(newState)
self.__redoInhibited = False
def setRedoInhibited(self, isInhibited):
"""Allow to avoid to push the command into the QUndoStack without
calling redo."""
self.__redoInhibited = isInhibited
def undo(self):
peakPickingModel = self.__peakPickingModel
peakPickingModel.clear()
for peakModel in self.__oldState:
peakPickingModel.append(peakModel)
def redo(self):
if self.__redoInhibited:
return
peakPickingModel = self.__peakPickingModel
peakPickingModel.clear()
for peakModel in self.__newState:
peakPickingModel.append(peakModel)
class _PeakSelectionTableView(qt.QTableView):
def __init__(self, parent):
super(_PeakSelectionTableView, self).__init__(parent=parent)
ringDelegate = _SpinBoxItemDelegate(self)
palette = qt.QPalette(self.palette())
# make sure this value is not edited
palette.setColor(qt.QPalette.Base, palette.base().color())
ringDelegate.setPalette(palette)
toolDelegate = _PeakToolItemDelegate(self)
enabledDelegate = _PeakEnabledItemDelegate(self)
self.setItemDelegateForColumn(_PeakSelectionTableModel.ColumnRingNumber, ringDelegate)
self.setItemDelegateForColumn(_PeakSelectionTableModel.ColumnControl, toolDelegate)
self.setItemDelegateForColumn(_PeakSelectionTableModel.ColumnEnabled, enabledDelegate)
self.setEditTriggers(qt.QAbstractItemView.NoEditTriggers)
self.setSelectionMode(qt.QAbstractItemView.SingleSelection)
self.setSelectionBehavior(qt.QAbstractItemView.SelectRows)
self.setVerticalScrollMode(qt.QAbstractItemView.ScrollPerPixel)
self.setVerticalScrollBarPolicy(qt.Qt.ScrollBarAlwaysOn)
self.setShowGrid(False)
self.setWordWrap(False)
# NoFrame glitchies on Debian8 Qt5
# self.setFrameShape(qt.QFrame.NoFrame)
self.horizontalHeader().setHighlightSections(False)
self.verticalHeader().setVisible(False)
palette = qt.QPalette(self.palette())
palette.setColor(qt.QPalette.Base, qt.QColor(0, 0, 0, 0))
self.setPalette(palette)
self.setFrameShape(qt.QFrame.Panel)
def mousePressEvent(self, event):
"""
:param qt.QMouseEvent event: Qt event
"""
index = self.indexAt(event.pos())
if index.isValid():
selectionModel = self.selectionModel()
if selectionModel.isSelected(index):
selectionModel.clear()
event.accept()
return
return super(_PeakSelectionTableView, self).mousePressEvent(event)
def setModel(self, model):
if self.model() is not None:
m = self.model()
m.rowsInserted.disconnect(self.__onRowInserted)
m.rowsRemoved.disconnect(self.__onRowRemoved)
m.modelReset.disconnect(self.__openPersistantViewOnModelReset)
super(_PeakSelectionTableView, self).setModel(model)
if self.model() is not None:
m = self.model()
m.rowsInserted.connect(self.__onRowInserted)
m.rowsRemoved.connect(self.__onRowRemoved)
m.modelReset.connect(self.__openPersistantViewOnModelReset)
self.__openPersistantViewOnModelReset()
# it is not possible to set column constraints while there is no model
self.__updateColumnConstraints()
def sizeHint(self):
"""Size hint while grow according to the content of the view"""
rowCount = self.model().rowCount()
size = qt.QTableView.sizeHint(self)
if rowCount <= 0:
return size
height = self.horizontalHeader().size().height()
height = height + self.rowHeight(0) * rowCount
if height < size.height():
return size
size = qt.QSize(size.width(), height)
return size
def __updateColumnConstraints(self):
header = self.horizontalHeader()
if qt.qVersion() < "5.0":
setResizeMode = header.setResizeMode
else:
setResizeMode = header.setSectionResizeMode
setResizeMode(_PeakSelectionTableModel.ColumnName, qt.QHeaderView.Stretch)
setResizeMode(_PeakSelectionTableModel.ColumnPeaksCount, qt.QHeaderView.ResizeToContents)
setResizeMode(_PeakSelectionTableModel.ColumnRingNumber, qt.QHeaderView.ResizeToContents)
setResizeMode(_PeakSelectionTableModel.ColumnEnabled, qt.QHeaderView.ResizeToContents)
setResizeMode(_PeakSelectionTableModel.ColumnControl, qt.QHeaderView.Fixed)
def __onRowRemoved(self, parent, start, end):
self.updateGeometry()
def __onRowInserted(self, parent, start, end):
self.__openPersistantViewOnRowInserted(parent, start, end)
self.updateGeometry()
# It have to be done only on the 3, else the layout is wrong
self.resizeColumnToContents(_PeakSelectionTableModel.ColumnControl)
def __openPersistantViewOnRowInserted(self, parent, start, end):
model = self.model()
for row in range(start, end):
index = model.index(row, _PeakSelectionTableModel.ColumnRingNumber, qt.QModelIndex())
self.openPersistentEditor(index)
index = model.index(row, _PeakSelectionTableModel.ColumnControl, qt.QModelIndex())
self.openPersistentEditor(index)
index = model.index(row, _PeakSelectionTableModel.ColumnEnabled, qt.QModelIndex())
self.openPersistentEditor(index)
def __openPersistantViewOnModelReset(self):
model = self.model()
index = qt.QModelIndex()
row = model.rowCount(index)
self.__onRowInserted(index, 0, row)
class _PeakSelectionTableModel(qt.QAbstractTableModel):
requestRingChange = qt.Signal(object, int)
requestRemovePeak = qt.Signal(object)
requestChangeEnable = qt.Signal(object, bool)
ColumnEnabled = 0
ColumnName = 1
ColumnPeaksCount = 2
ColumnRingNumber = 3
ColumnControl = 4
def __init__(self, parent, peakSelectionModel):
assert isinstance(parent, PeakPickingTask)
super(_PeakSelectionTableModel, self).__init__(parent=parent)
self.__peakSelectionModel = peakSelectionModel
peakSelectionModel.structureChanged.connect(self.__invalidateModel)
peakSelectionModel.contentChanged.connect(self.__invalidateContentModel)
self.__callbacks = []
self.__invalidateModel()
# QAbstractTableModel do not provide access to the parent
self.__parent = parent
def __invalidateModel(self):
self.beginResetModel()
for callback in self.__callbacks:
target, method = callback
target.changed.disconnect(method)
self.__callbacks = []
for index, item in enumerate(self.__peakSelectionModel):
callback = functools.partial(self.__invalidateItem, index)
item.changed.connect(callback)
self.__callbacks.append((item, callback))
self.endResetModel()
def __invalidateContentModel(self):
for index, _ in enumerate(self.__peakSelectionModel):
self.__invalidateItem(index)
def __invalidateItem(self, index):
index1 = self.index(index, 0, qt.QModelIndex())
index2 = self.index(index, self.columnCount() - 1, qt.QModelIndex())
self.dataChanged.emit(index1, index2)
def headerData(self, section, orientation, role=qt.Qt.DisplayRole):
if orientation != qt.Qt.Horizontal:
return None
if role != qt.Qt.DisplayRole:
return super(_PeakSelectionTableModel, self).headerData(section, orientation, role)
if section == self.ColumnName:
return "Name"
elif section == self.ColumnPeaksCount:
return "Peaks"
elif section == self.ColumnRingNumber:
return "Ring number"
elif section == self.ColumnEnabled:
return ""
elif section == self.ColumnControl:
return ""
return None
def flags(self, index):
return (qt.Qt.ItemIsEnabled |
qt.Qt.ItemIsSelectable)
def rowCount(self, parent=qt.QModelIndex()):
return len(self.__peakSelectionModel)
def columnCount(self, parent=qt.QModelIndex()):
return 5
def peakObject(self, index):
peakModel = self.__peakSelectionModel[index.row()]
return peakModel
def data(self, index=qt.QModelIndex(), role=qt.Qt.DisplayRole):
if not index.isValid():
return False
peakModel = self.__peakSelectionModel[index.row()]
column = index.column()
if role == qt.Qt.DisplayRole or role == qt.Qt.EditRole:
if column == self.ColumnName:
return peakModel.name()
elif column == self.ColumnPeaksCount:
return len(peakModel)
elif column == self.ColumnRingNumber:
return peakModel.ringNumber()
return ""
return None
def setData(self, index, value, role=qt.Qt.EditRole):
if not index.isValid():
return False
peakModel = self.__peakSelectionModel[index.row()]
column = index.column()
if role == qt.Qt.CheckStateRole:
if column == self.ColumnEnabled:
if value == qt.Qt.Checked:
isChecked = True
else:
isChecked = False
self.requestChangeEnable.emit(peakModel, isChecked)
return True
elif role == qt.Qt.EditRole:
if column == self.ColumnRingNumber:
self.requestRingChange.emit(peakModel, value)
return True
return False
def removeRows(self, row, count, parent=qt.QModelIndex()):
# while the tablemodel is already connected to the data model
self.__peakSelectionModel.structureChanged.disconnect(self.__invalidateModel)
self.beginRemoveRows(parent, row, row + count - 1)
for i in reversed(range(count)):
peakModel = self.__peakSelectionModel[row + i]
self.requestRemovePeak.emit(peakModel)
self.endRemoveRows()
# while the tablemodel is already connected to the data model
self.__peakSelectionModel.structureChanged.connect(self.__invalidateModel)
return True
class _PeakPickingPlot(silx.gui.plot.PlotWidget):
PEAK_SELECTION_MODE = 0
ERASOR_MODE = 1
BRUSH_MODE = 2
sigPeakPicked = qt.Signal(int, int)
"""Emitted when a mouse interaction requesteing a peak selection."""
sigShapeErased = qt.Signal(object)
"""Emitted when a mouse interaction requesteing to erase peaks on shape."""
sigShapeBrushed = qt.Signal(object)
"""Emitted when a mouse interaction requesteing to brush peaks on shape."""
def __init__(self, parent):
super(_PeakPickingPlot, self).__init__(parent=parent)
self.setKeepDataAspectRatio(True)
self.setAxesDisplayed(False)
self.__peakSelectionModel = None
self.__callbacks = {}
self.__selectedPeak = None
self.__processing = None
self.__mode = self.PEAK_SELECTION_MODE
self.__mask = None
self.sigPlotSignal.connect(self.__onPlotEvent)
markerModel = CalibrationContext.instance().getCalibrationModel().markerModel()
self.__markerManager = MarkerManager(self, markerModel, pixelBasedPlot=True)
handle = self.getWidgetHandle()
handle.setContextMenuPolicy(qt.Qt.CustomContextMenu)
handle.customContextMenuRequested.connect(self.__plotContextMenu)
colormap = CalibrationContext.instance().getRawColormap()
self.setDefaultColormap(colormap)
self.__plotBackground = SynchronizePlotBackground(self)
if hasattr(self, "centralWidget"):
self.centralWidget().installEventFilter(self)
def setInteractiveMode(self, mode, color='black',
shape='polygon', label=None,
zoomOnWheel=True, source=None, width=None):
"""Override the function to allow to disable extrat interaction modes.
"""
self.setPeakInteractiveMode(self.PEAK_SELECTION_MODE)
silx.gui.plot.PlotWidget.setInteractiveMode(self, mode, color=color, shape=shape, label=label, zoomOnWheel=zoomOnWheel, source=source, width=width)
def peakInteractiveMode(self):
"""Returns the peak interactive mode selected."""
return self.__mode
def setPeakInteractiveMode(self, mode):
if self.__mode == mode:
return
self.__mode = mode
if mode == self.PEAK_SELECTION_MODE:
super(_PeakPickingPlot, self).setInteractiveMode('zoom')
elif mode == self.ERASOR_MODE:
color = "black"
super(_PeakPickingPlot, self).setInteractiveMode('draw', shape='rectangle', source=self, color=color)
elif mode == self.BRUSH_MODE:
color = "black"
super(_PeakPickingPlot, self).setInteractiveMode('draw', shape='rectangle', source=self, color=color)
else:
assert(False)
def setSelectedPeak(self, name):
if self.__selectedPeak == name:
return
self.__selectedPeak = name
for peakModel in self.__peakSelectionModel:
self.updatePeak(peakModel)
def eventFilter(self, widget, event):
if event.type() == qt.QEvent.Enter:
if self.__mode == self.PEAK_SELECTION_MODE:
self.setCursor(qt.Qt.CrossCursor)
else:
self.setCursor(qt.Qt.ArrowCursor)
return True
elif event.type() == qt.QEvent.Leave:
self.unsetCursor()
return True
return False
def event(self, event):
"""
Dispatch Qt events to the widget.
:param qt.QEvent event: Event from Qt
"""
if event.type() == qt.QEvent.ToolTip:
handle = self.getWidgetHandle()
pos = handle.mapFromGlobal(event.globalPos())
coord = self.pixelToData(pos.x(), pos.y())
# About 4 pixels (screen relative)
coord2 = self.pixelToData(pos.x() + 1, pos.y() + 1)
ratio = abs(coord[0] - coord2[0]), abs(coord[1] - coord2[1])
threshold = 2 * (ratio[0] + ratio[1])
peak = self.__peakSelectionModel.closestGroup((coord[1], coord[0]), threshold=threshold)
if peak is not None:
message = "Group name: %s\nRing number: %d"
message = message % (peak.name(), peak.ringNumber())
qt.QToolTip.showText(event.globalPos(), message)
else:
qt.QToolTip.hideText()
event.ignore()
return True
return super(_PeakPickingPlot, self).event(event)
def __onPlotEvent(self, event):
if self.__mode == self.PEAK_SELECTION_MODE:
if event["event"] == "imageClicked":
x, y, button = event["col"], event["row"], event["button"]
if button == "left":
self.__plotClicked(x, y)
elif self.__mode in [self.ERASOR_MODE, self.BRUSH_MODE]:
if event['event'] == 'drawingFinished':
# Convert from plot to array coords
ox, oy = 0, 0
sx, sy = 1.0, 1.0
height = int(abs(event['height'] / sy))
width = int(abs(event['width'] / sx))
row = int((event['y'] - oy) / sy)
if sy < 0:
row -= height
col = int((event['x'] - ox) / sx)
if sx < 0:
col -= width
# Use a shape in case we generalize it
# FIXME: This code should be done in silx
shape = Shape('rectangle')
points = numpy.array([[col, row], [col + width, row + height]])
shape.setPoints(points, copy=False)
if self.__mode == self.ERASOR_MODE:
self.sigShapeErased.emit(shape)
elif self.__mode == self.BRUSH_MODE:
self.sigShapeBrushed.emit(shape)
else:
assert(False)
else:
assert(False)
def __plotClicked(self, x, y):
self.sigPeakPicked.emit(x, y)
def __plotContextMenu(self, pos):
plot = self
from silx.gui.plot.actions.control import ZoomBackAction
zoomBackAction = ZoomBackAction(plot=plot, parent=plot)
menu = qt.QMenu(self)
menu.addAction(zoomBackAction)
menu.addSeparator()
menu.addAction(self.__markerManager.createMarkPixelAction(menu, pos))
menu.addAction(self.__markerManager.createMarkGeometryAction(menu, pos))
action = self.__markerManager.createRemoveClosestMaskerAction(menu, pos)
if action is not None:
menu.addAction(action)
handle = plot.getWidgetHandle()
menu.exec_(handle.mapToGlobal(pos))
def setModel(self, peakSelectionModel):
assert self.__peakSelectionModel is None
self.__peakSelectionModel = peakSelectionModel
self.__peakSelectionModel.changed.connect(self.__invalidateModel)
self.__invalidateModel()
def __invalidateModel(self):
added = set(self.__peakSelectionModel) - set(self.__callbacks.keys())
removed = set(self.__callbacks.keys()) - set(self.__peakSelectionModel)
# remove items
for peakModel in removed:
callback = self.__callbacks[peakModel]
del self.__callbacks[peakModel]
peakModel.changed.disconnect(callback)
self.removePeak(peakModel)
# add items
for peakModel in added:
callback = functools.partial(self.__invalidateItem, peakModel)
peakModel.changed.connect(callback)
self.addPeak(peakModel)
self.__callbacks[peakModel] = callback
def __invalidateItem(self, peakModel):
self.updatePeak(peakModel)
def removePeak(self, peakModel):
legend = "marker" + peakModel.name()
try:
self.removeMarker(legend=legend)
except Exception:
pass
legend = "coord" + peakModel.name()
self.removeCurve(legend=legend)
def addPeak(self, peakModel):
color = peakModel.color()
if not peakModel.isEnabled():
context = CalibrationContext.instance()
color = context.disabledMarkerColor()
numpyColor = numpy.array([color.redF(), color.greenF(), color.blueF(), 0.5])
points = peakModel.coords()
name = peakModel.name()
if self.__selectedPeak is None:
# Nothing selected
symbol = 'o'
elif name == self.__selectedPeak:
# Selected marker
symbol = 'o'
else:
# Unselected marker
symbol = '+'
if len(points) != 0:
y, x = points[0] + 0.5
self.addMarker(x=x, y=y,
legend="marker" + name,
text=name)
yy = points[:, 0] + 0.5
xx = points[:, 1] + 0.5
self.addCurve(x=xx, y=yy,
legend="coord" + name,
linestyle=' ',
selectable=False,
symbol=symbol,
color=numpyColor,
resetzoom=False)
def setProcessingLocation(self, mask):
"""Update the processing location over the image.
:param numpy.ndarray mask: Mask of the location.
"""
if mask is None:
mask = numpy.empty(shape=(0, 0))
if self.__mask is None:
self.addImage(mask,
legend="processing-mask",
selectable=False,
copy=False,
z=-0.5,
resetzoom=False)
self.__mask = self.getImage("processing-mask")
colormap = colors.Colormap(name=None,
colors=((0., 0., 0., 0.), (1., 1., 1., 0.5)),
vmin=0,
vmax=1)
self.__mask.setColormap(colormap)
mask = mask.astype(numpy.uint8)
self.__mask.setData(mask)
def updatePeak(self, peakModel):
self.removePeak(peakModel)
self.addPeak(peakModel)
def unsetProcessing(self):
if self.__processing is not None:
self.__processing.deleteLater()
self.__processing = None
def setProcessing(self):
self.__processing = ProcessingWidget.createProcessingWidgetOverlay(self)
class _SpinBoxItemDelegate(qt.QStyledItemDelegate):
def __init__(self, *args, **kwargs):
qt.QStyledItemDelegate.__init__(self, *args, **kwargs)
self.__palette = None
def setPalette(self, palette):
self.__palette = qt.QPalette(palette)
def createEditor(self, parent, option, index):
if not index.isValid():
return super(_SpinBoxItemDelegate, self).createEditor(parent, option, index)
editor = AdvancedSpinBox(parent=parent)
if self.__palette is not None:
editor.setPalette(self.__palette)
editor.setMouseWheelEnabled(False)
editor.setMinimum(1)
editor.setMaximum(999)
editor.valueChanged.connect(lambda x: self.commitData.emit(editor))
editor.setFocusPolicy(qt.Qt.StrongFocus)
editor.setValue(index.data())
editor.installEventFilter(self)
editor.setBackgroundRole(qt.QPalette.Background)
editor.setAutoFillBackground(True)
return editor
def eventFilter(self, widget, event):
if event.type() == qt.QEvent.ChildPolished:
# Fix issue relative to Qt4. after createEditor and setEditorData
# The lineedit content is set selected without any reason.
widget.lineEdit().deselect()
return qt.QSpinBox.eventFilter(self, widget, event)
def setEditorData(self, editor, index):
value = index.data()
if editor.value() == value:
return
old = editor.blockSignals(True)
editor.setValue(value)
editor.blockSignals(old)
def setModelData(self, editor, model, index):
editor.interpretText()
value = editor.value()
model.setData(index, value)
def updateEditorGeometry(self, editor, option, index):
"""
Update the geometry of the editor according to the changes of the view.
:param qt.QWidget editor: Editor widget
:param qt.QStyleOptionViewItem option: Control how the editor is shown
:param qt.QIndex index: Index of the data to display
"""
editor.setGeometry(option.rect)
class _PeakEnabledItemDelegate(qt.QStyledItemDelegate):
def createEditor(self, parent, option, index):
if not index.isValid():
return super(_PeakToolItemDelegate, self).createEditor(parent, option, index)
persistantIndex = qt.QPersistentModelIndex(index)
editor = ColoredCheckBox(parent=parent)
editor.toggled.connect(functools.partial(self.__toggleEnabled, persistantIndex))
return editor
def setEditorData(self, editor, index):
"""
:param qt.QWidget editor: Editor widget
:param qt.QIndex index: Index of the data to display
"""
model = index.model()
peak = model.peakObject(index)
old = editor.blockSignals(True)
editor.setChecked(peak.isEnabled())
editor.blockSignals(old)
editor.setBoxColor(peak.color())
def __toggleEnabled(self, index):
model = index.model()
peak = model.peakObject(index)
newValue = not peak.isEnabled()
newValue = qt.Qt.Checked if newValue else qt.Qt.Unchecked
model.setData(index, newValue, role=qt.Qt.CheckStateRole)
def updateEditorGeometry(self, editor, option, index):
# Center the widget to the cell
size = editor.sizeHint()
half = size / 2
halfPoint = qt.QPoint(half.width(), half.height())
pos = option.rect.center() - halfPoint
editor.move(pos)
class _PeakToolItemDelegate(qt.QStyledItemDelegate):
def createEditor(self, parent, option, index):
if not index.isValid():
return super(_PeakToolItemDelegate, self).createEditor(parent, option, index)
editor = qt.QToolBar(parent=parent)
editor.setIconSize(qt.QSize(20, 20))
editor.setStyleSheet("QToolBar { border: 0px }")
persistantIndex = qt.QPersistentModelIndex(index)
extract = qt.QAction(editor)
extract.setToolTip("Re-extract peaks from this ring")
extract.setIcon(icons.getQIcon("pyfai:gui/icons/extract-ring"))
extract.triggered.connect(functools.partial(self.__extractPeak, persistantIndex))
editor.addAction(extract)
remove = qt.QAction(editor)
remove.setToolTip("Remove this group of peaks")
remove.setIcon(icons.getQIcon("pyfai:gui/icons/remove-peak"))
remove.triggered.connect(functools.partial(self.__removePeak, persistantIndex))
editor.addAction(remove)
editor.setMinimumSize(editor.sizeHint())
editor.setMaximumSize(editor.sizeHint())
editor.setSizePolicy(qt.QSizePolicy.Fixed, qt.QSizePolicy.Fixed)
return editor
def updateEditorGeometry(self, editor, option, index):
"""
Update the geometry of the editor according to the changes of the view.
:param qt.QWidget editor: Editor widget
:param qt.QStyleOptionViewItem option: Control how the editor is shown
:param qt.QIndex index: Index of the data to display
"""
editor.setGeometry(option.rect)
def getTask(self):
"""
:rtype: PeakPickingTask
"""
widget = self
while widget is not None:
if isinstance(widget, PeakPickingTask):
return widget
widget = widget.parent()
raise TypeError("PeakPickingTask not found")
def __extractPeak(self, persistantIndex, checked):
if not persistantIndex.isValid():
return
model = persistantIndex.model()
peak = model.peakObject(persistantIndex)
task = self.getTask()
if task is not None:
task.autoExtractSingleRing(peak)
def __removePeak(self, persistantIndex, checked):
if not persistantIndex.isValid():
return
model = persistantIndex.model()
model.removeRow(persistantIndex.row(), persistantIndex.parent())
class _RingSelectionBehaviour(qt.QObject):
"""Manage behaviour relative to ring selection.
This ensure coherence between many widgets.
- If "always new ring" activated
- The spinner is diabled
- The spinner have to display the number of the next ring created.
- The spinner value is decorelated from the hilighted ring
of the table view
- Else
- The spinner is enabled
- The value of the spinner have to be consistant with the hilighted
ring from the table view.
"""
def __init__(self, parent,
peakSelectionModel,
spinnerRing,
newRingOption,
ringSelectionModel,
plot):
qt.QObject.__init__(self, parent)
self.__peakSelectionModel = peakSelectionModel
self.__spinnerRing = spinnerRing
self.__newRingOption = newRingOption
self.__ringSelectionModel = ringSelectionModel
self.__peakSelectionModel.changed.connect(self.__peaksHaveChanged)
self.__peakSelectionModel.structureChanged.connect(self.__peaksStructureHaveChanged)
self.__spinnerRing.valueChanged.connect(self.__spinerRingChanged)
self.__newRingOption.toggled.connect(self.__newRingToggeled)
if self.__ringSelectionModel is not None:
self.__ringSelectionModel.selectionChanged.connect(self.__ringSelectionChanged)
self.__ringSelectionModel.selectionChanged.connect(self.__hightlightedRingChnaged)
self.__plot = plot
self.__initState()
def incRing(self):
"""Select the next ring. The auto selectection will be disabled."""
ringNumber = self.__spinnerRing.value()
ringNumber = ringNumber + 1
self.selectRing(ringNumber)
def decRing(self):
"""Select the next ring. The auto selection will be disabled."""
ringNumber = self.__spinnerRing.value()
ringNumber = ringNumber - 1
if ringNumber > 0:
self.selectRing(ringNumber)
def selectRing(self, ringNumber):
"""Select one of the rings.
The tools are updated to edit/create this ring.
:param int ringNumber: The ring number to select
"""
if self.__newRingOption.isChecked():
self.__newRingOption.trigger()
self.__spinnerRing.setValue(ringNumber)
def toggleNewRing(self):
"""Toggle the "new ring" modificator.
"""
self.__newRingOption.trigger()
def clear(self):
self.__peakSelectionModel.changed.disconnect(self.__peaksHaveChanged)
self.__peakSelectionModel.structureChanged.disconnect(self.__peaksStructureHaveChanged)
self.__spinnerRing.valueChanged.disconnect(self.__spinerRingChanged)
self.__newRingOption.toggled.disconnect(self.__newRingToggeled)
if self.__ringSelectionModel is not None:
self.__ringSelectionModel.selectionChanged.disconnect(self.__ringSelectionChanged)
self.__ringSelectionModel.selectionChanged.disconnect(self.__hightlightedRingChnaged)
def __initState(self):
self.__newRingToggeled()
def __peaksStructureHaveChanged(self):
self.__plot.setSelectedPeak(None)
def __peaksHaveChanged(self):
if self.__newRingOption.isChecked():
self.__updateNewRing()
if not self.__ringSelectionModel.hasSelection():
# Update the model selection if nothing was selected
# TODO: It would be good to remove the timer,
# but this event is generated before the update of the model
qt.QTimer.singleShot(0, self.__spinerRingChanged)
def __updateNewRing(self):
createNewRing = False
indexes = self.__ringSelectionModel.selectedIndexes()
if len(indexes) == 0:
createNewRing = True
peakSelectionModel = self.__peakSelectionModel
if createNewRing or self.__newRingOption.isChecked():
# reach bigger ring
ringNumbers = [p.ringNumber() for p in peakSelectionModel]
if ringNumbers == []:
lastRingNumber = 0
else:
lastRingNumber = max(ringNumbers)
ringNumber = lastRingNumber + 1
else:
assert(len(indexes))
index = indexes[0]
model = self.__ringSelectionModel.model()
index = model.index(index.row(), 0)
peak = model.peakObject(index)
ringNumber = peak.ringNumber()
self.__spinnerRing.valueChanged.disconnect(self.__spinerRingChanged)
self.__spinnerRing.setValue(ringNumber)
self.__spinnerRing.valueChanged.connect(self.__spinerRingChanged)
def ringNumber(self):
"""Returns the targetted ring.
:rtype: int
"""
return self.__spinnerRing.value()
def __hightlightedRingChnaged(self):
indexes = self.__ringSelectionModel.selectedIndexes()
model = self.__ringSelectionModel.model()
if len(indexes) == 0:
peak = None
else:
index = indexes[0]
peak = model.peakObject(index)
if peak is not None:
name = peak.name()
self.__plot.setSelectedPeak(name)
else:
self.__plot.setSelectedPeak(None)
def __ringSelectionChanged(self):
indexes = self.__ringSelectionModel.selectedIndexes()
model = self.__ringSelectionModel.model()
if len(indexes) == 0:
peak = None
else:
index = indexes[0]
peak = model.peakObject(index)
if not self.__newRingOption.isChecked():
# It have to be updated
if peak is not None:
self.__spinnerRing.valueChanged.disconnect(self.__spinerRingChanged)
try:
ringNumber = peak.ringNumber()
self.__spinnerRing.setValue(ringNumber)
finally:
self.__spinnerRing.valueChanged.connect(self.__spinerRingChanged)
def __spinerRingChanged(self):
"""Called when the spinner displaying the selected ring changes."""
ringNumber = self.__spinnerRing.value()
if self.__ringSelectionModel is None:
return
model = self.__ringSelectionModel.model()
self.__ringSelectionModel.selectionChanged.disconnect(self.__ringSelectionChanged)
try:
for i in range(model.rowCount()):
index = model.index(i, 0)
peak = model.peakObject(index)
if peak.ringNumber() == ringNumber:
break
else:
i = None
if i is not None:
index = i
indexStart = model.index(index, 0)
indexEnd = model.index(index, model.columnCount() - 1)
selection = qt.QItemSelection(indexStart, indexEnd)
self.__ringSelectionModel.select(selection, qt.QItemSelectionModel.ClearAndSelect)
else:
self.__ringSelectionModel.clear()
finally:
self.__ringSelectionModel.selectionChanged.connect(self.__ringSelectionChanged)
def __newRingToggeled(self):
"""Called when the new ring option is toggled."""
newRingActivated = self.__newRingOption.isChecked()
self.__spinnerRing.setEnabled(not newRingActivated)
if self.__newRingOption.isChecked():
self.__updateNewRing()
else:
self.__ringSelectionChanged()
class PeakPickingTask(AbstractCalibrationTask):
def _initGui(self):
qt.loadUi(pyFAI.utils.get_ui_file("calibration-peakpicking.ui"), self)
icon = silx.gui.icons.getQIcon("pyfai:gui/icons/task-identify-rings")
self.setWindowIcon(icon)
self.initNextStep()
# Insert the plot on the layout
holder = self._plotHolder
self.__extractionThread = None
self.__plot = _PeakPickingPlot(parent=holder)
self.__plot.setObjectName("plot-picking")
holderLayout = qt.QVBoxLayout(holder)
holderLayout.setContentsMargins(1, 1, 1, 1)
holderLayout.addWidget(self.__plot)
# Insert the peak view on the layout
holder = self._peakSelectionDummy.parent()
self.__peakSelectionView = _PeakSelectionTableView(holder)
holderLayout = holder.layout()
holderLayout.replaceWidget(self._peakSelectionDummy, self.__peakSelectionView)
self.__undoStack = qt.QUndoStack(self)
layout = qt.QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
self._ringToolBarHolder.setLayout(layout)
toolBar = self.__createMainRingToolBar()
layout.addWidget(toolBar)
toolBar = self.__createRingToolBar()
layout.addWidget(toolBar)
self.__createPlotToolBar(self.__plot)
statusBar = self.__createPlotStatusBar(self.__plot)
self.__plot.setStatusBar(statusBar)
self.__plot.sigPeakPicked.connect(self.__onPickPicked)
self.__plot.sigShapeErased.connect(self.__onShapeErased)
self.__plot.sigShapeBrushed.connect(self.__onShapeBrushed)
self.__plot.sigInteractiveModeChanged.connect(self.__onPlotModeChanged)
action = qt.QAction(self)
action.setText("Extract rings until")
action.setToolTip("Remove all the rings and extract it again")
action.setIcon(icons.getQIcon("pyfai:gui/icons/extract-rings-to"))
action.triggered.connect(self.__autoExtractRings)
selectAction = self._extract.addDefaultAction(action)
selectAction.triggered.connect(self.__updateOptionToExtractAgain)
action = qt.QAction(self)
action.setText("Extract already picked rings")
action.setToolTip("Duplicated rings will be removed")
action.setIcon(icons.getQIcon("pyfai:gui/icons/extract-current-rings"))
action.triggered.connect(self.__autoExtractExistingRings)
self._extract.addDefaultAction(action)
action = qt.QAction(self)
action.setText("Extract all reachable rings")
action.setToolTip("Remove all the rings and extract everything possible")
action.setIcon(icons.getQIcon("pyfai:gui/icons/extract-reachable-rings"))
action.triggered.connect(self.__autoExtractReachableRings)
self._extract.addDefaultAction(action)
action = qt.QAction(self)
action.setText("Extract more rings")
action.setToolTip("Extract new rings after the last picked one")
action.setIcon(icons.getQIcon("pyfai:gui/icons/extract-more-rings"))
action.triggered.connect(self.__autoExtractMoreRings)
selectAction = self._extract.addDefaultAction(action)
selectAction.triggered.connect(self.__updateOptionToExtractMoreRings)
self.__updateOptionToExtractMoreRings()
moreAction = action
action = qt.QAction(self)
action.setText("Merge rings and sort")
action.setToolTip("Merge the groups using the same ring number and sort them")
action.setIcon(icons.getQIcon("silx:gui/icons/draw-brush"))
action.triggered.connect(self.__cleanUpRings)
self._extract.addAction(action)
self._extract.setEnabled(False)
self._extract.setDefaultAction(moreAction)
validator = validators.DoubleValidator(self)
self._numberOfPeakPerDegree.lineEdit().setValidator(validator)
locale = qt.QLocale(qt.QLocale.C)
self._numberOfPeakPerDegree.setLocale(locale)
self.__synchronizeRawView = SynchronizeRawView()
self.__synchronizeRawView.registerTask(self)
self.__synchronizeRawView.registerPlot(self.__plot)
self.__ringSelection = None
self.__massif = None
self.__massifReconstructed = None
for i, key in enumerate(range(qt.Qt.Key_0, qt.Qt.Key_9 + 1)):
if i == 0:
i = 10
action = qt.QAction(self)
action.setText("Select ring %d" % i)
def selectRing(ringNumber):
self.__ringSelection.selectRing(ringNumber)
action.triggered.connect(functools.partial(selectRing, i))
action.setShortcut(qt.QKeySequence(key))
self.addAction(action)
action = qt.QAction(self)
action.setText("Select the next ring")
action.triggered.connect(lambda: self.__ringSelection.incRing())
action.setShortcut(qt.QKeySequence(qt.Qt.Key_Plus))
self.addAction(action)
action = qt.QAction(self)
action.setText("Select the previous ring")
action.triggered.connect(lambda: self.__ringSelection.decRing())
action.setShortcut(qt.QKeySequence(qt.Qt.Key_Minus))
self.addAction(action)
action = qt.QAction(self)
action.setText("Toggle new ring tool")
action.triggered.connect(lambda: self.__ringSelection.toggleNewRing())
action.setShortcut(qt.QKeySequence(qt.Qt.Key_Equal))
self.addAction(action)
def __onPlotModeChanged(self, owner):
# TODO: This condition should not be reached like that
if owner is not self.__plot:
# Here a default plot tool is triggered
# Set back the default tool
if (not self.__arcSelectionMode.isChecked() and
not self.__ringSelectionMode.isChecked() and
not self.__peakSelectionMode.isChecked()):
self.__arcSelectionMode.trigger()
def __createSavePeakDialog(self):
dialog = CalibrationContext.instance().createFileDialog(self)
dialog.setAcceptMode(qt.QFileDialog.AcceptSave)
dialog.setWindowTitle("Save selected peaks")
dialog.setModal(True)
builder = FilterBuilder.FilterBuilder()
builder.addFileFormat("Control point files", "npt")
dialog.setNameFilters(builder.getFilters())
return dialog
def __createLoadPeakDialog(self):
dialog = CalibrationContext.instance().createFileDialog(self)
dialog.setWindowTitle("Load peaks")
dialog.setModal(True)
builder = FilterBuilder.FilterBuilder()
builder.addFileFormat("Control point files", "npt")
dialog.setNameFilters(builder.getFilters())
return dialog
def __loadPeaksFromFile(self):
dialog = self.__createLoadPeakDialog()
result = dialog.exec_()
if not result:
return
filename = dialog.selectedFiles()[0]
if os.path.exists(filename):
try:
controlPoints = pyFAI.control_points.ControlPoints(filename)
oldState = self.__copyPeaks(self.__undoStack)
peakSelectionModel = self.model().peakSelectionModel()
model_transform.initPeaksFromControlPoints(peakSelectionModel, controlPoints)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, self.model().peakSelectionModel(), oldState, newState)
command.setText("load rings")
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
except Exception as e:
MessageBox.exception(self, "Error while loading peaks", e, _logger)
except KeyboardInterrupt:
raise
def __savePeaksAsFile(self):
dialog = self.__createSavePeakDialog()
result = dialog.exec_()
if not result:
return
filename = dialog.selectedFiles()[0]
nameFilter = dialog.selectedNameFilter()
isNptFilter = ".npt" in nameFilter
if isNptFilter and not filename.endswith(".npt"):
filename = filename + ".npt"
try:
controlPoints = model_transform.createControlPoints(self.model())
controlPoints.save(filename)
except Exception as e:
MessageBox.exception(self, "Error while saving peaks", e, _logger)
except KeyboardInterrupt:
raise
def __createMainRingToolBar(self):
toolBar = qt.QToolBar(self)
# Load peak selection as file
loadPeaksFromFile = qt.QAction(self)
icon = icons.getQIcon('document-open')
self.__icon = icon
loadPeaksFromFile.setIcon(icon)
loadPeaksFromFile.setText("Load peak selection from file")
loadPeaksFromFile.triggered.connect(self.__loadPeaksFromFile)
loadPeaksFromFile.setIconVisibleInMenu(True)
toolBar.addAction(loadPeaksFromFile)
# Save peak selection as file
savePeaksAsFile = qt.QAction(self)
icon = icons.getQIcon('document-save')
savePeaksAsFile.setIcon(icon)
savePeaksAsFile.setText("Save peak selection as file")
savePeaksAsFile.triggered.connect(self.__savePeaksAsFile)
savePeaksAsFile.setIconVisibleInMenu(True)
toolBar.addAction(savePeaksAsFile)
toolBar.addSeparator()
style = qt.QApplication.style()
def createIcon(identifiyers):
for i in identifiyers:
if isinstance(i, (str,)):
if qt.QIcon.hasThemeIcon(i):
return qt.QIcon.fromTheme(i)
elif isinstance(i, qt.QIcon):
return i
else:
return style.standardIcon(i)
return qt.QIcon()
action = self.__undoStack.createUndoAction(self, "Undo")
action.setShortcut(qt.QKeySequence.Undo)
icon = createIcon(["edit-undo", qt.QStyle.SP_ArrowBack])
action.setIcon(icon)
toolBar.addAction(action)
action = self.__undoStack.createRedoAction(self, "Redo")
action.setShortcut(qt.QKeySequence.Redo)
icon = createIcon(["edit-redo", qt.QStyle.SP_ArrowForward])
action.setIcon(icon)
toolBar.addAction(action)
return toolBar
def __createRingToolBar(self):
toolBar = qt.QToolBar(self)
action = qt.QAction(self)
action.setIcon(icons.getQIcon("pyfai:gui/icons/search-full-ring"))
action.setText("Ring")
action.setCheckable(True)
action.setToolTip("Extract peaks, beyond masked values")
toolBar.addAction(action)
self.__ringSelectionMode = action
action = qt.QAction(self)
action.setIcon(icons.getQIcon("pyfai:gui/icons/search-ring"))
action.setText("Arc")
action.setCheckable(True)
action.setToolTip("Extract contiguous peaks")
toolBar.addAction(action)
self.__arcSelectionMode = action
action = qt.QAction(self)
action.setIcon(icons.getQIcon("pyfai:gui/icons/search-peak"))
action.setText("Arc")
action.setCheckable(True)
action.setToolTip("Extract contiguous peaks")
toolBar.addAction(action)
self.__peakSelectionMode = action
toolBar.addSeparator()
action = qt.QAction(self)
action.setIcon(icons.getQIcon("silx:gui/icons/draw-brush"))
action.setText("Brush")
action.setCheckable(True)
action.setToolTip("Change the ring number to a set of already identified peaks")
toolBar.addAction(action)
self.__brushMode = action
action = qt.QAction(self)
action.setIcon(icons.getQIcon("silx:gui/icons/draw-rubber"))
action.setText("Rubber")
action.setCheckable(True)
action.setToolTip("Remove a set of already identified peaks")
toolBar.addAction(action)
self.__erasorMode = action
toolBar.addSeparator()
action = qt.QAction(self)
action.setIcon(icons.getQIcon("pyfai:gui/icons/new-ring"))
action.setText("+")
action.setCheckable(True)
action.setChecked(True)
action.setToolTip("Create always a new ring when a peak is picked")
toolBar.addAction(action)
self.__createNewRingOption = action
spiner = qt.QSpinBox(self)
spiner.setRange(1, 9999)
spiner.setToolTip("Ring to edit")
toolBar.addWidget(spiner)
self.__selectedRingNumber = spiner
mode = qt.QActionGroup(self)
mode.setExclusive(True)
mode.addAction(self.__ringSelectionMode)
mode.addAction(self.__arcSelectionMode)
mode.addAction(self.__peakSelectionMode)
mode.addAction(self.__erasorMode)
mode.addAction(self.__brushMode)
mode.triggered.connect(self.__requestChangeMode)
self.__arcSelectionMode.setChecked(True)
return toolBar
def __requestChangeMode(self, action):
if action is self.__erasorMode:
self.__plot.setPeakInteractiveMode(_PeakPickingPlot.ERASOR_MODE)
elif action is self.__brushMode:
self.__plot.setPeakInteractiveMode(_PeakPickingPlot.BRUSH_MODE)
elif (action is self.__ringSelectionMode or
action is self.__arcSelectionMode or
action is self.__peakSelectionMode):
self.__plot.setPeakInteractiveMode(_PeakPickingPlot.PEAK_SELECTION_MODE)
else:
assert(False)
def __createPlotToolBar(self, plot):
from silx.gui.plot import tools
toolBar = tools.InteractiveModeToolBar(parent=self, plot=plot)
plot.addToolBar(toolBar)
toolBar = tools.ImageToolBar(parent=self, plot=plot)
colormapDialog = CalibrationContext.instance().getColormapDialog()
toolBar.getColormapAction().setColorDialog(colormapDialog)
plot.addToolBar(toolBar)
def __createPlotStatusBar(self, plot):
converters = [
('X', lambda x, y: x),
('Y', lambda x, y: y),
('Value', self.__getImageValue)]
hbox = qt.QHBoxLayout()
hbox.setContentsMargins(0, 0, 0, 0)
info = PositionInfo(plot=plot, converters=converters)
info.setSnappingMode(True)
statusBar = qt.QStatusBar(plot)
statusBar.setSizeGripEnabled(False)
statusBar.addWidget(info)
return statusBar
def __invalidateMassif(self):
self.__massif = None
self.__massifReconstructed = None
def __widgetShow(self):
pass
def __createMassif(self, reconstruct=False):
qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
experimentSettings = self.model().experimentSettingsModel()
image = experimentSettings.image().value()
mask = experimentSettings.mask().value()
if image is None:
return None
massif = pyFAI.massif.Massif(image, mask)
massif.get_labeled_massif(reconstruct=reconstruct)
qt.QApplication.restoreOverrideCursor()
return massif
def __getMassif(self):
if self.__ringSelectionMode.isChecked():
if self.__massifReconstructed is None:
self.__massifReconstructed = self.__createMassif(reconstruct=True)
self.__massifReconstructed.log_info = False
return self.__massifReconstructed
elif self.__arcSelectionMode.isChecked() or self.__peakSelectionMode.isChecked():
if self.__massif is None:
self.__massif = self.__createMassif()
self.__massif.log_info = False
return self.__massif
else:
assert(False)
def __findPeaks(self, x, y):
"""
Returns peaks around the location x, y
"""
image = self.model().experimentSettingsModel().image().value()
massif = self.__getMassif()
if massif is None:
# Nothing to pick
return
points = massif.find_peaks([y, x], stdout=None)
if len(points) == 0:
# toleration
toleration = 3
# clamp min to avoid negative values
ymin = y - toleration
if ymin < 0:
ymin = 0
ymax = y + toleration + 1
xmin = x - toleration
if xmin < 0:
xmin = 0
xmax = x + toleration + 1
data = image[ymin:ymax, xmin:xmax]
coord = numpy.argmax(data)
coord = numpy.unravel_index(coord, data.shape)
y, x = ymin + coord[0], xmin + coord[1]
points = massif.find_peaks([y, x], stdout=None)
# filter peaks from the mask
mask = self.model().experimentSettingsModel().mask().value()
if mask is not None:
points = filter(lambda coord: mask[int(coord[0]), int(coord[1])] == 0, points)
points = list(points)
return points
def __findSinglePeak(self, x, y):
"""
Returns a single peak a location x, y
"""
points = self.__findPeaks(x, y)
if len(points) > 1:
points = points[0:1]
return points
def __onPickPicked(self, x, y):
if self.__peakSelectionMode.isChecked():
points = self.__findSinglePeak(x, y)
else:
points = self.__findPeaks(x, y)
if len(points) == 0:
return
peakSelectionModel = self.model().peakSelectionModel()
ringNumber = self.__ringSelection.ringNumber()
points = numpy.array(points)
peakModel = model_transform.createRing(points, peakSelectionModel, ringNumber=ringNumber)
oldState = self.__copyPeaks(self.__undoStack)
peak = peakSelectionModel.peakFromRingNumber(ringNumber)
if peak is None:
peakSelectionModel.append(peakModel)
else:
peak.mergeCoords(peakModel)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, peakSelectionModel, oldState, newState)
command.setText("add peak %s" % peakModel.name())
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __onShapeBrushed(self, shape):
"""
Callback when brushing peaks is requested.
:param Shape shape: A shape containing peaks to erase
"""
if shape.getType() == "rectangle":
points = shape.getPoints()
minCoord = points.min(axis=0)
maxCoord = points.max(axis=0)
def erasePeaksFromRect(x, y):
return not (minCoord[1] < x < maxCoord[1] and
minCoord[0] < y < maxCoord[0])
brushedPeaks = []
oldState = self.__copyPeaks(self.__undoStack)
peakSelectionModel = self.model().peakSelectionModel()
model_transform.filterControlPoints(erasePeaksFromRect,
peakSelectionModel,
removedPeaks=brushedPeaks)
if len(brushedPeaks) == 0:
_logger.debug("No peak to brush")
return
brushedPeaks = numpy.array(brushedPeaks)
ringNumber = self.__ringSelection.ringNumber()
peak = peakSelectionModel.peakFromRingNumber(ringNumber)
if peak is None:
peakModel = model_transform.createRing(brushedPeaks, peakSelectionModel, ringNumber=ringNumber)
peakSelectionModel.append(peakModel)
else:
peak.mergeCoords(brushedPeaks)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, peakSelectionModel, oldState, newState)
command.setText("erase peaks with rubber tool")
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
else:
assert(False)
def __onShapeErased(self, shape):
"""
Callback when erasing peaks is requested.
:param Shape shape: A shape containing peaks to erase
"""
if shape.getType() == "rectangle":
points = shape.getPoints()
minCoord = points.min(axis=0)
maxCoord = points.max(axis=0)
def erasePeaksFromRect(x, y):
return not (minCoord[1] < x < maxCoord[1] and
minCoord[0] < y < maxCoord[0])
removedPeaks = []
oldState = self.__copyPeaks(self.__undoStack)
peakSelectionModel = self.model().peakSelectionModel()
model_transform.filterControlPoints(erasePeaksFromRect,
peakSelectionModel,
removedPeaks=removedPeaks)
if len(removedPeaks) == 0:
_logger.debug("No peak to remove")
return
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, peakSelectionModel, oldState, newState)
command.setText("erase peaks with rubber tool")
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
else:
assert(False)
def __removePeak(self, peakModel):
oldState = self.__copyPeaks(self.__undoStack)
self.model().peakSelectionModel().remove(peakModel)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, self.model().peakSelectionModel(), oldState, newState)
command.setText("remove peak %s" % peakModel.name())
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __setRingEnable(self, peakModel, value):
oldState = self.__copyPeaks(self.__undoStack)
peakModel.setEnabled(value)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, self.model().peakSelectionModel(), oldState, newState)
action = "enable" if value else "disable"
command.setText("%s ring %s" % (action, peakModel.name()))
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __setRingNumber(self, peakModel, value):
oldState = self.__copyPeaks(self.__undoStack)
context = CalibrationContext.instance()
color = context.getMarkerColor(value - 1)
with peakModel.lockContext():
peakModel.setRingNumber(value)
peakModel.setColor(color)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, self.model().peakSelectionModel(), oldState, newState)
command.setText("update ring number of %s" % peakModel.name())
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __copyPeaks(self, parent):
selection = self.model().peakSelectionModel()
state = []
for peakModel in selection:
copy = peakModel.copy(parent)
state.append(copy)
return state
def _createRingExtractor(self, ring=None, existingRings=False, reachableRings=False, moreRings=None):
"""Create a ring extractor according to some params.
:param Union[int,None] ring: If set the extraction is only executed on
a single ring
:param bool existingRings: If true, the extractor is configured to only
extract existing rings
:param Union[int,None] moreRings: If defined, extract more rings that
was not yet extracted
:param bool reachableRings: If true, reach all reachable rings
"""
extractor = RingExtractorThread(self)
experimentSettings = self.model().experimentSettingsModel()
extractor.setExperimentSettings(experimentSettings, copy=False)
extractor.sigProcessLocationChanged.connect(self.__autoExtractLocationChanged)
# Constant dependant of the ui file
FROM_PEAKS = 0
FROM_FIT = 1
if reachableRings:
maxRings = None
ringNumbers = None
elif moreRings is not None:
maxRings = None
peaksModel = self.model().peakSelectionModel()
ringNumbers = [p.ringNumber() for p in peaksModel]
maxRing = max(ringNumbers)
ringNumbers = list(range(maxRing + 1, maxRing + 1 + moreRings))
elif existingRings:
maxRings = None
peaksModel = self.model().peakSelectionModel()
ringNumbers = [p.ringNumber() for p in peaksModel]
ringNumbers = set(ringNumbers)
ringNumbers = list(ringNumbers)
ringNumbers = sorted(ringNumbers)
elif ring is None:
maxRings = self._maxRingToExtract.value()
ringNumbers = None
else:
maxRings = self._maxRingToExtract.value()
ringNumbers = [ring.ringNumber()]
pointPerDegree = self._numberOfPeakPerDegree.value()
extractor.setMaxRings(maxRings)
extractor.setRingNumbers(ringNumbers)
extractor.setPointPerDegree(pointPerDegree)
geometrySourceIndex = self._geometrySource.currentIndex()
if geometrySourceIndex == FROM_PEAKS:
peaksModel = self.model().peakSelectionModel()
extractor.setPeaksModel(peaksModel)
elif geometrySourceIndex == FROM_FIT:
geometryModel = self.model().fittedGeometry()
extractor.setGeometryModel(geometryModel)
else:
assert(False)
return extractor
EXTRACT_ALL = "extract-all"
EXTRACT_SINGLE = "extract-single"
EXTRACT_EXISTING = "extract-existing"
EXTRACT_MORE = "extract-more"
def __autoExtractRings(self):
thread = self._createRingExtractor(ring=None)
thread.setUserData("ROLE", self.EXTRACT_ALL)
thread.setUserData("TEXT", "extract rings")
self.__startExtractThread(thread)
def __autoExtractReachableRings(self):
thread = self._createRingExtractor(reachableRings=True)
thread.setUserData("ROLE", self.EXTRACT_ALL)
thread.setUserData("TEXT", "extract reachable rings")
self.__startExtractThread(thread)
def __autoExtractExistingRings(self):
thread = self._createRingExtractor(existingRings=True)
thread.setUserData("ROLE", self.EXTRACT_EXISTING)
thread.setUserData("TEXT", "extract existing rings")
self.__startExtractThread(thread)
def autoExtractSingleRing(self, ring):
thread = self._createRingExtractor(ring=ring)
thread.setUserData("ROLE", self.EXTRACT_SINGLE)
thread.setUserData("TEXT", "extract ring %d" % ring.ringNumber())
thread.setUserData("RING", ring)
self.__startExtractThread(thread)
def __autoExtractMoreRings(self):
value = self._moreRingToExtract.value()
thread = self._createRingExtractor(moreRings=value)
thread.setUserData("ROLE", self.EXTRACT_MORE)
thread.setUserData("TEXT", "extract %s more rings" % value)
self.__startExtractThread(thread)
def __startExtractThread(self, thread):
if self.__extractionThread is not None:
_logger.error("A task to extract rings is already processing")
return
thread.started.connect(self.__extractionStarted)
thread.finished.connect(functools.partial(self.__extractionFinishedSafe, thread))
thread.finished.connect(thread.deleteLater)
thread.start()
self.__extractionThread = thread
def __autoExtractLocationChanged(self, mask):
self.__plot.setProcessingLocation(mask)
def __updateOptionToExtractAgain(self):
self._moreRingToExtractTitle.setVisible(False)
self._moreRingToExtract.setVisible(False)
self._maxRingToExtractTitle.setVisible(True)
self._maxRingToExtract.setVisible(True)
def __updateOptionToExtractMoreRings(self):
self._moreRingToExtractTitle.setVisible(True)
self._moreRingToExtract.setVisible(True)
self._maxRingToExtractTitle.setVisible(False)
self._maxRingToExtract.setVisible(False)
def __extractionStarted(self):
self.__plot.setProcessing()
qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
self._extract.setWaiting(True)
def __extractionFinishedSafe(self, thread):
"""
Compute the result of the processing
:param RingExtractorThread thread: A ring ring extractor processing
"""
errorMessage = None
if thread.isAborted():
errorMessage = thread.errorString()
else:
try:
self.__extractionFinished(thread)
except Exception as e:
_logger.debug("Backtrace", exc_info=True)
errorMessage = str(e)
self.__plot.setProcessingLocation(None)
self.__plot.unsetProcessing()
qt.QApplication.restoreOverrideCursor()
self._extract.setWaiting(False)
if errorMessage is not None:
qt.QMessageBox.critical(self, "Error", errorMessage)
self.__extractionThread = None
def __extractionFinished(self, thread):
"""
Compute the result of the processing
:param RingExtractorThread thread: A ring ring extractor processing
"""
newPeaks = thread.resultPeaks()
# update the gui
oldState = self.__copyPeaks(self.__undoStack)
peakSelectionModel = self.model().peakSelectionModel()
role = thread.userData("ROLE")
if role == self.EXTRACT_ALL:
# Remove everything and recreate everything
disabledRings = set([p.ringNumber() for p in peakSelectionModel if not p.isEnabled()])
peakSelectionModel.clear()
for ringNumber in sorted(newPeaks.keys()):
coords = newPeaks[ringNumber]
peakModel = model_transform.createRing(coords, peakSelectionModel, ringNumber=ringNumber)
if ringNumber in disabledRings:
peakModel.setEnabled(False)
peakSelectionModel.append(peakModel)
elif role == self.EXTRACT_EXISTING:
# Remove everything and recreate everything with the same name/color...
ringNumbers = sorted(newPeaks.keys())
disabledRings = set([p.ringNumber() for p in peakSelectionModel if not p.isEnabled()])
peaks = [peakSelectionModel.peakFromRingNumber(n) for n in ringNumbers]
peakSelectionModel.clear()
for prevousRing in peaks:
coords = newPeaks[prevousRing.ringNumber()]
ringNumber = prevousRing.ringNumber()
peakModel = model_transform.createRing(coords, peakSelectionModel, ringNumber=ringNumber)
peakModel.setName(prevousRing.name())
if prevousRing.ringNumber() in disabledRings:
peakModel.setEnabled(False)
peakSelectionModel.append(peakModel)
elif role == self.EXTRACT_MORE:
# Append the extracted rings to the current ones
for ringNumber in sorted(newPeaks.keys()):
coords = newPeaks[ringNumber]
peakModel = model_transform.createRing(coords, peakSelectionModel, ringNumber=ringNumber)
peakSelectionModel.append(peakModel)
elif role == self.EXTRACT_SINGLE:
# Update coord of a single ring
ringObject = thread.userData("RING")
empty = numpy.empty(shape=(0, 2))
coords = newPeaks.get(ringObject.ringNumber(), empty)
ringObject.setCoords(coords)
else:
assert(False)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, peakSelectionModel, oldState, newState)
text = thread.userData("TEXT")
command.setText(text)
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __cleanUpRings(self):
"""Clean up the picked rings"""
oldState = self.__copyPeaks(self.__undoStack)
peakSelectionModel = self.model().peakSelectionModel()
peaks = {}
for p in peakSelectionModel:
ringNumber = p.ringNumber()
if ringNumber in peaks:
peaks[ringNumber].append(p)
else:
peaks[ringNumber] = [p]
peakSelectionModel.clear()
for ringNumber in sorted(peaks.keys()):
peak = peaks[ringNumber][0]
for p in peaks[ringNumber][1:]:
peak.mergeCoords(p)
peakSelectionModel.append(peak)
newState = self.__copyPeaks(self.__undoStack)
command = _PeakSelectionUndoCommand(None, peakSelectionModel, oldState, newState)
command.setText("Clean up")
command.setRedoInhibited(True)
self.__undoStack.push(command)
command.setRedoInhibited(False)
def __getImageValue(self, x, y):
"""Get value of top most image at position (x, y).
:param float x: X position in plot coordinates
:param float y: Y position in plot coordinates
:return: The value at that point or 'n/a'
"""
value = 'n/a'
image = self.__plot.getImage("image")
if image is None:
return value
data = image.getData(copy=False)
ox, oy = image.getOrigin()
sx, sy = image.getScale()
row, col = (y - oy) / sy, (x - ox) / sx
if row >= 0 and col >= 0:
# Test positive before cast otherwise issue with int(-0.5) = 0
row, col = int(row), int(col)
if (row < data.shape[0] and col < data.shape[1]):
value = data[row, col]
return value
def _updateModel(self, model):
self.__synchronizeRawView.registerModel(model.rawPlotView())
settings = model.experimentSettingsModel()
settings.maskedImage().changed.connect(self.__imageUpdated)
settings.image().changed.connect(self.__invalidateMassif)
settings.mask().changed.connect(self.__invalidateMassif)
model.peakSelectionModel().changed.connect(self.__peakSelectionChanged)
self.__plot.setModel(model.peakSelectionModel())
self.__initPeakSelectionView(model)
self.__undoStack.clear()
self.__imageUpdated()
self.__peakSelectionChanged()
if self.__ringSelection is not None:
self.__ringSelection.clear()
self.__ringSelection = _RingSelectionBehaviour(self,
self.model().peakSelectionModel(),
self.__selectedRingNumber,
self.__createNewRingOption,
self.__peakSelectionView.selectionModel(),
self.__plot)
def __peakSelectionChanged(self):
peakCount = self.model().peakSelectionModel().peakCount()
if peakCount < 3:
self._extract.setEnabled(False)
self.setToolTip("Select manually more peaks to auto extract peaks")
else:
self._extract.setEnabled(True)
self.setToolTip("")
def __initPeakSelectionView(self, model):
tableModel = _PeakSelectionTableModel(self, model.peakSelectionModel())
tableModel.requestRingChange.connect(self.__setRingNumber)
tableModel.requestChangeEnable.connect(self.__setRingEnable)
tableModel.requestRemovePeak.connect(self.__removePeak)
self.__peakSelectionView.setModel(tableModel)
def __imageUpdated(self):
image = self.model().experimentSettingsModel().maskedImage().value()
if image is not None:
self.__plot.addImage(image, legend="image", selectable=True, copy=False, z=-1)
self.__plot.setGraphXLimits(0, image.shape[0])
self.__plot.setGraphYLimits(0, image.shape[1])
self.__plot.resetZoom()
else:
self.__plot.removeImage("image")
|