Skip to content

Commit

Permalink
Applied black formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
calebweinreb committed Jan 6, 2024
1 parent 6c0ca9e commit e311a78
Show file tree
Hide file tree
Showing 16 changed files with 575 additions and 376 deletions.
35 changes: 18 additions & 17 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#
import os
import sys
sys.path.insert(0, os.path.abspath('../../'))

sys.path.insert(0, os.path.abspath("../../"))


# -- Project information -----------------------------------------------------

project = 'snub'
copyright = '2022, Caleb Weinreb'
author = 'Caleb Weinreb'
project = "snub"
copyright = "2022, Caleb Weinreb"
author = "Caleb Weinreb"


# -- General configuration ---------------------------------------------------
Expand All @@ -28,16 +29,17 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.autosummary',
'sphinx.ext.viewcode',
'sphinx.ext.coverage',
'sphinx.ext.autosectionlabel']
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx.ext.autosummary",
"sphinx.ext.viewcode",
"sphinx.ext.coverage",
"sphinx.ext.autosectionlabel",
]


# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
Expand All @@ -50,16 +52,15 @@
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

html_favicon = '../media/favicon.png'
html_static_path = ["_static"]

def setup(app):
app.add_css_file('css/custom.css') # may also be an URL
html_favicon = "../media/favicon.png"


def setup(app):
app.add_css_file("css/custom.css") # may also be an URL
4 changes: 2 additions & 2 deletions snub/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from snub.gui.main import run

if __name__ == '__main__':
run()
if __name__ == "__main__":
run()
2 changes: 1 addition & 1 deletion snub/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import panels
from . import stacks
from . import tracks
from . import main
from . import main
152 changes: 101 additions & 51 deletions snub/gui/panels/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,129 +19,179 @@


def _roi_contours(rois, dims, threshold_max_ratio=0.2, blur_kernel=2):
rois = np.array(rois.todense()).reshape(rois.shape[0],*dims)
rois = np.array(rois.todense()).reshape(rois.shape[0], *dims)
contour_coordinates = []
for roi in rois:
roi_blur = cv2.GaussianBlur(roi,(11,11),blur_kernel)
roi_mask = roi_blur > roi_blur.max()*threshold_max_ratio
xy = cv2.findContours(roi_mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0][0].squeeze()
contour_coordinates.append(np.vstack((xy,xy[:1])))
roi_blur = cv2.GaussianBlur(roi, (11, 11), blur_kernel)
roi_mask = roi_blur > roi_blur.max() * threshold_max_ratio
xy = cv2.findContours(
roi_mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)[0][0].squeeze()
contour_coordinates.append(np.vstack((xy, xy[:1])))
return contour_coordinates


class ROIPanel(Panel, HeaderMixin):
eps = 1e-10

def __init__(self, config, rois_path=None, labels_path=None, timestamps_path=None,
dimensions=None, video_paths=None, contour_colors={}, linewidth=3,
initial_selected_rois=[], vmin=0, vmax=1, colormap='viridis', **kwargs):

def __init__(
self,
config,
rois_path=None,
labels_path=None,
timestamps_path=None,
dimensions=None,
video_paths=None,
contour_colors={},
linewidth=3,
initial_selected_rois=[],
vmin=0,
vmax=1,
colormap="viridis",
**kwargs
):
super().__init__(config, **kwargs)

self.linewidth = linewidth
self.colormap = colormap
self.vmin,self.vmax = vmin,vmax
self.vmin, self.vmax = vmin, vmax
self.dims = dimensions
self.current_frame_index = None
self.is_visible = True

self.rois = load_npz(os.path.join(config['project_directory'],rois_path))
self.timestamps = np.load(os.path.join(config['project_directory'],timestamps_path))
self.rois = load_npz(os.path.join(config["project_directory"], rois_path))
self.timestamps = np.load(
os.path.join(config["project_directory"], timestamps_path)
)

if labels_path is None: self.labels = [str(i) for i in range(self.rois.shape[0])]
else: self.labels = open(os.path.join(config['project_directory'],labels_path),'r').read().split('\n')
if labels_path is None:
self.labels = [str(i) for i in range(self.rois.shape[0])]
else:
self.labels = (
open(os.path.join(config["project_directory"], labels_path), "r")
.read()
.split("\n")
)

self.adjust_colormap_dialog = AdjustColormapDialog(self, self.vmin, self.vmax)
self.adjust_colormap_dialog.new_range.connect(self.update_colormap_range)

self.canvas = SceneCanvas(self, keys='interactive', show=True)
self.canvas = SceneCanvas(self, keys="interactive", show=True)
self.canvas.events.mouse_release.connect(self.mouse_release)
self.viewbox = self.canvas.central_widget.add_grid().add_view(row=0, col=0, camera='panzoom')
self.viewbox.camera.set_range(x=(0,self.dims[1]), y=(0,self.dims[0]), margin=0)
self.viewbox.camera.aspect=1
self.viewbox = self.canvas.central_widget.add_grid().add_view(
row=0, col=0, camera="panzoom"
)
self.viewbox.camera.set_range(
x=(0, self.dims[1]), y=(0, self.dims[0]), margin=0
)
self.viewbox.camera.aspect = 1

self.contours = {}
for label,coordinates in zip(self.labels, _roi_contours(self.rois, self.dims)):
color = contour_colors[label] if label in contour_colors else _random_color()
self.contours[label] = Line(coordinates, color=np.array(color)/255,
width=self.linewidth, connect='strip', parent=None)

self.vids = {name : VideoReader(
os.path.join(config['project_directory'],video_path)
) for name,video_path in video_paths.items()}
for label, coordinates in zip(self.labels, _roi_contours(self.rois, self.dims)):
color = (
contour_colors[label] if label in contour_colors else _random_color()
)
self.contours[label] = Line(
coordinates,
color=np.array(color) / 255,
width=self.linewidth,
connect="strip",
parent=None,
)

self.vids = {
name: VideoReader(os.path.join(config["project_directory"], video_path))
for name, video_path in video_paths.items()
}

self.dropDown = QComboBox()
self.dropDown.addItems(list(video_paths.keys())[::-1])
self.dropDown.activated.connect(self.update_image)

self.image = Image(np.zeros(self.dims, dtype=np.float32),
cmap=colormap, parent=self.viewbox.scene, clim=(0,1))
self.update_current_time(config['init_current_time'])
self.image = Image(
np.zeros(self.dims, dtype=np.float32),
cmap=colormap,
parent=self.viewbox.scene,
clim=(0, 1),
)
self.update_current_time(config["init_current_time"])
self.initUI(**kwargs)

def initUI(self, **kwargs):
super().initUI(**kwargs)
self.layout.addWidget(self.dropDown)
self.layout.addWidget(self.canvas.native)
self.image.order = 1
for c in self.contours.values(): c.order=0
self.dropDown.setStyleSheet("""
for c in self.contours.values():
c.order = 0
self.dropDown.setStyleSheet(
"""
QComboBox::item { color: white; background-color : #3E3E3E;}
QComboBox::item:selected { background-color: #999999;} """)
QComboBox::item:selected { background-color: #999999;} """
)

def update_visible_contours(self, visible_contours):
for l,c in self.contours.items():
for l, c in self.contours.items():
if l in visible_contours:
c.parent = self.viewbox.scene
else: c.parent = None
else:
c.parent = None

def update_current_time(self, t):
self.current_frame_index = min(self.timestamps.searchsorted(t), len(self.timestamps)-1)
if self.is_visible: self.update_image()
self.current_frame_index = min(
self.timestamps.searchsorted(t), len(self.timestamps) - 1
)
if self.is_visible:
self.update_image()

def toggle_visiblity(self, *args):
super().toggle_visiblity(*args)
if self.is_visible: self.update_image()
if self.is_visible:
self.update_image()

def update_image(self):
name = self.dropDown.currentText()
if self.current_frame_index is None: x = np.zeros(self.dims)
else: x = self.vids[name][self.current_frame_index][:,:,0]/255
image = (np.clip(x, self.vmin, self.vmax)-self.vmin)/(self.vmax-self.vmin)
if self.current_frame_index is None:
x = np.zeros(self.dims)
else:
x = self.vids[name][self.current_frame_index][:, :, 0] / 255
image = (np.clip(x, self.vmin, self.vmax) - self.vmin) / (self.vmax - self.vmin)
self.image.set_data(image.astype(np.float32))
self.canvas.update()

def update_colormap_range(self, vmin, vmax):
self.vmin,self.vmax = vmin,vmax
self.vmin, self.vmax = vmin, vmax
self.update_image()

def show_adjust_colormap_dialog(self):
self.adjust_colormap_dialog.show()

def mouse_release(self, event):
if event.button == 2: self.context_menu(event)
if event.button == 2:
self.context_menu(event)

def context_menu(self, event):
contextMenu = QMenu(self)
def add_menu_item(name, slot, item_type='label'):

def add_menu_item(name, slot, item_type="label"):
action = QWidgetAction(self)
if item_type=='checkbox':
if item_type == "checkbox":
widget = QCheckBox(name)
widget.stateChanged.connect(slot)
elif item_type=='label':
elif item_type == "label":
widget = QLabel(name)
action.triggered.connect(slot)
action.setDefaultWidget(widget)
contextMenu.addAction(action)
contextMenu.addAction(action)
return widget

# click to show adjust colormap range dialog
label = add_menu_item('Adjust colormap range',self.show_adjust_colormap_dialog)
label = add_menu_item("Adjust colormap range", self.show_adjust_colormap_dialog)

contextMenu.setStyleSheet("""
contextMenu.setStyleSheet(
"""
QMenu::item, QLabel, QCheckBox { background-color : #3E3E3E; padding: 5px 6px 5px 6px;}
QMenu::item:selected, QLabel:hover, QCheckBox:hover { background-color: #999999;}
QMenu::separator { background-color: rgb(20,20,20);} """)
QMenu::separator { background-color: rgb(20,20,20);} """
)
action = contextMenu.exec_(event.native.globalPos())


Loading

0 comments on commit e311a78

Please sign in to comment.