-
Notifications
You must be signed in to change notification settings - Fork 0
/
SAFT.py
1633 lines (1269 loc) · 68 KB
/
SAFT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os.path
import platform
import copy
import itertools
#PySide2 imports
from PySide2 import QtCore, QtGui
from PySide2.QtCore import Slot
from PySide2 import __version__ as pyside_version
from PySide2.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QMessageBox, QFileDialog, QAction, QGroupBox, QHBoxLayout, QRadioButton, QDialog, QVBoxLayout, QCheckBox, QButtonGroup, QFrame
#package imports
import numpy as np
import pandas as pd
from scipy import __version__ as scipy_version
import scipy.signal as scsig
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
#SAFT imports
from clicker import clickAlgebra
from extractPeakResponses import extractPeaksDialog
from fitHistograms import histogramFitDialog
from processGroupedPeaks import groupPeakDialog
from quantal import fit_nGaussians, nGaussians_display
from baselines import savitzky_golay, baseline_als, baselineIterator
from dataStructures import Store, Dataset, Results, HistogramsR
from helpMessages import gettingStarted
import utils #addFileSuffix, findCurve, findScatter etc
#Import pg last to avoid namespace-overwrite problems?
import pyqtgraph as pg
class QHLine(QFrame):
### from https://stackoverflow.com/questions/5671354
def __init__(self):
super(QHLine, self).__init__()
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
class SAFTMainWindow(QMainWindow):
### Methods
###
### createMenu : make menubar
### about : About the app
### getStarted : a help file for novices
### createSplitTraceLayout : for display of split traces
### createPlotWidgets : build the plots
### createLinearRegion : linear selection for zoom window
### mouseMoved : when the mouse moves in zoom
### splitState : change the split state for the overview window
### manualPeakToggle :
###
###
###
def __init__(self, *args, **kwargs):
super(SAFTMainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Semi-Automatic Fluorescence Trace analysis")
self.central_widget = QWidget()
self.central_layout = QGridLayout()
self.central_widget.setLayout(self.central_layout)
self.setCentralWidget(self.central_widget)
self.resize(1500,800) # works well on MacBook Retina display
self.split_traces = False
self.LR_created = False # was a pg linear region created yet?
self.wasManualOnce = False # was manual editing of peaks ever engaged?
self.simplePeaks = False # choice of peak finding algorithm
self.autoPeaks = True # find peaks automatically or manually
self.cwt_width = 5 # width of the continuous wavelet transform peak finding
self.store = Store() # store for the datasets not being analysed
self.dataLoaded = False # was any data loaded yet?
self.pauseUpdates = False # should updates be paused whilst we make a lot of changes to GUI?
self.noCrosshair = True # is there any crosshair shown?
self.workingDataset = Dataset("Empty") # unnamed, empty dataset for traces, pk results and GUI settings
self.workingDataset.ROI_list = None
self.filename = None
self.conditions = [] # conditions will be sheet names from xlsx
self.datasetList_CBX = ['-'] # maintain our own list of datasets from the combobox
self.extPa = {} # external parameters for the peak scraping dialog
self.dataLock = True # when manual peak editing, lock to trace data
self.noPeaks = True # were any peaks found yet?
self.fitHistogramsOption = False # histograms are not fitted by default, checkbox -> False later
self.saveHistogramsOption = False # histograms are not saved by default, checkbox -> False later
# setup main window widgets and menus
self.createPlotWidgets()
self.createControlsWidgets()
self.createMenu()
self.toggleDataSource = False
def createMenu(self):
# Skeleton menu commands
self.file_menu = self.menuBar().addMenu("File")
self.analysis_menu = self.menuBar().addMenu("Analysis")
self.help_menu = self.menuBar().addMenu("Help")
self.file_menu.addAction("About FSTPA", self.about) #this actually goes straight into the FSTPA menu
self.file_menu.addAction("Open File", self.open_file)
self.file_menu.addAction("Save Peaks", self.save_peaks)
self.file_menu.addAction("Save baselined", self.save_baselined)
self.analysis_menu.addAction("Extract all peaks", self.extractAllPeaks)
self.analysis_menu.addAction("Grouped peak stats", self.getGroups)
self.analysis_menu.addAction("Quantal Histogram Fit", self.launchHistogramFit)
self.help_menu.addAction("Getting Started", self.getStarted)
def about(self):
QMessageBox.about (self, "About SAFT",
""" ----*- SAFT {0} -*----
\nSemi-Automatic Fluorescence Trace analysis
\nAndrew Plested FMP- and HU-Berlin 2020
\nThis application can analyse sets of fluorescence time series.
\nIt makes heavy use of PyQtGraph ({8}, Luke Campagnola).
\nPython {1}
\nPandas {2}, Numpy {3}, SciPy {4}
\nPySide2 {5} built on Qt {6}
\nRunning on {7}
""".format(__version__, platform.python_version(), pd.__version__, np.__version__, scipy_version, pyside_version, QtCore.__version__, platform.platform(), pg.__version__))
def modalWarning(self, s):
#print("click", s)
QMessageBox.warning(self, 'Warning', s)
#dlg.setWindowTitle("HELLO!")
#dlg.exec_()
def getStarted(self):
QMessageBox.information(self, "Getting Started", gettingStarted())
def createSplitTraceLayout(self):
"""Optional split view with each ROI trace in a separate plot (not the default)"""
# Store the plot items in a list - can't seem to get them easily otherwise?
data = []
self.p1stackMembers = []
for c in self.conditions:
memberName = c + " trace"
p1_stack_member = self.p1stack.addPlot(title=c, y=data, name=memberName)
p1_stack_member.hideAxis('bottom')
self.p1stackMembers.append(p1_stack_member)
self.p1stack.nextRow()
#print (c, len(self.p1stackMembers))
#link y-axes - using final member of stack as anchor
for s in self.p1stackMembers:
if s != p1_stack_member:
s.setXLink(p1_stack_member)
s.setYLink(p1_stack_member)
#add back bottom axis to the last graph
p1_stack_member.showAxis("bottom")
p1_stack_member.setLabel('bottom', "Time (s)")
def createPlotWidgets(self):
"""analysis plots"""
# traces plot
data = []
self.plots = pg.GraphicsLayoutWidget()
self.p1rc = (1,0)
self.p1 = self.plots.addPlot(y=data, row=self.p1rc[0], col=self.p1rc[1], rowspan=3, colspan=1)
self.p1.setTitle(title="Traces and background subtraction", color="F0F0F0", justify="right")
self.p1.setLabel('left', "dF / F")
self.p1.setLabel('bottom', "Time (s)")
self.p1.vb.setLimits(xMin=0)
#just a blank for now, populate after loading data to get the right number of split graphs
self.p1stack = pg.GraphicsLayout()
if self.dataLoaded:
createLinearRegion()
# Histograms
self.p2 = self.plots.addPlot(row=0, col=0, rowspan=1, colspan=1)
self.p2.setTitle("Peak Histograms", color="F0F0F0", justify="right")
self.p2.setLabel('left', "N")
self.p2.setLabel('bottom', "dF / F")
self.p2.vb.setLimits(xMin=0, yMin=0)
self.p2.addLegend(offset=(-50,50))
#self.p2.legend.setOffset()
# zoomed editing region , start in auto peak mode
self.p3 = self.plots.addPlot(y=data, row=0, col=1, rowspan=4, colspan=2)
self.p3.setTitle('Zoom - auto peak mode', color="F0F0F0", justify="right")
self.p3.setLabel('left', "dF / F")
self.p3.setLabel('bottom', "Time (s)")
self.p3.setFixedWidth(550)
self.p3vb = self.p3.vb
# draw the crosshair if we are in manual editing mode
self.p3proxyM = pg.SignalProxy(self.p3.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
# what does this do??
self.p3.scene().sigMouseClicked.connect(self.clickRelay)
#self.p3.sigMouseClicked.connect(self.clickRelay)
self.plots.cursorlabel = pg.LabelItem(text='', justify='right')
# to stop label jiggling about (graphicswidget method)
self.plots.cursorlabel.setFixedWidth(100)
self.plots.peakslabel = pg.LabelItem(text='', justify='left')
# to stop label jiggling about (graphicswidget method)
self.plots.peakslabel.setFixedWidth(100)
self.plots.addItem(self.plots.cursorlabel, row=4, col=2)
self.plots.addItem(self.plots.peakslabel, row=4, col=1)
self.central_layout.addWidget(self.plots, row=0, col=0, rowspan=1,colspan=2)
def clickRelay(self, *args):
"""Logic to avoid the click signal getting sent out if manual peak editing is not on."""
if self.autoPeaks:
print ("Turn on manual peak editing to get some value for your clicks.\nFor debugging: ", args)
self.modalWarning ("Turn on manual peak editing to get some value for your clicks.\nFor debugging: {}".format(args))
return
else:
# the asterisk in call unpacks the tuple into individual arguments.
# a free click should be locked to the data (as the crosshair is)
self.cA.onClick (*args, dataLocked=self.dataLock)
def createLinearRegion(self):
"""Linear region in p1 that defines the x-region in p3 (manual editing window)"""
# taken from pyqtgraph examples.
if self.LR_created == False:
self.LR_created = True
xrange = self.ranges['xmax']-self.ranges['xmin']
self.lr = pg.LinearRegionItem([xrange/2, xrange/1.5])
self.lr.setZValue(-10)
def updatePlot():
self.p3.setXRange(*self.lr.getRegion(), padding=0)
def updateRegion():
self.lr.setRegion(self.p3.getViewBox().viewRange()[0])
self.lr.sigRegionChanged.connect(updatePlot)
self.p3.sigXRangeChanged.connect(updateRegion)
if self.split_traces:
for s in self.p1stackMembers:
s.addItem(self.lr)
else:
self.p1.addItem(self.lr)
updatePlot()
def mouseMoved(self, evt):
"""Crosshair in p3 shown during manual fitting"""
if self.autoPeaks == False:
pos = evt[0] ## using signal proxy turns original arguments into a tuple
if self.p3.sceneBoundingRect().contains(pos):
mousePoint = self.p3vb.mapSceneToView(pos)
# there should be two plot data items, find the curve data
_c = utils.findCurve(self.p3.items)
sx, sy = _c.getData()
# quantize x to curve, and get corresponding y that is locked to curve
idx = np.abs(sx - mousePoint.x()).argmin()
ch_x = sx[idx]
ch_y = sy[idx]
self.hLine.setPos(ch_y)
self.vLine.setPos(ch_x)
# print ("update label: x={:.2f}, y={:.2f}".format(ch_x, ch_y))
self.plots.cursorlabel.setText("Cursor: x={: .2f}, y={: .3f}".format(ch_x, ch_y))
def splitState(self, b):
"""Called when trace display selection radio buttons are activated """
if b.text() == "Split traces":
if b.isChecked() == True:
self.split_traces = True
else:
self.split_traces = False
if b.text() == "Combined traces":
if b.isChecked() == True:
self.split_traces = False
else:
self.split_traces = True
tobeRemoved = self.plots.getItem(*self.p1rc)
print ("Removing", tobeRemoved)
#self.p1rc is a tuple containing the position (row-column) of p1
if self.split_traces:
self.plots.removeItem(tobeRemoved)
self.plots.addItem(self.p1stack, *self.p1rc, 3, 1)
else:
self.plots.removeItem(tobeRemoved)
self.plots.addItem(self.p1, *self.p1rc, 3, 1)
# call general update method
self.ROI_Change()
def toggleDataLogic (self, b):
if b.isChecked() == True:
self.showExtracted = True
# some hook to take traces and data from extracted etc
print ("data from extracted")
else:
self.showExtracted = False
# some hook to take traces and peaks data from searched
print ("raw data from search")
def saveHistogramsLogic (self, b):
if b.isChecked() == True:
self.saveHistogramsOption = True
else:
self.saveHistogramsOption = False
def fitHistogramsLogic (self, b):
if b.isChecked() == True:
self.fitHistogramsOption = True
else:
self.fitHistogramsOption = False
def manualPeakToggle (self, b=None):
"""Disable controls if we are editing peaks manually"""
#print ("MPT {}".format(b))
if self.manual.isChecked() == True:
# enter manual mode
print ("Manual peak editing")
self.autoPeaks = False
self.wasManualOnce = True
# disable all controls that could trigger auto peak finding
self.peak_CB.setDisabled(True)
self.SGsmoothing_CB.setDisabled(True)
self.cwt_SNR_Spin.setDisabled(True)
self.cwt_w_Spin.setDisabled(True)
self.auto_bs_lam_slider.setDisabled(True)
self.auto_bs_P_slider.setDisabled(True)
self.autobs_Box.setDisabled(True)
self.removeSml_Spin.setDisabled(True)
# Turn on crosshair and change mouse mode in p3.
if self.noCrosshair:
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.p3.addItem(self.vLine, ignoreBounds=True)
self.p3.addItem(self.hLine, ignoreBounds=True)
self.noCrosshair = False
# add a hint
self.p3.setTitle('Zoom - Manual editing L-click to add/remove peaks', color="F0F0F0", justify="right")
elif self.wasManualOnce:
# Enter auto peak mode
print ("Auto peak finding")
self.autoPeaks = True
# Re-enable all the controls for auto peak finding
self.peak_CB.setEnabled(True)
self.SGsmoothing_CB.setEnabled(True)
self.cwt_SNR_Spin.setEnabled(True)
self.cwt_w_Spin.setEnabled(True)
self.auto_bs_lam_slider.setEnabled(True)
self.auto_bs_P_slider.setEnabled(True)
self.autobs_Box.setEnabled(True)
self.removeSml_Spin.setEnabled(True)
# Change the hint
self.p3.setTitle('Zoom - Auto peak mode', color="F0F0F0", justify="right")
if self.noCrosshair == False:
# Remove crosshair from p3.
self.p3.removeItem(self.vLine)
self.p3.removeItem(self.hLine)
self.noCrosshair = True
else:
print ("DEBUG: Fell through manualPeakToggle without updating")
def createControlsWidgets(self):
"""control panel"""
controls = pg.LayoutWidget()
histograms = QGroupBox("Histogram options")
histGrid = QGridLayout()
NBin_label = QtGui.QLabel("No. of bins")
NBin_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.histo_NBin_Spin = pg.SpinBox(value=100, step=10, bounds=[0, 250], delay=0)
self.histo_NBin_Spin.setFixedSize(60, 25)
self.histo_NBin_Spin.valueChanged.connect(self.updateHistograms)
histMax_label = QtGui.QLabel("dF/F max")
histMax_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.histo_Max_Spin = pg.SpinBox(value=1, step=0.1, bounds=[0.1, 10], delay=0, int=False)
self.histo_Max_Spin.setFixedSize(60, 25)
self.histo_Max_Spin.valueChanged.connect(self.updateHistograms)
#toggle show ROI histogram sum
histsum_label = QtGui.QLabel("Show histograms")
histsum_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.sum_hist = pg.ComboBox()
self.sum_hist.setFixedSize(95,25)
self.sum_hist.addItems(['Separated','Summed'])
self.sum_hist.currentIndexChanged.connect(self.updateHistograms)
#toggle fitting
self.fitHistogramsToggle = QCheckBox("Fit Histograms", self)
self.fitHistogramsToggle.toggled.connect(lambda:self.fitHistogramsLogic(self.fitHistogramsToggle))
self.fitHistogramsToggle.setChecked(self.fitHistogramsOption)
#fit parameters
histnG_label = QtGui.QLabel("No. of Gaussians")
histnG_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.histo_nG_Spin = pg.SpinBox(value=5, step=1, bounds=[1,10], delay=0, int=True)
self.histo_nG_Spin.setFixedSize(60, 25)
self.histo_nG_Spin.valueChanged.connect(self.updateHistograms)
histq_label = QtGui.QLabel("dF ('q') guess")
histq_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.histo_q_Spin = pg.SpinBox(value=.05, step=0.01, bounds=[0.01,1], delay=0, int=False)
self.histo_q_Spin.setFixedSize(60, 25)
self.histo_q_Spin.valueChanged.connect(self.updateHistograms)
self.saveHistogramsToggle = QCheckBox("Save Histograms", self)
self.saveHistogramsToggle.setChecked(self.saveHistogramsOption)
self.saveHistogramsToggle.toggled.connect(lambda:self.saveHistogramsLogic(self.saveHistogramsToggle))
histGrid.addWidget(histsum_label, 0, 0, 1, 2)
histGrid.addWidget(self.sum_hist, 0, 2, 1, 3)
histGrid.addWidget(self.fitHistogramsToggle, 1, 5, 1, 2)
histGrid.addWidget(histnG_label, 2, 4, 1, 2)
histGrid.addWidget(self.histo_nG_Spin, 2, 6)
histGrid.addWidget(histq_label, 3, 4, 1, 2)
histGrid.addWidget(self.histo_q_Spin, 3, 6)
histGrid.addWidget(histMax_label, 1, 0)
histGrid.addWidget(self.histo_Max_Spin, 1, 1, 1, 2)
histGrid.addWidget(NBin_label, 2, 0)
histGrid.addWidget(self.histo_NBin_Spin, 2, 1, 1, 2)
histGrid.addWidget(self.saveHistogramsToggle, 3, 0, 1, 4)
histograms.setLayout(histGrid)
# Data display options panel
dataPanel = QGroupBox("Data display and processing")
dataGrid = QGridLayout()
self.split_B = QRadioButton("Split traces", self)
self.combine_B = QRadioButton("Combined traces", self)
self.combine_B.setChecked(True)
self.split_B.toggled.connect(lambda:self.splitState(self.split_B))
self.combine_B.toggled.connect(lambda:self.splitState(self.combine_B))
# load dataset button - for Benni
loadDataBtn = QtGui.QPushButton('Load traces')
loadDataBtn.clicked.connect(self.open_file)
# select working dataset
datasetLabel = QtGui.QLabel("Dataset")
datasetLabel.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.datasetCBx = pg.ComboBox()
self.datasetCBx.setFixedWidth(200)
self.datasetCBx.setItems(self.datasetList_CBX)
self.datasetCBx.currentIndexChanged.connect(self.datasetChange)
# selection of ROI trace, or mean, variance etc
ROIBox_label = QtGui.QLabel("Select ROI")
ROIBox_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.ROI_selectBox = QtGui.QComboBox()
self.ROI_selectBox.addItems(['None'])
self.ROI_selectBox.currentIndexChanged.connect(self.ROI_Change)
d_divider = QHLine()
d_divider.setFixedWidth(375)
# launch histogram fitting dialog
# should be inactive until extraction
self.fitHistDialogBtn = QtGui.QPushButton('Quantal Histogram Fit')
self.fitHistDialogBtn.clicked.connect(self.launchHistogramFit)
self.fitHistDialogBtn.setDisabled(True)
# launch peak extraction wizard dialog
extractPeaksBtn = QtGui.QPushButton('Extract peaks from all ROIs')
extractPeaksBtn.clicked.connect(self.extractAllPeaks)
# should be inactive until extraction
self.extractGroupsDialog_Btn = QtGui.QPushButton('Extract grouped responses')
self.extractGroupsDialog_Btn.clicked.connect(self.getGroups)
self.extractGroupsDialog_Btn.setDisabled(True)
reference_label = QtGui.QLabel("Reference condition for pattern extraction")
reference_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.refSelection = QtGui.QComboBox()
self.refSelection.setFixedSize(90, 25) # only width seems to work
self.refSelection.addItems(['-'])
self.refSelection.currentIndexChanged.connect(self.ROI_Change)
_buttonList = [self.fitHistDialogBtn, extractPeaksBtn, self.extractGroupsDialog_Btn]
bsize = (190, 40)
for b in _buttonList:
b.setMinimumSize(*bsize)
dataGrid.addWidget(loadDataBtn, 0, 0, 1, 2)
dataGrid.addWidget(datasetLabel, 0, 2, 1, 2)
dataGrid.addWidget(self.datasetCBx, 0, 4, 1, 4)
dataGrid.addWidget(ROIBox_label, 2, 2, 1, 2)
dataGrid.addWidget(self.ROI_selectBox, 2, 4, 1, 3)
dataGrid.addWidget(self.combine_B, 3, 0, 1, 4)
dataGrid.addWidget(self.split_B, 3, 4, 1, 4)
dataGrid.addWidget(d_divider, 4, 0, 1, -1)
dataGrid.addWidget(reference_label, 5, 0, 1, 6)
dataGrid.addWidget(self.refSelection, 5, 6, 1, 2)
dataGrid.addWidget(extractPeaksBtn, 6, 0, 1, 4)
dataGrid.addWidget(self.extractGroupsDialog_Btn, 6, 4, 1, 4)
dataGrid.addWidget(self.fitHistDialogBtn, 7, 0, 1, 4)
dataPanel.setLayout(dataGrid)
# Baseline controls box
baseline = QGroupBox("Automatic baseline cleanup")
base_grid = QGridLayout()
auto_bs_label = QtGui.QLabel("Baseline removal?")
auto_bs_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.autobs_Box = pg.ComboBox()
self.autobs_Box.addItems(['Auto', 'None', 'Lock'])
self.autobs_Box.setFixedSize(70, 25)
self.autobs_Box.currentIndexChanged.connect(self.ROI_Change)
# parameters for the auto baseline algorithm
auto_bs_lam_label = QtGui.QLabel("lambda")
auto_bs_lam_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.auto_bs_lam_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
self.auto_bs_lam_slider.setTickPosition(QtGui.QSlider.TicksBothSides)
self.auto_bs_lam_slider.setMinimum(2)
self.auto_bs_lam_slider.setMaximum(9)
self.auto_bs_lam_slider.setValue(6)
self.auto_bs_lam_slider.setFixedSize(100, 25)
self.auto_bs_lam_slider.valueChanged.connect(self.ROI_Change)
auto_bs_P_label = QtGui.QLabel("p")
auto_bs_P_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.auto_bs_P_slider = QtGui.QSlider(QtCore.Qt.Horizontal)
self.auto_bs_P_slider.setMinimum(0)
self.auto_bs_P_slider.setMaximum(20)
self.auto_bs_P_slider.setTickPosition(QtGui.QSlider.TicksBothSides)
self.auto_bs_P_slider.setValue(3)
self.auto_bs_P_slider.setFixedSize(100, 25)
self.auto_bs_P_slider.valueChanged.connect(self.ROI_Change)
# Savitsky-Golay smoothing is very aggressive and doesn't work well in this case
SGsmoothing_label = QtGui.QLabel("Savitzky-Golay smoothing")
SGsmoothing_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
SGsmoothing_label.setFixedWidth(160)
self.SGsmoothing_CB = pg.ComboBox()
self.SGsmoothing_CB.setFixedSize(70, 25)
self.SGsmoothing_CB.addItems(['Off','On'])
self.SGsmoothing_CB.currentIndexChanged.connect(self.ROI_Change)
SG_window_label = QtGui.QLabel("Window")
SG_window_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.SGWin_Spin = pg.SpinBox(value=15, step=2, bounds=[5, 49], delay=0, int=True)
self.SGWin_Spin.setFixedSize(60, 25)
self.SGWin_Spin.valueChanged.connect(self.ROI_Change)
# should be inactive until extraction
self.save_baselined_ROIs_Btn = QtGui.QPushButton('Save baselined ROI traces')
self.save_baselined_ROIs_Btn.clicked.connect(self.save_baselined)
self.save_baselined_ROIs_Btn.setDisabled(True)
b_divider = QHLine()
b_divider.setFixedWidth(375)
base_grid.addWidget(auto_bs_label, 0, 0, 1, 2)
base_grid.addWidget(self.autobs_Box, 0, 2, 1, 1)
base_grid.addWidget(self.save_baselined_ROIs_Btn, 1, 0, 1, 3)
base_grid.addWidget(auto_bs_P_label, 0, 3)
base_grid.addWidget(self.auto_bs_P_slider, 0, 4, 1, 2)
base_grid.addWidget(auto_bs_lam_label, 1, 3)
base_grid.addWidget(self.auto_bs_lam_slider, 1, 4, 1, 2)
base_grid.addWidget(b_divider, 2, 0, 1, -1)
base_grid.addWidget(SGsmoothing_label, 3, 0, 1, 3)
base_grid.addWidget(self.SGsmoothing_CB, 3, 3)
base_grid.addWidget(SG_window_label, 3, 4)
base_grid.addWidget(self.SGWin_Spin, 3, 5)
baseline.setLayout(base_grid)
# peak finding controls box
peakFinding = QGroupBox("Peak finding and editing")
pkF_grid = QGridLayout()
# Switch for manual peak finding
self.manual = QRadioButton("Edit peaks with mouse", self)
self.auto = QRadioButton("Auto find peaks", self)
self.man_auto_group = QButtonGroup()
self.man_auto_group.addButton(self.manual)
self.man_auto_group.addButton(self.auto)
if self.autoPeaks:
self.auto.setChecked(True)
self.man_auto_group.buttonClicked.connect(self.manualPeakToggle)
p_divider = QHLine()
p_divider.setFixedWidth(375)
p3_show_label = QtGui.QLabel("Show")
p3_show_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
p3_select_label = QtGui.QLabel("in zoom.")
self.p3Selection = QtGui.QComboBox()
self.p3Selection.setFixedSize(90, 25) # only width seems to work
self.p3Selection.addItems(['-'])
self.p3Selection.currentIndexChanged.connect(self.ROI_Change)
# Toggle between wavelet transform and simple algorithm for peak finding
peakFind_L_label = QtGui.QLabel("Auto-find peaks with")
peakFind_L_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
peakFind_R_label = QtGui.QLabel("algorithm.")
peakFind_R_label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
cwt_width_label = QtGui.QLabel("Width (wavelet only)")
cwt_width_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
SNR_label = QtGui.QLabel("Prominence / SNR")
SNR_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.peak_CB = pg.ComboBox()
self.peak_CB.setFixedSize(90, 25)
self.peak_CB.addItems(['wavelet','simple'])
self.peak_CB.currentIndexChanged.connect(self.ROI_Change)
# spin boxes for CWT algorithm parameters
self.cwt_SNR_Spin = pg.SpinBox(value=1.3, step=.1, bounds=[.1, 4], delay=0, int=False)
self.cwt_SNR_Spin.setFixedSize(70, 25)
self.cwt_SNR_Spin.valueChanged.connect(self.ROI_Change)
self.cwt_w_Spin = pg.SpinBox(value=6, step=1, bounds=[2, 20], delay=0, int=True)
self.cwt_w_Spin.setFixedSize(70, 25)
self.cwt_w_Spin.valueChanged.connect(self.ROI_Change)
# Control to exclude small peaks
removeSml_L_label = QtGui.QLabel("Ignore peaks < ")
removeSml_L_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
removeSml_R_label = QtGui.QLabel(" of largest.")
removeSml_R_label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.removeSml_Spin = pg.SpinBox(value=30, step=10, bounds=[0, 100], suffix='%', delay=0, int=False)
self.removeSml_Spin.setFixedSize(70, 25)
self.removeSml_Spin.valueChanged.connect(self.ROI_Change)
# should be inactive until peaks are extracted
self.savePSRBtn = QtGui.QPushButton('Save peak data')
self.savePSRBtn.clicked.connect(self.save_peaks)
self.savePSRBtn.setDisabled(True)
showDataBtn = QtGui.QPushButton('Show current peak data')
showDataBtn.clicked.connect(self.resultsPopUp)
pkF_grid.addWidget(p3_show_label, 0, 0)
pkF_grid.addWidget(p3_select_label, 0, 2, 1, 2)
pkF_grid.addWidget(self.p3Selection, 0 , 1)
pkF_grid.addWidget(p_divider, 1, 0, 1, -1)
pkF_grid.addWidget(self.manual, 2, 2, 1, 2)
pkF_grid.addWidget(self.auto, 2, 0, 1, 2)
pkF_grid.addWidget(peakFind_L_label, 3, 0, 1, 2)
pkF_grid.addWidget(self.peak_CB, 3, 2)
pkF_grid.addWidget(peakFind_R_label, 3, 3)
pkF_grid.addWidget(SNR_label, 4, 0, 1, 2)
pkF_grid.addWidget(self.cwt_SNR_Spin, 4, 2)
pkF_grid.addWidget(cwt_width_label, 5, 0, 1, 2)
pkF_grid.addWidget(self.cwt_w_Spin, 5, 2)
pkF_grid.addWidget(removeSml_L_label, 6, 0, 1, 2)
pkF_grid.addWidget(self.removeSml_Spin, 6, 2)
pkF_grid.addWidget(removeSml_R_label, 6, 3)
pkF_grid.addWidget(showDataBtn, 7, 0, 1, 2)
pkF_grid.addWidget(self.savePSRBtn, 7, 2, 1, 2)
pkF_grid.setSpacing(10)
#pkF_grid.setColumnStretch(0,3)
#pkF_grid.setColumnStretch(2,2)
peakFinding.setLayout(pkF_grid)
#stack widgets into control panel
dataPanel.setFixedHeight(225)
baseline.setFixedHeight(150)
peakFinding.setFixedHeight(225)
histograms.setFixedHeight(150)
controls.addWidget(dataPanel, 0, 0, 2, -1)
controls.addWidget(baseline, 2, 0 , 1, -1)
controls.addWidget(peakFinding, 3, 0 , 2, -1)
controls.addWidget(histograms, 5 , 0, 1, -1)
controls.setFixedWidth(420)
self.central_layout.addWidget(controls, 0, 3, -1, 1)
return
def updateDatasetComboBox(self, _name):
"""Return value indicates a duplicate name was found"""
#self.evasion = False
print ("self.datasetListCBX {}".format(self.datasetList_CBX))
if self.datasetList_CBX == ['-']:
# the list is empty so reset with the passed value
self.datasetCBx.setItems([_name])
self.datasetList_CBX = [_name]
return False
else:
# add new data set to combobox
if _name in self.datasetList_CBX:
# get random 3 letter string and add it
_s = utils.getRandomString(3)
_sname = _name + _s
self.datasetCBx.addItem(_sname)
self.datasetList_CBX.append(_sname)
#self.evasion = True
return _sname
else:
self.datasetCBx.addItem(_name)
self.datasetList_CBX.append(_name)
return False
def resultsPopUp(self):
"""Make a pop up window of the current peak results"""
_ROI = self.ROI_selectBox.currentText()
_r = self.workingDataset.resultsDF.df[_ROI]
#print (_r, type(_r))
qmb = QDialog()
qmb.setWindowTitle('Peaks from {}'.format(_ROI))
qmb.setGeometry(1000,600,600,800)
self.peaksText = QtGui.QTextEdit()
font = QtGui.QFont()
font.setFamily('Courier')
font.setFixedPitch(True)
font.setPointSize(12)
self.peaksText.setCurrentFont(font)
self.peaksText.setText(_r.to_string())
self.peaksText.setReadOnly(True)
#add buttons, make it the right size
qmb.layout = QVBoxLayout()
qmb.layout.addWidget(self.peaksText)
qmb.setLayout(qmb.layout)
qmb.exec_()
def histogram_parameters(self):
_nbins = int(self.histo_NBin_Spin.value())
_max = self.histo_Max_Spin.value()
return _nbins, _max
def doHistograms(self):
"""called for histogram output"""
_nbins, _max = self.histogram_parameters()
_condList = self.conditions + ["Sum"]
# create a dataframe to put the results in
self.hDF = HistogramsR(self.workingDataset.ROI_list, _condList, _nbins, 0., _max)
maxVal = len (self.workingDataset.ROI_list) * len (_condList)
progMsg = "Histograms for {0} traces".format(maxVal)
with pg.ProgressDialog(progMsg, 0, maxVal) as dlg:
#from the allowlist, should be from edited internal data?
for _condi, _pdf in self.gpd.pk_extracted_by_condi.items():
print (_condi, _pdf.columns)
for _ROI in _pdf.columns:
dlg += 1
# calculate individual histograms and add to dataframe
hy, hx = np.histogram(_pdf[_ROI], bins=_nbins, range=(0., _max))
self.hDF.addHist(_ROI, _condi, hy)
# add sum columns
self.hDF.ROI_sum()
def updateHistograms(self):
"""called when histogram controls are changed"""
# get controls values and summarise to terminal
_nbins, _max = self.histogram_parameters()
_ROI = self.ROI_selectBox.currentText()
_hsum = self.sum_hist.currentText()
print ('Update {3} Histogram(s) for {2} with Nbins = {0} and maximum dF/F = {1}.'.format(_nbins, _max, _ROI, _hsum))
# clear
self.p2.clear()
if _hsum == "Separated":
for i, _condi in enumerate(self.conditions):
# colours
col_series = (i, len(self.conditions))
# get relevant peaks data for displayed histograms
_, _pdata = self.workingDataset.resultsDF.getPeaks(_ROI, _condi)
# redo histogram
hy, hx = np.histogram(_pdata, bins=_nbins, range=(0., _max))
# replot
self.p2.plot(hx, hy, name="Histogram "+_condi, stepMode=True, fillLevel=0, pen=col_series, brush=col_series) ###fillOutline=True,
elif _hsum == "Summed":
sumhy = np.zeros(_nbins)
for _condi in self.conditions:
_, _pdata = self.workingDataset.resultsDF.getPeaks(_ROI, _condi)
hy, hx = np.histogram(_pdata, bins=_nbins, range=(0., _max))
sumhy += hy
self.p2.plot(hx, sumhy, name="Summed histogram "+_ROI, stepMode=True, fillLevel=0, fillOutline=True, brush='y')
if self.fitHistogramsOption:
#print ("len hx {}, hy {}".format(len(hx), len(hy)))
_num = self.histo_nG_Spin.value()
_q = self.histo_q_Spin.value()
_ws = self.histo_Max_Spin.value() / 20
_hxc = np.mean(np.vstack([hx[0:-1], hx[1:]]), axis=0)
_opti = fit_nGaussians(_num, _q, _ws, sumhy, _hxc)
if _opti.success:
_hx_u, _hy_u = nGaussians_display (_hxc, _num, _opti.x)
_qfit = _opti.x[0]
_c = self.p2.plot(_hx_u, _hy_u, name='Fit of {} Gaussians q: {:.2f}'.format(_num,_qfit))
#from pyqtgraph.examples
_c.setPen('w', width=3)
_c.setShadowPen(pg.mkPen((70,70,30), width=8, cosmetic=True))
else:
print ("fit failed")
def datasetChange(self):
print ("a (dataset) change is coming")
if self.datasetCBx.currentText() != self.workingDataset.DSname:
# prep current data for store
# store GUI settings?
self.store.storeSet(copy.copy(self.workingDataset))
print ('Stored {}'.format(self.workingDataset.DSname))
self.workingDataset = self.store.retrieveWorkingSet(self.datasetCBx.currentText())
print ('Retrieved {}'.format(self.workingDataset.DSname))
# print GUI control dict
print ("swdsGC {}".format(self.workingDataset.GUIcontrols))
# do this first otherwise on ROI extracted peaks are overwritten
# execute GUI controls specified in the retrieved Dataset
for k,v in self.workingDataset.GUIcontrols.items():
if k == "autoPeaks":
self.autoPeaks_GUI_switch(v)
elif k == "print":
print (v)
# update the ROI list combobox (should have a list)
# needed for updating GUI
self.updateROI_list_Box()
self.ROI_Change()
def autoPeaks_GUI_switch(self, v):
if v == "Disable":
print ("autoPeaks_GUI_switch : Disable")
self.pauseUpdates = True
self.autobs_Box.setValue('None')
self.auto_bs = False
self.manual.setChecked(True)
self.manual.setDisabled(True)
self.auto.setDisabled(True)
self.autoPeaks = False
self.pauseUpdates = False
self.manualPeakToggle()
elif v == "Enable":
print ("autoPeaks_GUI_switch : Enable")
#self.autobs_Box.setValue('Auto')
#self.auto_bs = True
#self.manual.setChecked(False)
self.pauseUpdates = True
self.manual.setEnabled(True)
self.auto.setEnabled(True)
#self.autoPeaks = True
self.pauseUpdates = False
self.manualPeakToggle()
def getGroups(self):
"""launch group processing dialog"""
print ('Process grouped peaks from all ROIs.')
self.getgroupsDialog = groupPeakDialog()
_dataset = copy.copy(self.workingDataset)
ddf = utils.decomposeRDF(_dataset.resultsDF.df)
self.getgroupsDialog.addData(ddf, name=_dataset.DSname)
accepted = self.getgroupsDialog.exec_()
def launchHistogramFit(self):
"""Wrapping function to launch histogram fit dialog"""
print ('Dialog to obtain quantal parameters from histogram fits.')
self.hfd = histogramFitDialog()
#send current peak data
_dataset = copy.copy(self.workingDataset)
ddf = utils.decomposeRDF(_dataset.resultsDF.df)
self.hfd.addData(ddf, _dataset.DSname, _dataset.getSD(maskWidth=10))
accepted = self.hfd.exec_()
def extractAllPeaks(self):
"""Wrapping function to get peak data from the dialog"""
print ('Dialog for getting peaks from all ROIs according to reference pattern.')
# if the QDialog object is instantiated in __init__, it persists in state....
# do it here to get a fresh one each time.
self.gpd = extractPeaksDialog()
# pass the data into the get peaks dialog object
# we do not want the original trace data modified
_dataset = copy.copy(self.workingDataset)
# automatically reduce baseline (could also do this interactively??)
# baselineIterator includes a progress indicator.
# can be Auto or Lock (meaning GUI controls are not updating algorithm)
if self.autobs_Box.value() != 'None':
if self.autobs_Box.value() == 'Auto':
# populate values for automatic baseline removal from GUI (unless 'Lock')
self.setBaselineParams()
_dataset.traces = baselineIterator(_dataset.traces, self.auto_bs_lam, self.auto_bs_P)
#get the times of the peaks from the "best" trace, that were selected auto or manually
_peak_t, _ = self.workingDataset.resultsDF.getPeaks('Mean', self.refSelection.currentText()) # pd.series
#print ("srsv: {}".format(self.refSelection.currentText()))
_sorted_peak_t = _peak_t.sort_values(ascending=True) # list is not sorted until now
_sorted_peak_t.dropna(inplace=True) # if there are 'empty' NaN, remove them
self.workingDataset.resultsDF.peakTimes = _sorted_peak_t # the definitive list of peak times, woud be degraded by editing but will only be used for masking traces later.
print ("swdrdfpt : {}".format(self.workingDataset.resultsDF.peakTimes))
self.extPa["tPeaks"] = _sorted_peak_t
# pass in "external parameters" for the peak extraction via extPa
self.gpd.setExternalParameters(self.extPa)
#reordered because peaks must be put first.
self.gpd.addDataset(_dataset)
# returns 1 (works like True) when accept() or 0 (we take for False) otherwise.
# data from dialog is stored in the attributes of self.gpd
accepted = self.gpd.exec_()
if accepted:
self.noPeaks = False