Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit dd40120

Browse files
author
LAKESIDE\LindaT18
committed
rewrite line profile using getArrayRegion, compare roi now appears on same plot
1 parent 2aaaf1b commit dd40120

1 file changed

Lines changed: 58 additions & 45 deletions

File tree

PythonGUI_apps/FLIM_analysis/FLIM_plot.py

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,20 @@ def line_profile_update_plot(self):
8484
if self.ui.line_profile_checkBox.isChecked() and hasattr(self, "intensity_sums"):
8585
roiPlot = self.ui.intensity_sums_viewBox.getRoiPlot()
8686
roiPlot.clear()
87-
intensity_sums = self.intensity_sums
8887
roi = self.ui.intensity_sums_viewBox.roi
8988

90-
##select relevant data in x direction by looking at roi
91-
roi_width = roi.size()[0]
92-
x_roi_min = self.custom_round(roi.pos()[0], self.x_step_size)
93-
x_roi_max = self.custom_round(roi.pos()[0] + roi_width, self.x_step_size)
94-
x_values = np.arange(start=x_roi_min, stop=x_roi_max+self.x_step_size, step=self.x_step_size) #get x values within x range, in x_step_size increments
95-
x_index_min = 0
96-
if x_roi_min != 0: #to avoid divide by 0 error
97-
x_index_min = round(x_roi_min/self.x_step_size) -1
98-
x_index_max = round(x_roi_max/self.x_step_size)+1
99-
column_slice = intensity_sums[:, x_index_min:x_index_max] #get array of data in x range
100-
101-
##select relevant data in y direction by looking at roi
102-
roi_height = roi.size()[1]
103-
y_roi_min = self.custom_round(roi.pos()[1], self.y_step_size)
104-
y_roi_max = self.custom_round(roi.pos()[1] + roi_height, self.y_step_size)
105-
y_range = [y_roi_min, y_roi_max] #get y range of roi
106-
y_index_min = 0
107-
if y_range[0] != 0: #to avoid divide by 0 error
108-
y_index_min = round(y_roi_min/self.y_step_size) -1
109-
y_index_max = round(y_range[1]/self.y_step_size)+1
110-
row_slice = column_slice[y_index_min:y_index_max] #get array of data in y range
111-
112-
#sum intensities along columns
113-
sums_to_plot = np.sum(row_slice, axis=0)
114-
115-
#even with rounding, x_values and sums_to_plot sometimes have slightly uneven lengths
116-
#handle this issue here
117-
if x_values.shape[0] != sums_to_plot.shape[0]:
118-
newshape = min(x_values.shape[0], sums_to_plot.shape[0])
119-
x_values = x_values[0:newshape]
120-
sums_to_plot = sums_to_plot[0:newshape]
121-
89+
image = self.ui.intensity_sums_viewBox.getProcessedImage()
90+
91+
# Extract image data from ROI
92+
axes = (self.ui.intensity_sums_viewBox.axes['x'], self.ui.intensity_sums_viewBox.axes['y'])
93+
data, coords = roi.getArrayRegion(image.view(np.ndarray), self.ui.intensity_sums_viewBox.imageItem, axes, returnMappedCoords=True)
94+
95+
#calculate sums along columns in region
96+
sums_to_plot = np.sum(data, axis=0)
97+
98+
#get scan x-coordinates in region
99+
x_values = coords[1][0]
100+
122101
try:
123102
roiPlot.plot(x_values, sums_to_plot)
124103
except:
@@ -129,6 +108,10 @@ def plot_raw_scan(self):
129108
data = self.pkl_file
130109
self.numb_pixels_X = int((data['Scan Parameters']['X scan size (um)'])/(data['Scan Parameters']['X step size (um)']))
131110
self.numb_pixels_Y = int((data['Scan Parameters']['Y scan size (um)'])/(data['Scan Parameters']['Y step size (um)']))
111+
self.x_step_size = float(data['Scan Parameters']['X step size (um)'])
112+
self.x_scan_size = float(data['Scan Parameters']['X scan size (um)'])
113+
self.y_step_size = float(data['Scan Parameters']['Y step size (um)'])
114+
self.y_scan_size = float(data['Scan Parameters']['Y scan size (um)'])
132115
# TODO test line scan plots
133116
hist_data = data['Histogram data']
134117

@@ -139,9 +122,12 @@ def plot_raw_scan(self):
139122
self.ui.raw_hist_data_viewBox.setImage(self.hist_image, scale=
140123
(data['Scan Parameters']['X step size (um)'],
141124
data['Scan Parameters']['Y step size (um)']), xvals=self.times)
142-
if self.ui.compare_checkBox.isChecked():
143-
self.ui.imv2.setImage(self.hist_image, scale= (data['Scan Parameters']['X step size (um)'],
144-
data['Scan Parameters']['Y step size (um)']), xvals=self.times)
125+
self.ui.raw_hist_data_viewBox.roi.setSize([self.x_scan_size, self.y_scan_size])
126+
# if self.ui.compare_checkBox.isChecked():
127+
# self.ui.imv2.setImage(self.hist_image, scale= (data['Scan Parameters']['X step size (um)'],
128+
# data['Scan Parameters']['Y step size (um)']), xvals=self.times)
129+
self.switch_compare()
130+
self.ui.raw_hist_data_viewBox.ui.roiBtn.clicked.connect(self.switch_compare)
145131
scale = pg.ScaleBar(size=1,suffix='um')
146132
scale.setParentItem(self.ui.raw_hist_data_viewBox.view)
147133
scale.anchor((1, 1), (1, 1), offset=(-30, -30))
@@ -153,16 +139,43 @@ def switch_compare(self):
153139
"""
154140
Handles compare checkbox. If checked, show second ROI that user can use for comparison to first ROI.
155141
"""
156-
if self.ui.compare_checkBox.isChecked():
157-
self.imv2 = pg.ImageView()
158-
if hasattr(self, "hist_image"):
159-
self.imv2.setImage(self.hist_image, scale= (self.pkl_file['Scan Parameters']['X step size (um)'],
160-
self.pkl_file['Scan Parameters']['Y step size (um)']), xvals=self.times)
161-
self.imv2.view.invertY(False) # stop y-axis invert
162-
self.ui.gridLayout.addWidget(self.imv2, 10, 4)
142+
if self.ui.compare_checkBox.isChecked() and hasattr(self, "hist_image"):
143+
if not hasattr(self, "roi2"):
144+
self.roi2 = pg.ROI(pos=[0,0], size=[int(self.x_scan_size/2), int(self.y_scan_size/2)], movable=True, pen='r')
145+
self.roi2.addScaleHandle([1, 1], [0, 0])
146+
self.roi2.addScaleHandle([0, 0], [1, 1])
147+
self.roi2.sigRegionChangeFinished.connect(self.update_roi2_plot)
148+
self.ui.raw_hist_data_viewBox.addItem(self.roi2)
149+
self.update_roi2_plot()
150+
self.roi2.hide()
151+
self.roi2_plot.hide()
152+
if self.ui.raw_hist_data_viewBox.ui.roiBtn.isChecked():
153+
self.roi2.show()
154+
self.roi2_plot.show()
155+
else:
156+
self.roi2.hide()
157+
self.roi2.hide()
163158
else:
164-
self.ui.gridLayout.removeWidget(self.imv2)
165-
self.imv2.hide()
159+
if hasattr(self, "roi2"):
160+
self.roi2.hide()
161+
self.roi2_plot.hide()
162+
163+
def update_roi2_plot(self):
164+
#Adapted from pyqtgraph imageview sourcecode
165+
166+
image = self.ui.raw_hist_data_viewBox.getProcessedImage()
167+
168+
# Extract image data from ROI
169+
axes = (self.ui.raw_hist_data_viewBox.axes['x'], self.ui.raw_hist_data_viewBox.axes['y'])
170+
data, coords = self.roi2.getArrayRegion(image.view(np.ndarray), self.ui.raw_hist_data_viewBox.imageItem, axes, returnMappedCoords=True)
171+
if data is None:
172+
return
173+
# Average data within entire ROI for each frame
174+
data = data.mean(axis=max(axes)).mean(axis=min(axes))
175+
xvals = self.ui.raw_hist_data_viewBox.tVals
176+
if hasattr(self, "roi2_plot"):
177+
self.roi2_plot.clear()
178+
self.roi2_plot = self.ui.raw_hist_data_viewBox.getRoiPlot().plot(xvals, data, pen='r')
166179

167180
def save_intensities_image(self):
168181
try:

0 commit comments

Comments
 (0)