1+ import pyqtgraph as pg
2+ from pathlib import Path
3+ from pyqtgraph .Qt import QtCore , QtGui , QtWidgets
4+ try :
5+ from Lifetime_analysis .read_ph_phd import read_picoharp_phd , get_x_y
6+ except Exception as e :
7+ print (e )
8+ import matplotlib .pyplot as plt
9+
10+ """Recylce params for plotting"""
11+ plt .rc ('xtick' , labelsize = 20 )
12+ plt .rc ('xtick.major' , pad = 3 )
13+ plt .rc ('ytick' , labelsize = 20 )
14+ plt .rc ('lines' , lw = 2.5 , markersize = 7.5 )
15+ plt .rc ('legend' , fontsize = 20 )
16+ plt .rc ('axes' , linewidth = 3.5 )
17+
18+ pg .mkQApp ()
19+
20+ base_path = Path (__file__ ).parent
21+ file_path = (base_path / "Multi_Trace_Exporter.ui" ).resolve ()
22+
23+ uiFile = file_path
24+
25+ WindowTemplate , TemplateBaseClass = pg .Qt .loadUiType (uiFile )
26+
27+ class MainWindow (TemplateBaseClass ):
28+
29+ def __init__ (self ):
30+ super (TemplateBaseClass , self ).__init__ ()
31+
32+ # Create the main window
33+ self .ui = WindowTemplate ()
34+ self .ui .setupUi (self )
35+
36+ self .temp_layout = pg .GraphicsLayoutWidget ()
37+
38+ # file system tree
39+ self .fs_model = QtWidgets .QFileSystemModel ()
40+ self .fs_model .setRootPath (QtCore .QDir .currentPath ())
41+ self .ui .treeView .setModel (self .fs_model )
42+ self .ui .treeView .setIconSize (QtCore .QSize (25 ,25 ))
43+ self .ui .treeView .setSortingEnabled (True )
44+
45+ self .tree_selectionModel = self .ui .treeView .selectionModel ()
46+ self .tree_selectionModel .selectionChanged .connect (self .on_treeview_selection_change )
47+
48+ self .ui .comboBox .currentIndexChanged .connect (self .add_trace_to_temp_plot )
49+ self .ui .add_pushButton .clicked .connect (self .add_trace_to_mem )
50+ self .ui .export_pushButton .clicked .connect (self .pub_ready_plot_export )
51+
52+ self .x_i = []
53+ self .y_i = []
54+ self .x_mem = []
55+ self .y_mem = []
56+ self .legend = []
57+
58+ self .show ()
59+
60+ def on_treeview_selection_change (self ):
61+ try :
62+ fname = self .fs_model .filePath (self .tree_selectionModel .currentIndex ())
63+ _ , ext = fname .rsplit ('.' ,1 )
64+
65+ self .ui .comboBox .clear ()
66+ self .ui .textBrowser .clear ()
67+ self .x_i = []
68+ self .y_i = []
69+
70+ if ext in ['phd' ]:
71+ self .parser = read_picoharp_phd (fname )
72+ curve_list = []
73+
74+ for i in range (self .parser .no_of_curves ()):
75+ curve_list .append ("Curve " + str (i ))
76+ x , y = get_x_y (i , self .parser , smooth_trace = self .ui .smooth_checkBox .isChecked (), boxwidth = self .ui .smooth_spinBox .value ())
77+ self .x_i .append (x )
78+ self .y_i .append (y )
79+
80+ self .ui .comboBox .addItems (curve_list )
81+ self .ui .textBrowser .setText (str (self .parser .info ()))
82+
83+ else :
84+ self .ui .textBrowser .setText (str ("Select a PicoHarp File" ))
85+ except Exception as e :
86+ print (e )
87+
88+ def add_trace_to_temp_plot (self ):
89+ try :
90+ #self.temp_layout = pg.GraphicsLayoutWidget()
91+ self .temp_layout .clear ()
92+ self .temp_plot = self .temp_layout .addPlot (title = "Current Selection" )
93+ self .temp_plot .plot (self .x_i [self .ui .comboBox .currentIndex ()], self .y_i [self .ui .comboBox .currentIndex ()], pen = 'r' )
94+ self .temp_plot .setLogMode (False , True )
95+ self .temp_layout .show ()
96+ except Exception as e :
97+ print (e )
98+
99+ def add_trace_to_mem (self ):
100+ try :
101+ self .x_mem .append (self .x_i [self .ui .comboBox .currentIndex ()])
102+ self .y_mem .append (self .y_i [self .ui .comboBox .currentIndex ()])
103+ self .legend .append (self .ui .lineEdit .text ())
104+ except Exception as e :
105+ print (e )
106+
107+ def pub_ready_plot_export (self ):
108+ try :
109+ filename = QtWidgets .QFileDialog .getSaveFileName (self ,caption = "Filename with EXTENSION" )
110+
111+ plt .figure (figsize = (8 ,6 ))
112+ plt .tick_params (direction = 'out' , length = 8 , width = 3.5 )
113+ for i in range (len (self .x_mem )):
114+ if self .ui .Normalize_checkBox .isChecked ():
115+ plt .plot (self .x_mem [i ], self .y_mem [i ]/ max (self .y_mem [i ]), label = str (self .legend [i ]))
116+ else :
117+ plt .plot (self .x_mem [i ], self .y_mem [i ], label = str (self .legend [i ]))
118+
119+ plt .yscale ('log' )
120+ plt .xlabel ("Time (ns)" , fontsize = 20 , fontweight = 'bold' )
121+ plt .ylabel ("Intensity (norm.)" , fontsize = 20 , fontweight = 'bold' )
122+ plt .legend ()
123+ plt .tight_layout ()
124+
125+ plt .savefig (filename [0 ],bbox_inches = 'tight' , dpi = 300 )
126+ plt .close ()
127+
128+ self .clear_memory ()
129+
130+ except Exception as e :
131+ print (e )
132+ pass
133+
134+ def clear_memory (self ):
135+ self .x_mem = []
136+ self .y_mem = []
137+ self .legend = []
138+
139+
140+
141+
142+ def run ():
143+ win = MainWindow ()
144+ QtGui .QApplication .instance ().exec_ ()
145+ return win
146+
147+ #run()
0 commit comments