Skip to content
Snippets Groups Projects
GUI_main.py 102 KiB
Newer Older
  • Learn to ignore specific revisions
  • lpbsscientist's avatar
    lpbsscientist committed
    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 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    
    
    
        def DialogBoxECV(self, s):
            """This functions creates from the ExchangeCellValues.py file a
            window which takes two integer entries and then swaps the cells having
            the given integer values.
            """
    #        creates a dialog window from the ExchangeCellValues.py file
            dlg = ecv.CustomDialog(self)
    
    #        if the user presses 'ok', it executes the code
            if dlg.exec_():
    
    #            it tests if both value to be swapped are not empty.
                if dlg.entry1.text()!= '' and dlg.entry2.text() != '':
    
    #                reads out the values and converts it into integers.
                    value1 = int(dlg.entry1.text())
                    value2 = int(dlg.entry2.text())
    
    #                calls the function which does the swap
                    self.m.ExchangeCellValue(value1,value2)
    
    #                if the button to display the values of the cell is checked,
    #                the values are again displayed on the graph after the swap
    #                of cells.
                    if self.button_showval.isChecked():
                        self.m.ShowCellNumbersCurr()
                        self.m.ShowCellNumbersNext()
                        self.m.ShowCellNumbersPrev()
    
    
            else:
                return
    
    
    
        def SelectChannel(self, index):
            """This function is called when the button to select different channels
            is used. From the displayed list in the button, the chosen index
            corresponnds to the same index in the list of channels from the reader.
            So, it sets the default channel with the new index (called index below)
            """
    
    #        This function attributes the FOV chosen by the user corresponding to
    #        the index in the list of options.
            self.reader.default_channel = index
    #        update the pictures using the same function as the one used to
    #        change the fields of view.
            self.ChangeFOV()
    
    
    
    
        def SelectFov(self, index):
            """This function is called when the button containing the list of
            fields od view is used.
            The index correspondds to the field of view selected in the list.
    
            """
    
    #        This function attributes the FOV chosen by the user corresponding to
    #        the index in the list of options. First the mask is automatically
    #        saved.
            self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    #        the new index is set.
            self.FOVindex = index
    
    #        it updates the fov in the plot with the new index.
            self.ChangeFOV()
    
    #        the flag of the fluorescence extraction is set to False (such that
    #        if the user extracts fluorescence data in the new field of  view,
    #        there is a dialog box asking to select the corresponding xls file
    #        for this field of view. IF there is no data sheet for this fov, the
    #        user can enter a new name to make a new file.)
            self.FlagFluoExtraction = False
    
        def ChangeFOV(self):
    
    #        it changes the fov or channel according to the choice of the user
    #        and it updates the plot shown and it initializes the new fov/channel
    #        at t=0 by default.
    
    #        set the time index to 0
            self.Tindex = 0
    
    #        load the image and mask for the current plot
            self.m.currpicture = self.reader.LoadOneImage(self.Tindex,self.FOVindex)
            self.m.plotmask = self.reader.LoadMask(self.Tindex,self.FOVindex)
    
    #        sets the image and the mask to 0 for the previous plot
            self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
            self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    #        load the image and the mask for the next plot.
            self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1, self.FOVindex)
            self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
    
    #        once the images and masks are loaded into the variables, they are
    #        displaye in the gui.
            self.m.UpdateBckgrndPicture()
    
    
    #       enables the next frame button in case it was disabled when the
    #        fov/channel was changed
            self.button_nextframe.setEnabled(True)
    #        disables the previous frame button in case it was active before
    #        changing fov/channel.
            self.button_previousframe.setEnabled(False)
    
    #        updates the title of the plots to display the right time indices
    #        aboves the plots.
            self.UpdateTitleSubplots()
    
    #       if the button to show cell values is active, it shows the values again.
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    #       if the button to hide the mask was checked before changing fov/channel,
    #       it hides the mask again.
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
    #        the button to set the time index is also set to 0/default again.
            self.button_timeindex.setText('')
    #        enables the neural network buttons if there is already an
    #        existing prediction for the current image.
            self.EnableCNNButtons()
    
        def ChangeTimeFrame(self):
            """This funcion is called whenever the user gives an new time index,
            to jump to the new given index, onces "enter" button is pressed.
            """
    
    #        it reads out the text in the button and converts it to an int.
            newtimeindex = int(self.button_timeindex.text())
            if newtimeindex >= 0 and newtimeindex <= self.reader.sizet-1:
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                self.Tindex = newtimeindex
    
                if self.Tindex == 0:
                    self.button_nextframe.setEnabled(True)
                    self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1,self.FOVindex)
                    self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)
    
                    self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                    self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    
                    self.m.UpdateBckgrndPicture()
                    self.button_previousframe.setEnabled(False)
    
    
                elif self.Tindex == self.reader.sizet-1:
                    self.button_previousframe.setEnabled(True)
                    self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-1, self.FOVindex)
                    self.m.prevplotmask = self.reader.LoadMask(self.Tindex-1, self.FOVindex)
    
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)
    
    
                    self.m.nextpicture =  np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                    self.m.nextplotmask =  np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
                    self.m.UpdateBckgrndPicture()
                    self.button_nextframe.setEnabled(False)
    
    
                else:
    
                    self.button_nextframe.setEnabled(True)
                    self.button_previousframe.setEnabled(True)
                    self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-1, self.FOVindex)
                    self.m.prevplotmask = self.reader.LoadMask(self.Tindex-1, self.FOVindex)
    
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)
    
                    self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1,self.FOVindex)
                    self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
                    self.m.UpdateBckgrndPicture()
    
                self.UpdateTitleSubplots()
                self.button_timeindex.clearFocus()
    
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
    
                if self.button_hidemask.isChecked():
                    self.m.HideMask()
                self.EnableCNNButtons()
    
            else:
                self.button_timeindex.clearFocus()
                return
    
    #    def keyPressEvent(self, event):
    ##        print('keypressevengda211111')
    ##        print(event.key())
    #        if not self.button_nextframe.isChecked() and event.key() == Qt.Key_Right:
    #            self.button_nextframe.setChecked(True)
    #            self.ForwardTime()
    #            self.button_nextframe.setChecked(False)
    #        else:
    #            print('inside keypressevent')
    #            event.ignore()
    
        def CellCorrespActivation(self):
                self.Disable(self.button_cellcorespondance)
                self.statusBar.showMessage('Doing the cell correspondance')
    
                if self.Tindex != 0:
                    self.m.plotmask,_ = self.reader.CellCorrespondance(self.Tindex, self.FOVindex)
                    self.m.updatedata()
                else:
                    self.m.plotmask = self.reader.LoadSeg(self.Tindex, self.FOVindex)
                    self.m.updatedata()
    
                self.Enable(self.button_cellcorespondance)
                self.button_cellcorespondance.setChecked(False)
                self.statusBar.clearMessage()
    
        def SegmentBoxCheck(self):
    
            if self.button_segment.isChecked():
    
                self.Disable(self.button_segment)
                self.EnableCorrectionsButtons()
                self.m.SegmentedMask = self.reader.LoadSeg(self.Tindex, self.FOVindex)
                self.m.tempplotmask = self.m.plotmask.copy()
                self.m.plotmask = self.m.SegmentedMask.copy()
                self.m.currmask.set_data((self.m.SegmentedMask%10 + 1)*(self.m.SegmentedMask != 0))
                self.m.ax.draw_artist(self.m.currplot)
                self.m.ax.draw_artist(self.m.currmask)
                self.m.update()
                self.m.flush_events()
    #            update the graph
    
                self.button_SetSegmentation.setEnabled(True)
                self.button_savesegmask.setEnabled(True)
            else:
                self.m.SegmentedMask = self.m.plotmask.copy()
                self.m.plotmask = self.m.tempplotmask.copy()
                self.m.updatedata()
                self.button_SetSegmentation.setEnabled(False)
                self.button_savesegmask.setEnabled(False)
                self.Enable(self.button_segment)
    
        def SegmentThresholdedPredMask(self):
    
            segparamvalue = int(self.button_SetSegmentation.text())
            self.m.plotmask = self.reader.Segment(segparamvalue, self.Tindex,self.FOVindex)
            self.m.currmask.set_data((self.m.plotmask%10 + 1)*(self.m.plotmask != 0))
            self.m.ax.draw_artist(self.m.currplot)
            self.m.ax.draw_artist(self.m.currmask)
            self.m.update()
            self.m.flush_events()
    #        self.m.SegmentedMask = self.reader.Segment(segparamvalue, self.Tindex, self.FOVindex)
    #        update the plots to display the segmentation view
    
        def ButtonSaveSegMask(self):
            """saves the segmented mask
            """
            self.reader.SaveSegMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    
    
    
        def ThresholdBoxCheck(self):
            """if the buttons is checked it shows the thresholded version of the
            prediction, if it is not available it justs displays a null array.
            The buttons for the setting a threshold a value and to save it are then
            activated once this button is enabled.
            """
            if self.button_threshold.isChecked():
                self.Disable(self.button_threshold)
                self.m.ThresholdMask = self.reader.LoadThreshold(self.Tindex, self.FOVindex)
    
                self.m.currmask.set_data(self.m.ThresholdMask)
                self.m.ax.draw_artist(self.m.currplot)
                self.m.ax.draw_artist(self.m.currmask)
                self.m.update()
                self.m.flush_events()
    
    
    #            update the gra
    
                self.button_SetThreshold.setEnabled(True)
                self.button_savethresholdmask.setEnabled(True)
            else:
                self.m.updatedata()
    
                self.button_SetThreshold.setEnabled(False)
                self.button_savethresholdmask.setEnabled(False)
                self.Enable(self.button_threshold)
    
        def ThresholdPrediction(self):
    
            thresholdvalue = float(self.button_SetThreshold.text())
    
            self.m.ThresholdMask = self.reader.ThresholdPred(thresholdvalue, self.Tindex,self.FOVindex)
            self.m.currmask.set_data(self.m.ThresholdMask)
            self.m.ax.draw_artist(self.m.currplot)
            self.m.ax.draw_artist(self.m.currmask)
            self.m.update()
            self.m.flush_events()
    #        self.m.ThresholdMask = self.reader.ThresholdPred(thresholdvalue, self.Tindex, self.FOVindex)
    #        update the plots to display the thresholded view
    
        def ButtonSaveThresholdMask(self):
            """saves the thresholed mask
            """
    #        pass
            self.reader.SaveThresholdMask(self.Tindex, self.FOVindex, self.m.ThresholdMask)
    
    
        def ChangePreviousFrame(self):
    
             """This function is called when the previous frame buttons is pressed
             and it tests if the buttons is enabled and if so it calls the
             BackwardTime() function. It should avoid the let the user do multiple
             clicks and that the function is then called afterwards several times,
             once the frames and masks of the current time index have been loaded.
             """
    
             if self.button_previousframe.isEnabled():
                self.button_previousframe.setEnabled(False)
    
    #            self.button_nextframe.disconnect()
    #            self.button_nextframe.setShortcut('')
    
    #            self.button_nextframe.setEnabled(False)
    #            print(self.button_nextframe.isEnabled())
    #            self.button_nextframe.setChecked(False)
    #            self.Testbool = False
                self.BackwardTime()
    #            self.button_nextframe.setShortcut(Qt.Key_Right)
    #            self.button_nextframe.pressed.connect(self.Test)
    #            self.button_nextframe.setChecked(False)
                if self.Tindex >0:
                    self.button_previousframe.setEnabled(True)
    #            self.button_nextframe.pressed.connect(self.Test)
    
    #            self.button_nextframe.setChecked(False)
    
    #            self.Testbool = True
             else:
    #            print('jamais la dedans?')
                 return
    
    
        def ChangeNextFrame(self):
    
            """This function is called when the next frame buttons is pressed
            and it tests if the buttons is enabled and if so it calls the
            ForwardTime() function. It should avoid the let the user do multiple
            clicks and that the function is then called afterwards several times,
            once the frames and masks of the current time index have been loaded.
            """
    #        self.button_nextframe.setShortcutEnabled(False)
            if self.button_nextframe.isEnabled():
                self.button_nextframe.setEnabled(False)
    
    #            self.button_nextframe.disconnect()
    #            self.button_nextframe.setShortcut('')
    
    #            self.button_nextframe.setEnabled(False)
    #            print(self.button_nextframe.isEnabled())
    #            self.button_nextframe.setChecked(False)
    #            self.Testbool = False
                self.ForwardTime()
    #            self.button_nextframe.setShortcut(Qt.Key_Right)
    #            self.button_nextframe.pressed.connect(self.Test)
    #            self.button_nextframe.setChecked(False)
    
                self.button_nextframe.setEnabled(True)
    #            self.button_nextframe.pressed.connect(self.Test)
    
    #            self.button_nextframe.setChecked(False)
    
    #            self.Testbool = True
            else:
                return
    #            print('jamais la dedans?')
    #            if QKeyEvent.key() == Qt.Key_Right:
    #                QKeyEvent.ignore()
    
    
        def ForwardTime(self):
    
            """This function switches the frame in forward time index. And it tests
            several conditions if t == lastTimeIndex-1, because then the next frame
            button has to be disabled. It also tests if the show value of cells
            button and hidemask are active in order to hide/show the mask or to
            show the cell values.
            """
    #        print(self.Tindex)
    #        the t frame is defined as the currently shown frame on the display.
    #        If the button "Next time frame" is pressed, this function is called
            self.statusBar.showMessage('Loading the next frame...')
    #        self.button_nextframe.setEnabled(False)
    #        self.button_nextframe.disconnect()
            self.Disable(self.button_nextframe)
    
    
    
            if self.Tindex + 1 < self.reader.sizet - 1 :
                    self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                    self.m.prevpicture = self.m.currpicture.copy()
                    self.m.prevplotmask = self.m.plotmask.copy()
    
                    self.m.currpicture = self.m.nextpicture.copy()
                    self.m.plotmask = self.m.nextplotmask.copy()
    
    
                    self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+2, self.FOVindex)
                    self.m.nextplotmask = self.reader.LoadMask(self.Tindex+2, self.FOVindex)
                    self.m.UpdateBckgrndPicture()
    
                    if self.Tindex + 1 == 1:
                        self.button_previousframe.setEnabled(True)
    
    
    
    #
            else:
    
                    self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                    self.m.prevpicture = self.m.currpicture.copy()
                    self.m.prevplotmask = self.m.plotmask.copy()
                    self.m.currpicture = self.m.nextpicture.copy()
                    self.m.plotmask = self.m.nextplotmask.copy()
    
    
    
                    self.m.nextpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                    self.m.nextplotmask = np.zeros([self.reader.sizey,self.reader.sizex], dtype = np.uint16)
                    self.m.UpdateBckgrndPicture()
    
                    self.button_nextframe.setEnabled(False)
    
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    
    
    
    
            self.Tindex = self.Tindex+1
            self.UpdateTitleSubplots()
    
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
            self.Enable(self.button_nextframe)
    
    #        self.button_nextframe.setChecked(False)
    
            self.statusBar.clearMessage()
    #        if self.Tindex < self.reader.sizet - 1 :
    #            self.button_nextframe.setEnabled(True)
            self.button_timeindex.setText(str(self.Tindex))
    
    
        def BackwardTime(self):
    
            """This function switches the frame in backward time index. And it
            several conditions if t == 1, because then the button previous frame has to
            be disabled. It also tests if the show value of cells button and
            hidemask are active in order to hide/show the mask or to show the cell
            values.
            """
    #        print(self.Tindex)
    #        the t frame is defined as the currently shown frame on the display.
    #        If the button "Previous time frame" is pressed, this function is called
            self.statusBar.showMessage('Loading the previous frame...')
    #        self.button_previousframe.setEnabled(False)
    #        self.button_previousframe.disconnect()
            self.Disable(self.button_previousframe)
            if self.Tindex == 1:
    
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                self.m.nextpicture = self.m.currpicture.copy()
                self.m.nextplotmask = self.m.plotmask.copy()
    
                self.m.currpicture = self.m.prevpicture.copy()
                self.m.plotmask = self.m.prevplotmask.copy()
    
                self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    
                self.m.UpdateBckgrndPicture()
    
    
                self.button_previousframe.setEnabled(False)
    
    
    
            else:
    
    
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                self.m.nextpicture = self.m.currpicture.copy()
                self.m.nextplotmask = self.m.plotmask.copy()
    
                self.m.currpicture = self.m.prevpicture.copy()
                self.m.plotmask = self.m.prevplotmask.copy()
    
                self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-2, self.FOVindex)
                self.m.prevplotmask = self.reader.LoadMask(self.Tindex-2, self.FOVindex)
    
                self.m.UpdateBckgrndPicture()
                if self.Tindex-1 == self.reader.sizet-2:
                    self.button_nextframe.setEnabled(True)
    
    
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    
    
    
    #        self.button_previousframe.clicked.connect(self.BackwardTime)
    
            self.Tindex = self.Tindex-1
            self.UpdateTitleSubplots()
    
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
            self.Enable(self.button_previousframe)
    
    
            if self.Tindex > 0:
                self.button_previousframe.setEnabled(True)
    
    #        self.button_previousframe.setChecked(False)
            self.statusBar.clearMessage()
            self.button_timeindex.setText(str(self.Tindex))
    
        def MouseDraw(self):
    
            """
            This function is called whenever the brush or the eraser button is
            pressed. On the first press event it calls the self.m.OneClick, which
            tests whether it is a right click or a left click. If it a right click
            it assigns the value of the pixel which has been right clicked
            to self.cellval, meaning that the next drawn pixels will be set to this
            value.
            If it is left clicked, then it draws a 3x3 square with the current
            value of self.cellval.
            If after left clicking you drag the mouse, then you start drawing
            using the mouse and it stops once you release the left click.
    
            Same for the eraser button, it sets directly the value of self.cellval
            to 0.
            """
    
    
            if self.button_drawmouse.isChecked():
    
                self.statusBar.showMessage('Drawing using the brush, right click to set a value...')
    
                self.Disable(self.button_drawmouse)
                self.m.tempmask = self.m.plotmask.copy()
    
                self.id2 = self.m.mpl_connect('button_press_event', self.m.OneClick)
    
    
    
                self.id = self.m.mpl_connect('motion_notify_event', self.m.PaintBrush)
    
                self.id3 = self.m.mpl_connect('button_release_event', self.m.ReleaseClick)
    
    
                pixmap = QtGui.QPixmap('./icons/brush2.png')
                cursor = QtGui.QCursor(pixmap, 1,1)
                QApplication.setOverrideCursor(cursor)
    
            elif self.button_eraser.isChecked():
    
                self.statusBar.showMessage('Erasing by setting the values to 0...')
                self.Disable(self.button_eraser)
    
                self.m.tempmask = self.m.plotmask.copy()
    
                self.m.cellval = 0
                self.id2 = self.m.mpl_connect('button_press_event', self.m.OneClick)
                self.id = self.m.mpl_connect('motion_notify_event', self.m.PaintBrush)
    
                self.id3 = self.m.mpl_connect('button_release_event', self.m.ReleaseClick)
    
    
                pixmap = QtGui.QPixmap('./icons/eraser.png')
                cursor = QtGui.QCursor(pixmap, 1,1)
                QApplication.setOverrideCursor(cursor)
    
            else:
                self.m.mpl_disconnect(self.id3)
                self.m.mpl_disconnect(self.id2)
                self.m.mpl_disconnect(self.id)
                QApplication.restoreOverrideCursor()
                self.Enable(self.button_drawmouse)
                self.Enable(self.button_eraser)
    
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
                self.statusBar.clearMessage()
    
    
        def UpdateTitleSubplots(self):
            """This function updates the title of the plots according to the
            current time index. So it called whenever a frame or a fov is changed.
            """
            if self.Tindex == 0:
    
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('No frame {}'.format(''))
                self.m.titlenext.set_text('Next Time index {}'.format(self.Tindex+1))
    
    
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('No frame {}'.format(''))
    #            self.m.ax3.set_title('Next Time index {}'.format(self.Tindex+1))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
            elif self.Tindex == self.reader.sizet-1:
    
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('Previous time index {}'.format(self.Tindex-1))
                self.m.titlenext.set_text('No frame {}'.format(''))
    
    
    
    
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('Previous time index {}'.format(self.Tindex-1))
    #            self.m.ax3.set_title('No frame {}'.format(''))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
            else:
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('Previous time index {}'.format(self.Tindex-1))
                self.m.titlenext.set_text('Next Time index {}'.format(self.Tindex+1))
    
    
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('Previous time index {}'.format(self.Tindex-1))
    #            self.m.ax3.set_title('Next Time index {}'.format(self.Tindex+1))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
    
        def ClickNewCell(self):
            """
            this method is called when the button New Cell is clicked. If the button
            state corresponds to True (if is activated) then it connects the mouse
            clicks on the pyqt window to the canvas (so to the matplolib figure).
            The connection has an "id" which is given by the integer self.id
            After the connections is made, it calls the Disable function with argument 0
            which turns off the other button(s).
    
            If the button is clicked but it is deactivated then it disconnects the
            connection between the canvas and the window (the user can not interact
            with the plot anymore).
            Storemouseclicks is a list corresponding to the coordinates of all mouse
            clicks between the activation and the deactivation of the button.
            So if it is empty, it does not draw anything because no clicks
            were registered.
            But if it has some coordinates, it will draw a polygon where the vertices
            are the coordinates of all the mouseclicks.
            Once the figure has been updated with a new polygon, the other button(s)
            are again enabled.
    
            """
            if self.button_newcell.isChecked():
                self.statusBar.showMessage('Draw a new cell...')
                self.m.tempmask = self.m.plotmask.copy()
                self.id = self.m.mpl_connect('button_press_event', self.m.MouseClick)
                self.Disable(self.button_newcell)
    
    
    
            else:
    
                self.m.mpl_disconnect(self.id)
                if  self.m.storemouseclicks and self.TestSelectedPoints():
                    self.m.DrawRegion(True)
                else:
                    self.m.updatedata()
                self.Enable(self.button_newcell)
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
                self.statusBar.clearMessage()
    
    
        def TestSelectedPoints(self):
    
            """This function is just used to catch an exception, when the new cell
            or the add region function is called. If all the dots drawn by the user
            are located on one line (horizontal or vertical) the DrawRegion
            function calls a method to create a polygon and
            it can not make a polygon out of straight line so it gives an error.
            In order to prevent this error, this function avoids to attempt to draw
            by returning False if the square are all on one line.
            """
    
    
    
            allx = list(np.array(self.m.storemouseclicks)[:,0])
            ally = list(np.array(self.m.storemouseclicks)[:,1])
    
            resultx = all(elem == allx[0] for elem in allx)
            resulty = all(elem == ally[0] for elem in ally)
    
            if resultx or resulty:
                return False
            else:
                return True
    
    
    
    
        def clickmethod(self):
            """
            this method is called when the button Add region is clicked. If the button
            state corresponds to True (if is activated) then it connects the mouse
            clicks on the pyqt window to the canvas (so to the matplolib figure).
            The connection has an "id" which is given by the integer self.id
            After the connections is made, it calls the Disable function with argument 1
            which turns off the other button(s).
    
            If the button is clicked and it is deactivated then it disconnects the
            connection between the canvas and the window (the user can not interact
            with the plot anymore).
            Storemouseclicks is a list corresponding to the coordinates of all mouse
            clicks between the activation and the deactivation of the button.
            So if it is empty, it does not draw anything because no clicks
            were registered.
            But if it has some coordinates, it will draw a polygon where the vertices
            are the coordinates of all the mouseclicks.
            Once the figure has been updated with a new polygon, the other button(s)
            are again enabled.
    
            """
            if self.button_add_region.isChecked():
                self.statusBar.showMessage('Adding a region to an existing cell...')
                self.m.tempmask = self.m.plotmask.copy()
                self.id = self.m.mpl_connect('button_press_event', self.m.MouseClick)
                self.Disable(self.button_add_region)
    
            else:
    
                self.m.mpl_disconnect(self.id)
    
    #            test if the list is not empty and if the dots are not all in the same line
                if  self.m.storemouseclicks and self.TestSelectedPoints():
    
                    self.m.DrawRegion(False)
    
                else:
                    self.m.updatedata()
                self.Enable(self.button_add_region)
    
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
    
                self.statusBar.clearMessage()
    
    
    
    
    
        def Enable(self, button):
    
             """
             this functions turns on buttons all the buttons, depending on the time
             index. (next and previous buttons should not be turned on if t = 0
             or t = lasttimeindex)
             """
             if self.button_segment.isChecked():
                 self.EnableCorrectionsButtons()
                 self.button_home.setEnabled(True)
                 self.button_zoom.setEnabled(True)
                 self.button_pan.setEnabled(True)
                 self.button_back.setEnabled(True)
                 self.button_forward.setEnabled(True)
             else:
                 for k in range(0, len(self.buttonlist)):
                     if button != self.buttonlist[k]:
                         self.buttonlist[k].setEnabled(True)
    
             if self.Tindex == 0:
                 self.button_previousframe.setEnabled(False)
    
             if self.Tindex == self.reader.sizet-1:
                 self.button_nextframe.setEnabled(False)
    
             self.EnableCNNButtons()
    
    
        def Disable(self, button):
    
             """
             this functions turns off all the buttons except the one given in
             argument.
             """
             flag = False
             if button == self.button_add_region or button == self.button_newcell or button == self.button_exval or button == self.button_changecellvalue or button == self.button_drawmouse or button == self.button_eraser:
                 if self.button_segment.isChecked():
                     flag = True
    
    
             for k in range(0,len(self.buttonlist)):
                 if button != self.buttonlist[k]:
                     self.buttonlist[k].setEnabled(False)
             if flag:
                 self.button_segment.setEnabled(True)
    
             if button == self.button_segment or button == self.button_threshold:
                 self.button_home.setEnabled(True)
                 self.button_zoom.setEnabled(True)
                 self.button_pan.setEnabled(True)
                 self.button_back.setEnabled(True)
                 self.button_forward.setEnabled(True)
    
        def EnableCNNButtons(self):
    
            if self.reader.TestPredExisting(self.Tindex, self.FOVindex):
    #            self.button_cnn.setEnabled(False)
                self.button_threshold.setEnabled(True)
                self.button_segment.setEnabled(True)
                self.button_cellcorespondance.setEnabled(True)
                self.button_extractfluorescence.setEnabled(True)
            else:
    #            self.button_cnn.setEnabled(True)
                self.button_threshold.setEnabled(False)
                self.button_segment.setEnabled(False)
                self.button_cellcorespondance.setEnabled(False)
                self.button_extractfluorescence.setEnabled(False)
    
        def EnableCorrectionsButtons(self):
            self.button_newcell.setEnabled(True)
            self.button_add_region.setEnabled(True)
            self.button_drawmouse.setEnabled(True)
            self.button_eraser.setEnabled(True)
            self.button_exval.setEnabled(True)
            self.button_changecellvalue.setEnabled(True)
            self.button_showval.setEnabled(True)
    
        def DisableCorrectionsButtons(self):
            self.button_newcell.setEnabled(False)
            self.button_add_region.setEnabled(False)
            self.button_drawmouse.setEnabled(False)
            self.button_eraser.setEnabled(False)
            self.button_exval.setEnabled(False)
            self.button_changecellvalue.setEnabled(False)
            self.button_showval.setEnabled(False)
    
        def ButtonSaveMask(self):
            """
            When this function is called, it saves the current mask
            (self.m.plotmask)
            """
    
            self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    
    
    class PlotCanvas(FigureCanvas):
    
        def __init__(self, parent=None):
            """this class defines the canvas. It initializes a figure, which is then
            used to plot our data using imshow.
    
            """
    
    
    #       define three subplots corresponding to the previous, current and next
    #       time index.
            fig, (self.ax2, self.ax, self.ax3) = plt.subplots(1,3, sharex = True, sharey = True)
    
            FigureCanvas.__init__(self, fig)
            self.setParent(parent)
    
    #       this is some mambo jambo.
            FigureCanvas.setSizePolicy(self,
                    QSizePolicy.Expanding,
                    QSizePolicy.Expanding)
            FigureCanvas.updateGeometry(self)
    
    #       the self.currpicture attribute takes the original data and will then
    #       contain the updates drawn by the user.
    
            self.currpicture = parent.currentframe
    
            self.prevpicture = parent.previousframe
    
            self.nextpicture = parent.nextframe
    
            self.plotmask = parent.mask_curr
    
            self.prevplotmask = parent.mask_previous
    
            self.nextplotmask = parent.mask_next
    
            self.tempmask = self.plotmask.copy()
    
            self.tempplotmask = self.plotmask.copy()
    
            self.ThresholdMask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
            self.SegmentedMask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
    
    #        this line is just here to not attribute a zero value to the plot
    #        because if so, then it does not update the plot and it stays blank.
    #        (it is unclear why..if someone finds a better solution)
            self.prevpicture = self.currpicture.copy()
    
    
            self.currplot, self.currmask = self.plot(self.currpicture, self.plotmask, self.ax)
    
            self.previousplot, self.previousmask = self.plot(self.prevpicture, self.prevplotmask, self.ax2)
            self.prevpicture = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
            self.prevplotmask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype =np.uint16)
    
    #        print('set visible')
    #        self.ax2.set_visible(False)
    
            self.nextplot, self.nextmask = self.plot(self.nextpicture, self.nextplotmask, self.ax3)
    
            self.previousplot.set_data(self.prevpicture)
            self.previousmask.set_data((self.prevplotmask%10+1)*(self.prevplotmask != 0))
    
            self.ax2.draw_artist(self.previousplot)
            self.ax2.draw_artist(self.previousmask)
            self.update()
            self.flush_events()
    
    
            self.titlecurr = self.ax.set_title('Time index {}'.format(parent.Tindex))
            self.titleprev = self.ax2.set_title('No frame {}'.format(''))
            self.titlenext = self.ax3.set_title('Next Time index {}'.format(parent.Tindex+1))
    
    
    #        these variables are just set to test the states of the buttons
    #        (button turned on or  off, etc..) of the buttons in the methods
    #        used in this class.
            self.button_showval_check = parent.button_showval
            self.button_newcell_check = parent.button_newcell
            self.button_add_region_check = parent.button_add_region
            self.button_drawmouse_check = parent.button_drawmouse
            self.button_eraser_check = parent.button_eraser
            self.button_hidemask_check = parent.button_hidemask
    
    
    #       It will plot for the first time and return the imshow function
    
            self.currmask.set_clim(0, 10)
            self.previousmask.set_clim(0,10)
            self.nextmask.set_clim(0,10)
    
    #       This attribute is a list which stores all the clicks of the mouse.
            self.storemouseclicks = []
    
    #        This attribute is used to store the square where the mouse has been
    #        in order than to draw lines (Paintbrush function)
            self.storebrushclicks = [[False,False]]
    
    #        self.cellval is the variable which sets the value to the pixel
    #        whenever something is drawn.