Skip to content

Commit

Permalink
Merge pull request #90 from hanjinliu/fix-inspector
Browse files Browse the repository at this point in the history
Fix inspector
  • Loading branch information
hanjinliu authored Jul 2, 2024
2 parents b43d5c9 + 1307e18 commit 9e52272
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
38 changes: 27 additions & 11 deletions cylindra/widgets/subwidgets/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from cylindra.widgets.subwidgets._child_widget import ChildWidget

if TYPE_CHECKING:
from cylindra.components.tomogram._cyl_tomo import ImageWithPeak
from cylindra.components.tomogram._misc import ImageWithPeak


class MouseMode(Enum):
Expand Down Expand Up @@ -56,15 +56,16 @@ def __init__(self):
self._power_spectra = list[ip.ImgArray | None]()
self._image = np.zeros((1, 1))
self._is_log_scale = False
self._current_spline_index = 0
self._current_binsize = 1
self._last_upsample_params = {}

def __post_init__(self):
self._upsampled_image_item = pg.ImageItem()
self.canvas._viewbox.addItem(self._upsampled_image_item)
self._upsampled_image_item.setVisible(False)
self._infline_x = self.canvas.add_infline(1, 0, color="yellow")
self._infline_y = self.canvas.add_infline(1, 90, color="yellow")
self._infline_x = self.canvas.add_infline((0, 0), 0, color="yellow")
self._infline_y = self.canvas.add_infline((0, 0), 90, color="yellow")
self._layer_axial = self.canvas.add_scatter(
[], [], color="cyan", symbol="+", size=12
)
Expand All @@ -90,11 +91,14 @@ def _set_spline(self, i: int, binsize: int | None = None):
self._power_spectra = []
self._peaks = None
self._image = np.zeros((1, 1))
return None
return None
else:
raise ValueError(f"Invalid spline index: {i}")
spl = main.splines[i]
self._layer_axial.data = [], []
self._layer_angular.data = [], []
self._upsampled_image_item.setVisible(False)
self._current_spline_index = i
if self.show_what == "Local-CFT":
if spl.props.has_loc(H.twist) and spl.has_anchors:
# has local-CFT results
Expand All @@ -110,6 +114,10 @@ def _set_spline(self, i: int, binsize: int | None = None):
self["pos"].visible = True
self["pos"].max = len(self._power_spectra) - 1
else:
if self._may_show_text_overlay(
spl.radius is None, "No CFT available (radius not set)"
):
return
if spl.props.has_glob(H.twist):
# has global-CFT results
if binsize is None:
Expand Down Expand Up @@ -203,12 +211,8 @@ def _pos_changed(self, pos: int):
if len(self._power_spectra) == 0:
return None
_next_image = self._power_spectra[pos]
if _next_image is None:
self.canvas.text_overlay.text = "No CFT available"
self.canvas.text_overlay.visible = True
self.canvas.text_overlay.color = "lime"
return
self.canvas.text_overlay.visible = False
if self._may_show_text_overlay(_next_image is None, "No CFT available"):
return None
self._image = np.asarray(_next_image)
self._update_image()
if self._peaks is None:
Expand All @@ -225,7 +229,19 @@ def _pos_changed(self, pos: int):
@show_what.connect
def _show_what_changed(self, value: str):
if len(self._power_spectra) > 0:
self._set_spline(self.pos, binsize=None)
self._set_spline(self._current_spline_index, binsize=None)

def _may_show_text_overlay(self, when: bool, text: str) -> bool:
if when:
self.canvas.text_overlay.text = text
self.canvas.text_overlay.visible = True
self.canvas.text_overlay.color = "lime"
del self.canvas.image
self._markers.visible = False
self._image = np.zeros((1, 1))
else:
self.canvas.text_overlay.visible = False
return when


@magicclass(widget_type="groupbox", record=False)
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ structures are implemented.
- [Open an image](open_image.md)
- [Fit splines](fit_splines.md)
- [Measure lattice parameters](lattice_params.md)
- [Inspect CFT Results](inspect_results.md)
- [Load & Save Projects](project_io.md)
- [Custom Workflows](workflows.md)
- [Spline](spline/index.md)
Expand Down
15 changes: 11 additions & 4 deletions docs/inspect_results.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# Inspect CFT Results

To clarify if the analysis is done correctly or to determine the parameters, you may
sometimes want to inspect the results of the CFT.
sometimes want to inspect the results of the CFT. This can be done by the spectra
inspector widget.

:material-arrow-right-thin-circle-outline: GUI: `Analysis > Open spectra inspector` or ++ctrl+k++ → ++ctrl+v++

In this window, you can see the local- and global-CFT power spectra of the existing
splines, and the estimated peak positions (red markers) if available.
splines, and the estimated peak positions (red markers) if available. By clicking the
"Load spline" button, you can set other splines in the tomogram for inspection.

![Inspect Results](images/inspect_local_cft.png){ loading=lazy, width=500px }
![Inspection result](images/inspect_local_cft.png){ loading=lazy, width=570px }

After clicking "Upsample spectrum" to enable the upsampling mode, you can upsample the
local power spectrum interactively by clicking the canvas.

![Inspect Results](images/inspect_local_cft_upsampled.png){ loading=lazy, width=500px }
![Upsampled Result](images/inspect_local_cft_upsampled.png){ loading=lazy, width=570px }

If the spline is not well fitted to the image, the peaks of the local power spectrum are
blurred, especially in the x (angular) direction.

![Bad result](images/inspect_local_cft_bad.png){ loading=lazy, width=570px }

!!! note
This widget is also used to measure the lattice parameters of unknown structures.
Expand Down
20 changes: 19 additions & 1 deletion docs/scripts/_screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _workflow_gui(path: Path, tilt_range: tuple[float, float] = (-60, 60)):

ui._runner.run(interval=12, n_refine=0, map_monomers=True)

# inspect local CFT
### inspect local CFT ###
ui.spectra_inspector.show()
ui.spectra_inspector.load_spline(0)
ui.spectra_inspector.peak_viewer.show_what = "Local-CFT"
Expand All @@ -99,7 +99,22 @@ def _workflow_gui(path: Path, tilt_range: tuple[float, float] = (-60, 60)):
QtW.QApplication.processEvents()
_imsave(ui.spectra_inspector.native, "inspect_local_cft_upsampled")
ui.spectra_inspector.peak_viewer.show_what = "Global-CFT" # will be used later
ui.copy_spline(0)
ui.spline_fitter.fit(1, [[1, 0.5], [-0.2, 0.8], [1, 0.3]])
ui.measure_radius(1)
ui.local_cft_analysis(1, interval=50)
ui.spectra_inspector.load_spline(1)
ui.spectra_inspector.peak_viewer.show_what = "Local-CFT"
ui.spectra_inspector.width = 500
ui.spectra_inspector.height = 525
QtW.QApplication.processEvents()
ui.spectra_inspector.peak_viewer._upsample_and_update_image(45, 23)
ui.spectra_inspector.peak_viewer.canvas.xlim = (41, 49)
ui.spectra_inspector.peak_viewer.canvas.ylim = (18, 28)
QtW.QApplication.processEvents()
_imsave(ui.spectra_inspector.native, "inspect_local_cft_bad")

### magicgui widgets ###
for meth in [
ui.load_project,
# fit, CFT, etc.
Expand Down Expand Up @@ -142,10 +157,12 @@ def _workflow_gui(path: Path, tilt_range: tuple[float, float] = (-60, 60)):
_imsave(gui.native, method)
gui.close()

### canvas with monomers ###
ui.parent_viewer.dims.ndisplay = 3
_viewer_screenshot(ui, "viewer_01_monomer_mapped", canvas_only=True)
ui.parent_viewer.dims.ndisplay = 2

### magicgui widgets ###
for meth in [
ui.sta.average_all,
ui.sta.calculate_fsc,
Expand All @@ -171,6 +188,7 @@ def _workflow_gui(path: Path, tilt_range: tuple[float, float] = (-60, 60)):
_imsave(gui.native, meth.__name__)
gui.close()

### Bigger widgets ###
# Runner
ui._runner.show()
_imsave(ui._runner.native, "run_workflow_dialog")
Expand Down

0 comments on commit 9e52272

Please sign in to comment.