|
| 1 | +"""Widgets for loading movement datasets from file.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from napari.settings import get_settings |
| 7 | +from napari.utils.notifications import show_warning |
| 8 | +from napari.viewer import Viewer |
| 9 | +from qtpy.QtWidgets import ( |
| 10 | + QComboBox, |
| 11 | + QFileDialog, |
| 12 | + QFormLayout, |
| 13 | + QHBoxLayout, |
| 14 | + QLineEdit, |
| 15 | + QPushButton, |
| 16 | + QSpinBox, |
| 17 | + QWidget, |
| 18 | +) |
| 19 | + |
| 20 | +from movement.io import load_poses |
| 21 | +from movement.napari.convert import poses_to_napari_tracks |
| 22 | +from movement.napari.layer_styles import PointsStyle |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | +# Allowed poses file suffixes for each supported source software |
| 27 | +SUPPORTED_POSES_FILES = { |
| 28 | + "DeepLabCut": ["*.h5", "*.csv"], |
| 29 | + "LightningPose": ["*.csv"], |
| 30 | + "SLEAP": ["*.h5", "*.slp"], |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +class PosesLoader(QWidget): |
| 35 | + """Widget for loading movement poses datasets from file.""" |
| 36 | + |
| 37 | + def __init__(self, napari_viewer: Viewer, parent=None): |
| 38 | + """Initialize the loader widget.""" |
| 39 | + super().__init__(parent=parent) |
| 40 | + self.viewer = napari_viewer |
| 41 | + self.setLayout(QFormLayout()) |
| 42 | + # Create widgets |
| 43 | + self._create_source_software_widget() |
| 44 | + self._create_fps_widget() |
| 45 | + self._create_file_path_widget() |
| 46 | + self._create_load_button() |
| 47 | + # Enable layer tooltips from napari settings |
| 48 | + self._enable_layer_tooltips() |
| 49 | + |
| 50 | + def _create_source_software_widget(self): |
| 51 | + """Create a combo box for selecting the source software.""" |
| 52 | + self.source_software_combo = QComboBox() |
| 53 | + self.source_software_combo.setObjectName("source_software_combo") |
| 54 | + self.source_software_combo.addItems(SUPPORTED_POSES_FILES.keys()) |
| 55 | + self.layout().addRow("source software:", self.source_software_combo) |
| 56 | + |
| 57 | + def _create_fps_widget(self): |
| 58 | + """Create a spinbox for selecting the frames per second (fps).""" |
| 59 | + self.fps_spinbox = QSpinBox() |
| 60 | + self.fps_spinbox.setObjectName("fps_spinbox") |
| 61 | + self.fps_spinbox.setMinimum(1) |
| 62 | + self.fps_spinbox.setMaximum(1000) |
| 63 | + self.fps_spinbox.setValue(30) |
| 64 | + self.layout().addRow("fps:", self.fps_spinbox) |
| 65 | + |
| 66 | + def _create_file_path_widget(self): |
| 67 | + """Create a line edit and browse button for selecting the file path. |
| 68 | +
|
| 69 | + This allows the user to either browse the file system, |
| 70 | + or type the path directly into the line edit. |
| 71 | + """ |
| 72 | + # File path line edit and browse button |
| 73 | + self.file_path_edit = QLineEdit() |
| 74 | + self.file_path_edit.setObjectName("file_path_edit") |
| 75 | + self.browse_button = QPushButton("Browse") |
| 76 | + self.browse_button.setObjectName("browse_button") |
| 77 | + self.browse_button.clicked.connect(self._on_browse_clicked) |
| 78 | + # Layout for line edit and button |
| 79 | + self.file_path_layout = QHBoxLayout() |
| 80 | + self.file_path_layout.addWidget(self.file_path_edit) |
| 81 | + self.file_path_layout.addWidget(self.browse_button) |
| 82 | + self.layout().addRow("file path:", self.file_path_layout) |
| 83 | + |
| 84 | + def _create_load_button(self): |
| 85 | + """Create a button to load the file and add layers to the viewer.""" |
| 86 | + self.load_button = QPushButton("Load") |
| 87 | + self.load_button.setObjectName("load_button") |
| 88 | + self.load_button.clicked.connect(lambda: self._on_load_clicked()) |
| 89 | + self.layout().addRow(self.load_button) |
| 90 | + |
| 91 | + def _on_browse_clicked(self): |
| 92 | + """Open a file dialog to select a file.""" |
| 93 | + file_suffixes = SUPPORTED_POSES_FILES[ |
| 94 | + self.source_software_combo.currentText() |
| 95 | + ] |
| 96 | + |
| 97 | + file_path, _ = QFileDialog.getOpenFileName( |
| 98 | + self, |
| 99 | + caption="Open file containing predicted poses", |
| 100 | + filter=f"Poses files ({' '.join(file_suffixes)})", |
| 101 | + ) |
| 102 | + |
| 103 | + # A blank string is returned if the user cancels the dialog |
| 104 | + if not file_path: |
| 105 | + return |
| 106 | + |
| 107 | + # Add the file path to the line edit (text field) |
| 108 | + self.file_path_edit.setText(file_path) |
| 109 | + |
| 110 | + def _on_load_clicked(self): |
| 111 | + """Load the file and add as a Points layer to the viewer.""" |
| 112 | + fps = self.fps_spinbox.value() |
| 113 | + source_software = self.source_software_combo.currentText() |
| 114 | + file_path = self.file_path_edit.text() |
| 115 | + if file_path == "": |
| 116 | + show_warning("No file path specified.") |
| 117 | + return |
| 118 | + ds = load_poses.from_file(file_path, source_software, fps) |
| 119 | + |
| 120 | + self.data, self.props = poses_to_napari_tracks(ds) |
| 121 | + logger.info("Converted poses dataset to a napari Tracks array.") |
| 122 | + logger.debug(f"Tracks array shape: {self.data.shape}") |
| 123 | + |
| 124 | + self.file_name = Path(file_path).name |
| 125 | + self._add_points_layer() |
| 126 | + |
| 127 | + self._set_playback_fps(fps) |
| 128 | + logger.debug(f"Set napari playback speed to {fps} fps.") |
| 129 | + |
| 130 | + def _add_points_layer(self): |
| 131 | + """Add the predicted poses to the viewer as a Points layer.""" |
| 132 | + # Style properties for the napari Points layer |
| 133 | + points_style = PointsStyle( |
| 134 | + name=f"poses: {self.file_name}", |
| 135 | + properties=self.props, |
| 136 | + ) |
| 137 | + # Color the points by individual if there are multiple individuals |
| 138 | + # Otherwise, color by keypoint |
| 139 | + n_individuals = len(self.props["individual"].unique()) |
| 140 | + points_style.set_color_by( |
| 141 | + prop="individual" if n_individuals > 1 else "keypoint" |
| 142 | + ) |
| 143 | + # Add the points layer to the viewer |
| 144 | + self.viewer.add_points(self.data[:, 1:], **points_style.as_kwargs()) |
| 145 | + logger.info("Added poses dataset as a napari Points layer.") |
| 146 | + |
| 147 | + @staticmethod |
| 148 | + def _set_playback_fps(fps: int): |
| 149 | + """Set the playback speed for the napari viewer.""" |
| 150 | + settings = get_settings() |
| 151 | + settings.application.playback_fps = fps |
| 152 | + |
| 153 | + @staticmethod |
| 154 | + def _enable_layer_tooltips(): |
| 155 | + """Toggle on tooltip visibility for napari layers. |
| 156 | +
|
| 157 | + This nicely displays the layer properties as a tooltip |
| 158 | + when hovering over the layer in the napari viewer. |
| 159 | + """ |
| 160 | + settings = get_settings() |
| 161 | + settings.appearance.layer_tooltip_visibility = True |
0 commit comments