Skip to content

Commit

Permalink
add ability to save midplane transform
Browse files Browse the repository at this point in the history
  • Loading branch information
niksirbi committed May 23, 2024
1 parent 9ef6011 commit 7c16823
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
27 changes: 27 additions & 0 deletions brainglobe_template_builder/napari/align_widget.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from pathlib import Path

from napari.layers import Image, Labels, Layer, Points
from napari.utils.notifications import show_info
from napari.viewer import Viewer
from qtpy.QtWidgets import (
QComboBox,
QFileDialog,
QFormLayout,
QGroupBox,
QPushButton,
Expand Down Expand Up @@ -95,6 +98,16 @@ def _create_align_group(self):
self.align_image_button.clicked.connect(self._on_align_button_click)
self.align_groupbox.layout().addRow(self.align_image_button)

# Add button to save transformed image
self.save_transform_button = QPushButton(
"Save transform", parent=self.align_groupbox
)
self.save_transform_button.setEnabled(False)
self.save_transform_button.clicked.connect(
self._on_save_transform_click
)
self.align_groupbox.layout().addRow(self.save_transform_button)

def _get_layers_by_type(self, layer_type: Layer) -> list:
"""Return a list of napari layers of a given type."""
return [
Expand Down Expand Up @@ -183,6 +196,8 @@ def _on_align_button_click(self):
self.viewer.layers[aligned_mask_name].visible = False
# Make aligner object accessible to other methods
self.aligner = aligner
# Enable save transform button
self.save_transform_button.setEnabled(True)

def _on_dropdown_selection_change(self):
# Enable estimate button if mask dropdown has a value
Expand All @@ -199,3 +214,15 @@ def _on_dropdown_selection_change(self):
self.align_image_button.setEnabled(False)
else:
self.align_image_button.setEnabled(True)

def _on_save_transform_click(self):
"""Save the midplane alignment tranform to a text file."""
if not hasattr(self, "aligner"):
show_info("Please align the image to the midplane first")
return
dlg = QFileDialog()
dlg.setFileMode(QFileDialog.AnyFile)
dlg.setAcceptMode(QFileDialog.AcceptSave)
if dlg.exec_():
path = dlg.selectedFiles()[0]
self.aligner.save_transform(Path(path))
10 changes: 9 additions & 1 deletion brainglobe_template_builder/preproc/alignment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from itertools import product
from pathlib import Path
from typing import Literal

import numpy as np
Expand Down Expand Up @@ -193,9 +194,16 @@ def transform_image(self, image: np.ndarray = None):
self._compute_transform()
if image is None:
image = self.image
self.transformed_image = apply_transform(image, self.transform)
transformed_image = apply_transform(image, self.transform)
self.transformed_image = transformed_image.astype(image.dtype)
return self.transformed_image

def save_transform(self, dest_path: Path):
"""Save the midplane alignment transform to a text file."""
if not hasattr(self, "transform"):
raise ValueError("Please align the image to the midplane first")
np.savetxt(dest_path, self.transform)

def label_halves(self, image: np.ndarray) -> np.ndarray:
"""Label each half of the image along the symmetry axis with different
integer values, to help diagnose issues with the splitting process.
Expand Down

0 comments on commit 7c16823

Please sign in to comment.