|
5 | 5 | from pyqtgraph import exporters |
6 | 6 | from pyqtgraph.Qt import QtCore, QtGui, QtWidgets |
7 | 7 | import matplotlib.pyplot as plt |
8 | | -import pandas as pd |
9 | 8 | import numpy as np |
10 | | -import time |
11 | 9 |
|
12 | 10 | # local modules |
13 | 11 |
|
|
30 | 28 | plt.rc('axes', linewidth = 3.5) |
31 | 29 |
|
32 | 30 | class MainWindow(TemplateBaseClass): |
33 | | - |
34 | | - def __init__(self): |
35 | | - super(TemplateBaseClass, self).__init__() |
36 | | - |
37 | | - # Create the main window |
38 | | - self.ui = WindowTemplate() |
39 | | - self.ui.setupUi(self) |
40 | | - |
41 | | - #setup uv vis plot |
42 | | - self.plot = self.ui.plotWidget.getPlotItem() |
43 | | - self.plot.setTitle(title="Wavelength vs. Intensity") |
44 | | - self.plot.setLabel('bottom', 'Wavelength', unit='nm') |
45 | | - self.plot.setLabel('left', 'Intensity', unit='a.u.') |
46 | | - self.plot.setLogMode(x=None, y=1) |
47 | | - |
48 | | - #setup line rois for laser and emission |
49 | | - self.laser_region = pg.LinearRegionItem(brush=QtGui.QBrush(QtGui.QColor(255, 0, 0, 50))) |
50 | | - self.laser_region.sigRegionChanged.connect(self.update_laser_spinBoxes) |
51 | | - self.emission_region = pg.LinearRegionItem() |
52 | | - self.emission_region.sigRegionChanged.connect(self.update_emission_spinBoxes) |
53 | | - self.laser_region.setRegion((200, 400)) |
54 | | - self.emission_region.setRegion((700, 800)) |
55 | | - |
56 | | - #setup ui signals |
57 | | - self.ui.load_data_pushButton.clicked.connect(self.open_data_file) |
58 | | - self.ui.plot_pushButton.clicked.connect(self.plot_intensity) |
59 | | - self.ui.clear_pushButton.clicked.connect(self.clear) |
60 | | - self.ui.calculate_plqe_pushButton.clicked.connect(self.calculate_plqe) |
61 | | - self.ui.laser_start_spinBox.valueChanged.connect(self.update_laser_region) |
62 | | - self.ui.laser_stop_spinBox.valueChanged.connect(self.update_laser_region) |
63 | | - self.ui.emission_start_spinBox.valueChanged.connect(self.update_emission_region) |
64 | | - self.ui.emission_stop_spinBox.valueChanged.connect(self.update_emission_region) |
65 | | - |
66 | | - self.show() |
67 | | - |
68 | | - def open_data_file(self): |
69 | | - """ Open data file """ |
70 | | - try: |
71 | | - self.filename = QtWidgets.QFileDialog.getOpenFileName(self) |
72 | | - #self.data = np.loadtxt(self.filename[0], delimiter = '\t', skiprows = 1) |
73 | | - if ".txt" in self.filename[0]: |
74 | | - self.data = np.loadtxt(self.filename[0], delimiter = '\t', skiprows = 1) |
75 | | - elif ".csv" in self.filename[0]: |
76 | | - self.data = np.loadtxt(self.filename[0], delimiter = ',', skiprows = 1) |
77 | | - self.cs_window = ColSelectionWindow(self.data) |
78 | | - self.cs_window.col_selection_signal.connect(self.open_with_col_selection) |
79 | | - self.nm = np.copy(self.data[:,0]) |
80 | | - #self.ref_data = np.copy(self.data[:,1]) |
81 | | - #self.inpath_data = np.copy(self.data[:,2]) |
82 | | - #self.outpath_data = np.copy(self.data[:,3]) |
83 | | - except Exception as err: |
84 | | - print(format(err)) |
85 | | - |
86 | | - def open_with_col_selection(self): |
87 | | - ref_data_col = self.cs_window.ui.ref_spinBox.value() - 1 #subtract since spinboxes refer to column num and not index |
88 | | - inpath_data_col = self.cs_window.ui.inpath_spinBox.value() - 1 |
89 | | - outpath_data_col = self.cs_window.ui.outpath_spinBox.value() - 1 |
90 | | - self.ref_data = np.copy(self.data[:,ref_data_col]) |
91 | | - self.inpath_data = np.copy(self.data[:,inpath_data_col]) |
92 | | - self.outpath_data = np.copy(self.data[:,outpath_data_col]) |
93 | | - |
94 | | - def update_laser_spinBoxes(self): |
95 | | - """ Update laser spinboxes based on line rois """ |
96 | | - self.laser_start, self.laser_stop = self.laser_region.getRegion() |
97 | | - self.ui.laser_start_spinBox.setValue(self.laser_start) |
98 | | - self.ui.laser_stop_spinBox.setValue(self.laser_stop) |
99 | | - |
100 | | - |
101 | | - def update_emission_spinBoxes(self): |
102 | | - """ Update emission spinboxes based on line rois """ |
103 | | - self.emission_start, self.emission_stop = self.emission_region.getRegion() |
104 | | - self.ui.emission_start_spinBox.setValue(self.emission_start) |
105 | | - self.ui.emission_stop_spinBox.setValue(self.emission_stop) |
106 | | - |
107 | | - def update_laser_region(self): |
108 | | - """ Update laser line rois based on spinboxes """ |
109 | | - laser_start = self.ui.laser_start_spinBox.value() |
110 | | - laser_stop = self.ui.laser_stop_spinBox.value() |
111 | | - self.laser_region.setRegion((laser_start, laser_stop)) |
112 | | - |
113 | | - def update_emission_region(self): |
114 | | - """ Update emission line rois based on spinboxes """ |
115 | | - emission_start = self.ui.emission_start_spinBox.value() |
116 | | - emission_stop = self.ui.emission_stop_spinBox.value() |
117 | | - self.emission_region.setRegion((emission_start, emission_stop)) |
118 | | - |
119 | | - def plot_intensity(self): |
120 | | - try: |
121 | | - self.plot.plot(self.nm, self.inpath_data) |
122 | | - self.plot.addItem(self.laser_region, ignoreBounds=True) |
123 | | - self.plot.addItem(self.emission_region, ignoreBounds=True) |
124 | | - except Exception as err: |
125 | | - print(format(err)) |
126 | | - |
127 | | - def find_nearest(self,array,value): |
128 | | - idx = (np.abs(array-value)).argmin() |
129 | | - return idx |
130 | | - |
131 | | - def calculate_plqe(self): |
132 | | - |
133 | | - nm_interp_step = 1 |
134 | | - nm_interp_start = np.ceil(self.nm[0] / nm_interp_step) * nm_interp_step |
135 | | - nm_interp_stop = np.floor(self.nm[len(self.nm) - 1] / nm_interp_step) * nm_interp_step |
136 | | - nm_interp = np.arange(nm_interp_start, nm_interp_stop + nm_interp_step, nm_interp_step) |
137 | | - |
138 | | - ref_interp = np.interp(nm_interp, self.nm, self.ref_data) |
139 | | - |
140 | | - |
141 | | - inpath_interp = np.interp(nm_interp, self.nm, self.inpath_data) |
142 | | - outpath_interp = np.interp(nm_interp, self.nm, self.outpath_data) |
143 | | - |
144 | | - |
145 | | - """L_x is area under laser profile for experiment x""" |
146 | | - """P_x_ is area under emission profile for experiment x""" |
147 | | - |
148 | | - |
149 | | - #plt.semilogy(nm, a1_outpath_data[:,1]) |
150 | | - |
151 | | - emission_start_idx = self.find_nearest(nm_interp, self.emission_start) |
152 | | - emission_stop_idx = self.find_nearest(nm_interp, self.emission_stop) |
153 | | - |
154 | | - laser_start_idx = self.find_nearest(nm_interp, self.laser_start) |
155 | | - laser_stop_idx = self.find_nearest(nm_interp, self.laser_stop) |
156 | | - |
157 | | - la = np.trapz(ref_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
158 | | - lb = np.trapz(outpath_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
159 | | - lc = np.trapz(inpath_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
160 | | - |
161 | | - pa = np.trapz(ref_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
162 | | - pb = np.trapz(outpath_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
163 | | - pc = np.trapz(inpath_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
164 | | - |
165 | | - absorb = 1.0 - (lc / lb) |
166 | | - |
167 | | - plqe = 100 * (pc - ((1.0 - absorb) * pb)) / (la * absorb) |
168 | | - #print('PLQE Percent = %.3f' %(plqe)) |
169 | | - #return plqe |
170 | | - self.ui.plqe_label.setText("%.3f" %(plqe)) |
171 | | - |
172 | | - def clear(self): |
173 | | - self.plot.clear() |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + super(TemplateBaseClass, self).__init__() |
| 34 | + |
| 35 | + # Create the main window |
| 36 | + self.ui = WindowTemplate() |
| 37 | + self.ui.setupUi(self) |
| 38 | + |
| 39 | + #setup uv vis plot |
| 40 | + self.plot = self.ui.plotWidget.getPlotItem() |
| 41 | + self.plot.setTitle(title="Wavelength vs. Intensity") |
| 42 | + self.plot.setLabel('bottom', 'Wavelength', unit='nm') |
| 43 | + self.plot.setLabel('left', 'Intensity', unit='a.u.') |
| 44 | + self.plot.setLogMode(x=None, y=1) |
| 45 | + |
| 46 | + #setup line rois for laser and emission |
| 47 | + self.laser_region = pg.LinearRegionItem(brush=QtGui.QBrush(QtGui.QColor(255, 0, 0, 50))) |
| 48 | + self.laser_region.sigRegionChanged.connect(self.update_laser_spinBoxes) |
| 49 | + self.emission_region = pg.LinearRegionItem() |
| 50 | + self.emission_region.sigRegionChanged.connect(self.update_emission_spinBoxes) |
| 51 | + self.laser_region.setRegion((200, 400)) |
| 52 | + self.emission_region.setRegion((700, 800)) |
| 53 | + |
| 54 | + #setup ui signals |
| 55 | + self.ui.load_data_pushButton.clicked.connect(self.open_data_file) |
| 56 | + self.ui.plot_pushButton.clicked.connect(self.plot_intensity) |
| 57 | + self.ui.clear_pushButton.clicked.connect(self.clear) |
| 58 | + self.ui.calculate_plqe_pushButton.clicked.connect(self.calculate_plqe) |
| 59 | + self.ui.laser_start_spinBox.valueChanged.connect(self.update_laser_region) |
| 60 | + self.ui.laser_stop_spinBox.valueChanged.connect(self.update_laser_region) |
| 61 | + self.ui.emission_start_spinBox.valueChanged.connect(self.update_emission_region) |
| 62 | + self.ui.emission_stop_spinBox.valueChanged.connect(self.update_emission_region) |
| 63 | + |
| 64 | + self.show() |
| 65 | + |
| 66 | + def open_data_file(self): |
| 67 | + """ Open data file """ |
| 68 | + try: |
| 69 | + self.filename = QtWidgets.QFileDialog.getOpenFileName(self) |
| 70 | + #self.data = np.loadtxt(self.filename[0], delimiter = '\t', skiprows = 1) |
| 71 | + if ".txt" in self.filename[0]: |
| 72 | + self.data = np.loadtxt(self.filename[0], delimiter = '\t', skiprows = 1, usecols = (0,1,2,3)) |
| 73 | + elif ".csv" in self.filename[0]: |
| 74 | + self.data = np.loadtxt(self.filename[0], delimiter = ',', skiprows = 1) |
| 75 | + elif ".qua" in self.filename[0]: |
| 76 | + self.data = np.genfromtxt(self.filename[0], delimiter = '\t', skip_header = 12, usecols = (0,1,2,3)) |
| 77 | + self.cs_window = ColSelectionWindow(self.data) |
| 78 | + self.cs_window.col_selection_signal.connect(self.open_with_col_selection) |
| 79 | + self.nm = np.copy(self.data[:,0]) |
| 80 | + #self.ref_data = np.copy(self.data[:,1]) |
| 81 | + #self.inpath_data = np.copy(self.data[:,2]) |
| 82 | + #self.outpath_data = np.copy(self.data[:,3]) |
| 83 | + except Exception as err: |
| 84 | + print(format(err)) |
| 85 | + |
| 86 | + def open_with_col_selection(self): |
| 87 | + ref_data_col = self.cs_window.ui.ref_spinBox.value() - 1 #subtract since spinboxes refer to column num and not index |
| 88 | + inpath_data_col = self.cs_window.ui.inpath_spinBox.value() - 1 |
| 89 | + outpath_data_col = self.cs_window.ui.outpath_spinBox.value() - 1 |
| 90 | + self.ref_data = np.copy(self.data[:,ref_data_col]) |
| 91 | + self.inpath_data = np.copy(self.data[:,inpath_data_col]) |
| 92 | + self.outpath_data = np.copy(self.data[:,outpath_data_col]) |
| 93 | + |
| 94 | + def update_laser_spinBoxes(self): |
| 95 | + """ Update laser spinboxes based on line rois """ |
| 96 | + self.laser_start, self.laser_stop = self.laser_region.getRegion() |
| 97 | + self.ui.laser_start_spinBox.setValue(self.laser_start) |
| 98 | + self.ui.laser_stop_spinBox.setValue(self.laser_stop) |
| 99 | + |
| 100 | + |
| 101 | + def update_emission_spinBoxes(self): |
| 102 | + """ Update emission spinboxes based on line rois """ |
| 103 | + self.emission_start, self.emission_stop = self.emission_region.getRegion() |
| 104 | + self.ui.emission_start_spinBox.setValue(self.emission_start) |
| 105 | + self.ui.emission_stop_spinBox.setValue(self.emission_stop) |
| 106 | + |
| 107 | + def update_laser_region(self): |
| 108 | + """ Update laser line rois based on spinboxes """ |
| 109 | + laser_start = self.ui.laser_start_spinBox.value() |
| 110 | + laser_stop = self.ui.laser_stop_spinBox.value() |
| 111 | + self.laser_region.setRegion((laser_start, laser_stop)) |
| 112 | + |
| 113 | + def update_emission_region(self): |
| 114 | + """ Update emission line rois based on spinboxes """ |
| 115 | + emission_start = self.ui.emission_start_spinBox.value() |
| 116 | + emission_stop = self.ui.emission_stop_spinBox.value() |
| 117 | + self.emission_region.setRegion((emission_start, emission_stop)) |
| 118 | + |
| 119 | + def plot_intensity(self): |
| 120 | + try: |
| 121 | + self.plot.plot(self.nm, self.inpath_data) |
| 122 | + self.plot.addItem(self.laser_region, ignoreBounds=True) |
| 123 | + self.plot.addItem(self.emission_region, ignoreBounds=True) |
| 124 | + except Exception as err: |
| 125 | + print(format(err)) |
| 126 | + |
| 127 | + def find_nearest(self,array,value): |
| 128 | + idx = (np.abs(array-value)).argmin() |
| 129 | + return idx |
| 130 | + |
| 131 | + def calculate_plqe(self): |
| 132 | + |
| 133 | + nm_interp_step = 1 |
| 134 | + nm_interp_start = np.ceil(self.nm[0] / nm_interp_step) * nm_interp_step |
| 135 | + nm_interp_stop = np.floor(self.nm[len(self.nm) - 1] / nm_interp_step) * nm_interp_step |
| 136 | + nm_interp = np.arange(nm_interp_start, nm_interp_stop + nm_interp_step, nm_interp_step) |
| 137 | + |
| 138 | + ref_interp = np.interp(nm_interp, self.nm, self.ref_data) |
| 139 | + |
| 140 | + |
| 141 | + inpath_interp = np.interp(nm_interp, self.nm, self.inpath_data) |
| 142 | + outpath_interp = np.interp(nm_interp, self.nm, self.outpath_data) |
| 143 | + |
| 144 | + |
| 145 | + """L_x is area under laser profile for experiment x""" |
| 146 | + """P_x_ is area under emission profile for experiment x""" |
| 147 | + |
| 148 | + |
| 149 | + #plt.semilogy(nm, a1_outpath_data[:,1]) |
| 150 | + |
| 151 | + emission_start_idx = self.find_nearest(nm_interp, self.emission_start) |
| 152 | + emission_stop_idx = self.find_nearest(nm_interp, self.emission_stop) |
| 153 | + |
| 154 | + laser_start_idx = self.find_nearest(nm_interp, self.laser_start) |
| 155 | + laser_stop_idx = self.find_nearest(nm_interp, self.laser_stop) |
| 156 | + |
| 157 | + la = np.trapz(ref_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
| 158 | + lb = np.trapz(outpath_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
| 159 | + lc = np.trapz(inpath_interp[laser_start_idx: laser_stop_idx], x = nm_interp[laser_start_idx:laser_stop_idx]) |
| 160 | + |
| 161 | + pa = np.trapz(ref_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
| 162 | + pb = np.trapz(outpath_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
| 163 | + pc = np.trapz(inpath_interp[emission_start_idx:emission_stop_idx], x = nm_interp[emission_start_idx:emission_stop_idx]) |
| 164 | + |
| 165 | + absorb = 1.0 - (lc / lb) |
| 166 | + |
| 167 | + plqe = 100 * (pc - ((1.0 - absorb) * pb)) / (la * absorb) |
| 168 | + #print('PLQE Percent = %.3f' %(plqe)) |
| 169 | + #return plqe |
| 170 | + self.ui.plqe_label.setText("%.3f" %(plqe)) |
| 171 | + |
| 172 | + def clear(self): |
| 173 | + self.plot.clear() |
174 | 174 |
|
175 | 175 | """Table view GUI""" |
176 | 176 | ui_file_path = (base_path / "column_selection_gui.ui").resolve() |
177 | 177 | col_selection_WindowTemplate, col_selection_TemplateBaseClass = pg.Qt.loadUiType(ui_file_path) |
178 | 178 |
|
179 | 179 | class ColSelectionWindow(col_selection_TemplateBaseClass): |
180 | | - |
181 | | - col_selection_signal = QtCore.pyqtSignal() #signal to help with pass info back to MainWindow |
182 | | - |
183 | | - def __init__(self, data): |
184 | | - col_selection_TemplateBaseClass.__init__(self) |
185 | | - # Create the param window |
186 | | - self.ui = col_selection_WindowTemplate() |
187 | | - self.ui.setupUi(self) |
188 | | - self.ui.done_pushButton.clicked.connect(self.done) |
189 | | - |
190 | | - self.table_widget = pg.TableWidget() |
191 | | - self.ui.data_preview_groupBox.layout().addWidget(self.table_widget) |
192 | | - |
193 | | - self.table_widget.setData(data) |
194 | | - |
195 | | - #self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False) |
196 | | - self.show() |
197 | | - |
198 | | - def done(self): |
199 | | - self.col_selection_signal.emit() |
200 | | - self.ui.textBrowser.setText("Data successfully loaded.") |
201 | | - #self.close() |
202 | | - |
203 | | - def closeEvent(self, event): |
204 | | - self.col_selection_signal.emit() |
| 180 | + |
| 181 | + col_selection_signal = QtCore.pyqtSignal() #signal to help with pass info back to MainWindow |
| 182 | + |
| 183 | + def __init__(self, data): |
| 184 | + col_selection_TemplateBaseClass.__init__(self) |
| 185 | + # Create the param window |
| 186 | + self.ui = col_selection_WindowTemplate() |
| 187 | + self.ui.setupUi(self) |
| 188 | + self.ui.done_pushButton.clicked.connect(self.done) |
| 189 | + |
| 190 | + self.table_widget = pg.TableWidget() |
| 191 | + self.ui.data_preview_groupBox.layout().addWidget(self.table_widget) |
| 192 | + |
| 193 | + self.table_widget.setData(data) |
| 194 | + |
| 195 | + #self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False) |
| 196 | + self.show() |
| 197 | + |
| 198 | + def done(self): |
| 199 | + self.col_selection_signal.emit() |
| 200 | + self.ui.textBrowser.setText("Data successfully loaded.") |
| 201 | + #self.close() |
| 202 | + |
| 203 | + def closeEvent(self, event): |
| 204 | + self.col_selection_signal.emit() |
205 | 205 |
|
206 | 206 | """Run the Main Window""" |
207 | 207 | def run(): |
208 | | - win = MainWindow() |
209 | | - QtGui.QApplication.instance().exec_() |
210 | | - return win |
| 208 | + win = MainWindow() |
| 209 | + QtGui.QApplication.instance().exec_() |
| 210 | + return win |
211 | 211 |
|
212 | 212 | #run() |
0 commit comments