Skip to content

Commit 46de92d

Browse files
committed
Update drawROI to accomodate switching to background image to second channel. Fixes #1135.
1 parent 8b6cd2c commit 46de92d

1 file changed

Lines changed: 51 additions & 34 deletions

File tree

suite2p/gui/drawroi.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99
import pyqtgraph as pg
1010
from qtpy import QtGui, QtCore
11-
from qtpy.QtWidgets import QPushButton, QLabel, QLineEdit, QMainWindow, QGridLayout, QButtonGroup, QMessageBox, QWidget
11+
from qtpy.QtWidgets import QPushButton, QLabel, QLineEdit, QMainWindow, QGridLayout, QButtonGroup, QMessageBox, QWidget, QVBoxLayout
1212
from matplotlib.colors import hsv_to_rgb
1313
from scipy import stats
1414
from scipy.ndimage import rotate
@@ -121,8 +121,10 @@ def __init__(self, bid, Text, parent=None):
121121
self.show()
122122

123123
def press(self, parent, bid):
124+
for b in range(len(parent.views)):
125+
parent.viewbtns.button(b).setStyleSheet(parent.styleUnpressed)
126+
parent.viewbtns.button(bid).setStyleSheet(parent.stylePressed)
124127
parent.img0.setImage(parent.masked_images[:, :, :, bid])
125-
126128
parent.win.show()
127129
parent.show()
128130

@@ -141,7 +143,7 @@ def __init__(self, parent):
141143
# layout = QtGui.QFormLayout()
142144
self.cwidget.setLayout(self.l0)
143145
self.stylePressed = ("QPushButton {Text-align: left; "
144-
"background-color: rgb(100,50,100); "
146+
"background-color: rgb(100,100,100); "
145147
"color:white;}")
146148
self.styleUnpressed = ("QPushButton {Text-align: left; "
147149
"background-color: rgb(50,50,50); "
@@ -218,17 +220,28 @@ def __init__(self, parent):
218220
"W: mean img", "E: mean img (enhanced)", "R: correlation map",
219221
"T: max projection"
220222
]
223+
self.has_chan2 = "meanImg_chan2" in parent.ops
224+
if self.has_chan2:
225+
self.views.append("Y: mean img chan2")
221226
b = 0
222227
self.viewbtns = QButtonGroup(self)
228+
view_container = QWidget()
229+
view_vbox = QVBoxLayout()
230+
view_vbox.setContentsMargins(0, 0, 0, 0)
231+
view_vbox.setSpacing(4)
232+
view_container.setLayout(view_vbox)
223233
for names in self.views:
224234
btn = ViewButton(b, "&" + names, self)
225235
self.viewbtns.addButton(btn, b)
226-
self.l0.addWidget(btn, b, 4, 1, 1)
236+
view_vbox.addWidget(btn)
227237
btn.setEnabled(True)
228238
b += 1
229-
b = 0
230-
self.viewbtns.button(b).setChecked(True)
231-
self.viewbtns.button(b).setStyleSheet(self.stylePressed)
239+
view_vbox.addStretch()
240+
self.l0.addWidget(view_container, 0, 4, 3, 1)
241+
for b in range(len(self.views)):
242+
self.viewbtns.button(b).setStyleSheet(self.styleUnpressed)
243+
self.viewbtns.button(0).setChecked(True)
244+
self.viewbtns.button(0).setStyleSheet(self.stylePressed)
232245

233246
self.l0.addWidget(QLabel("neuropil"), 13, 13, 1, 1)
234247

@@ -309,37 +322,38 @@ def close_GUI(self):
309322
self.close()
310323

311324
def normalize_img_add_masks(self):
312-
masked_image = np.zeros(
313-
((self.Ly, self.Lx, 3, 4))) # 3 for RGB and 4 for buttons
314-
for i in np.arange(4): # 4 because 4 buttons
325+
nviews = len(self.views)
326+
masked_image = np.zeros((self.Ly, self.Lx, 3, nviews))
327+
yr = slice(self.parent.ops["yrange"][0], self.parent.ops["yrange"][1])
328+
xr = slice(self.parent.ops["xrange"][0], self.parent.ops["xrange"][1])
329+
for i in np.arange(nviews):
315330
if i == 0:
316-
mimg = np.zeros((self.Ly, self.Lx), np.float32)
317-
mimg[self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
318-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]] = self.parent.ops["meanImg"][
319-
self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
320-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]]
321-
331+
src = self.parent.ops["meanImg"]
322332
elif i == 1:
323-
mimg = np.zeros((self.Ly, self.Lx), np.float32)
324-
mimg[self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
325-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]] = self.parent.ops["meanImgE"][
326-
self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
327-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]]
333+
src = self.parent.ops["meanImgE"]
328334
elif i == 2:
329-
mimg = np.zeros((self.Ly, self.Lx), np.float32)
330-
mimg[self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
331-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]] = self.parent.ops["Vcorr"]
332-
335+
src = self.parent.ops["Vcorr"]
336+
elif i == 3:
337+
src = self.parent.ops.get("max_proj", None)
338+
elif i == 4 and self.has_chan2:
339+
src = self.parent.ops["meanImg_chan2"]
333340
else:
334-
mimg = np.zeros((self.Ly, self.Lx), np.float32)
335-
if "max_proj" in self.parent.ops:
336-
mimg[self.parent.ops["yrange"][0]:self.parent.ops["yrange"][1],
337-
self.parent.ops["xrange"][0]:self.parent.ops["xrange"][1]] = self.parent.ops["max_proj"]
338-
339-
mimg1 = np.percentile(mimg, 1)
340-
mimg99 = np.percentile(mimg, 99)
341-
mimg = (mimg - mimg1) / (mimg99 - mimg1)
342-
mimg = np.maximum(0, np.minimum(1, mimg))
341+
src = None
342+
343+
mimg = np.zeros((self.Ly, self.Lx), np.float32)
344+
if src is not None:
345+
mimg1 = np.percentile(src, 1)
346+
mimg99 = np.percentile(src, 99)
347+
if mimg99 > mimg1:
348+
src = (src - mimg1) / (mimg99 - mimg1)
349+
else:
350+
src = np.zeros_like(src)
351+
src = np.clip(src, 0, 1).astype(np.float32)
352+
if src.shape[0] == self.Ly and src.shape[1] == self.Lx:
353+
mimg = src
354+
else:
355+
mimg[yr, xr] = src
356+
343357
masked_image[:, :, :, i] = self.create_masks_of_cells(mimg)
344358

345359
return masked_image
@@ -387,6 +401,9 @@ def keyPressEvent(self, event):
387401
elif event.key() == QtCore.Qt.Key_T:
388402
self.viewbtns.button(3).setChecked(True)
389403
self.viewbtns.button(3).press(self, 3)
404+
elif event.key() == QtCore.Qt.Key_Y and self.has_chan2:
405+
self.viewbtns.button(4).setChecked(True)
406+
self.viewbtns.button(4).press(self, 4)
390407

391408
def add_ROI(self, pos=None):
392409
self.iROI = len(self.ROIs)

0 commit comments

Comments
 (0)