Skip to content

Commit

Permalink
support timelapse data with "frame" columns in interactive tables
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Apr 2, 2022
1 parent 8531c8a commit 7bbac46
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions napari_skimage_regionprops/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class TableWidget(QWidget):
The table widget represents a table inside napari.
Tables are just views on `properties` of `layers`.
"""
def __init__(self, layer: napari.layers.Layer):
def __init__(self, layer: napari.layers.Layer, viewer:napari.Viewer = None):
super().__init__()

self._layer = layer
self._viewer = viewer

self._view = QTableWidget()
self.set_content(layer.properties)
Expand All @@ -42,24 +43,44 @@ def __init__(self, layer: napari.layers.Layer):
action_widget.layout().setSpacing(3)
action_widget.layout().setContentsMargins(0, 0, 0, 0)


def _clicked_table(self):
if "label" in self._table.keys():
row = self._view.currentRow()
label = self._table["label"][row]
print("Table clicked, set label", label)
self._layer.selected_label = label

frame_column = _determine_frame_column(self._table)
if frame_column is not None and self._viewer is not None:
frame = self._table[frame_column][row]
current_step = list(self._viewer.dims.current_step)
if len(current_step) >= 4:
current_step[-4] = frame
self._viewer.dims.current_step = current_step

def _after_labels_clicked(self):
if "label" in self._table.keys() and hasattr(self._layer, "selected_label"):
row = self._view.currentRow()
label = self._table["label"][row]

frame_column = _determine_frame_column(self._table)
if frame_column is not None and self._viewer is not None:
current_step = list(self._viewer.dims.current_step)
if len(current_step) >= 4:
frame = current_step[-4]

print("labels clicked, set table", label)
if label != self._layer.selected_label:
for r, l in enumerate(self._table["label"]):
if l == self._layer.selected_label:
self._view.setCurrentCell(r, self._view.currentColumn())
break
if frame_column is not None and self._viewer is not None:
for r, (l, f) in enumerate(zip(self._table["label"], self._table[frame_column])):
if l == self._layer.selected_label and f == frame:
self._view.setCurrentCell(r, self._view.currentColumn())
break
else:
for r, l in enumerate(self._table["label"]):
if l == self._layer.selected_label:
self._view.setCurrentCell(r, self._view.currentColumn())
break

# We need to run this later as the labels_layer.selected_label isn't changed yet.
def _clicked_labels(self, event, event1): QTimer.singleShot(200, self._after_labels_clicked)
Expand Down Expand Up @@ -176,7 +197,7 @@ def add_table(labels_layer: napari.layers.Layer, viewer:napari.Viewer) -> TableW
"""
dock_widget = get_table(labels_layer, viewer)
if dock_widget is None:
dock_widget = TableWidget(labels_layer)
dock_widget = TableWidget(labels_layer, viewer)
# add widget to napari
viewer.window.add_dock_widget(dock_widget, area='right', name="Properties of " + labels_layer.name)
else:
Expand All @@ -197,3 +218,10 @@ def get_table(labels_layer: napari.layers.Layer, viewer:napari.Viewer) -> TableW
if potential_table_widget._layer is labels_layer:
return potential_table_widget
return None

def _determine_frame_column(table):
candidates = ["Frame", "frame"]
for c in candidates:
if c in table.keys():
return c
return None

0 comments on commit 7bbac46

Please sign in to comment.