Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

45 Move the view of the viewbox to a given position #57

Merged
merged 11 commits into from
Jul 20, 2024
Merged
44 changes: 43 additions & 1 deletion cellpose/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,48 @@ def minimap_closed(self):
if hasattr(self, 'minimap'):
del self.minimap

def center_view_on_position(self, normalized_x, normalized_y):
"""
Centers the view on the given normalized coordinates (x, y).
This will be used to navigate using the minimap window.
The zoom level will be maintained.

Args:
normalized_x (float): Normalized x-coordinate (0.0 to 1.0).
normalized_y (float): Normalized y-coordinate (0.0 to 1.0).
"""
# Get the size of the image
img_height = self.img.image.shape[0]
img_width = self.img.image.shape[1]

# Calculate the actual pixel coordinates
target_x = normalized_x * img_width
target_y = normalized_y * img_height

# Get the current view range of the viewbox.
# This tells us which part of the image is currently displayed.
view_range = self.p0.viewRange()
mariehartung marked this conversation as resolved.
Show resolved Hide resolved

# Extract the x and y ranges
x_range = view_range[0]
y_range = view_range[1]

# Calculate the current zoom level
zoom_x = (x_range[1] - x_range[0]) / img_width
zoom_y = (y_range[1] - y_range[0]) / img_height

# Calculate the width and height of the view range based on the zoom level
view_width = img_width * zoom_x
view_height = img_height * zoom_y

# Calculate the new view range, centered on the target position
new_x_range = [target_x - view_width / 2, target_x + view_width / 2]
new_y_range = [target_y - view_height / 2, target_y + view_height / 2]

# Set the new view range to the ViewBox
self.p0.setXRange(*new_x_range, padding=0)
self.p0.setYRange(*new_y_range, padding=0)


def make_buttons(self):
self.boldfont = QtGui.QFont("Arial", 11, QtGui.QFont.Bold)
Expand Down Expand Up @@ -1833,7 +1875,7 @@ def update_plot(self):

self.win.show()
self.show()


def update_layer(self):
if self.masksOn or self.outlinesOn:
Expand Down
11 changes: 10 additions & 1 deletion cellpose/gui/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import fastremap

from ..io import imread, imsave, outlines_to_text, add_model, remove_model, save_rois
from ..io import imread, imsave, outlines_to_text, add_model, remove_model, save_rois, save_settings
from ..models import normalize_default, MODEL_DIR, MODEL_LIST_PATH, get_user_models
from ..utils import masks_to_outlines, outlines_list
from . import guiparts, gui
Expand Down Expand Up @@ -619,6 +619,15 @@ def _save_outlines(parent):
else:
print("ERROR: cannot save 3D outlines")

def _save_settings(parent):
""" save settings to json file"""
filename = parent.filename
base = os.path.splitext(filename)[0]
if parent.NZ == 1:
print("GUI_INFO: saving Settings to json file")
save_settings(parent.filename)
else:
print("ERROR: cannot save settings")

def _save_sets_with_check(parent):
""" Save masks and update *_seg.npy file. Use this function when saving should be optional
Expand Down
10 changes: 7 additions & 3 deletions cellpose/gui/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ def mainmenu(parent):
parent.minimapWindow.setChecked(False)
parent.minimapWindow.triggered.connect(parent.minimap_window)
file_menu.addAction(parent.minimapWindow)


parent.saveSettings = QAction("Save Settings as .&json", parent)
parent.saveSettings.setShortcut("Ctrl+J")
parent.saveSettings.triggered.connect(lambda: io._save_settings(parent))
file_menu.addAction(parent.saveSettings)
parent.saveSettings.setEnabled(True)

def editmenu(parent):
main_menu = parent.menuBar()
edit_menu = main_menu.addMenu("&Edit")
Expand Down Expand Up @@ -155,5 +161,3 @@ def helpmenu(parent):
openTrainHelp = QAction("Training instructions", parent)
openTrainHelp.triggered.connect(parent.train_help_window)
help_menu.addAction(openTrainHelp)


15 changes: 14 additions & 1 deletion cellpose/io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Copyright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
"""

import json
import os, datetime, gc, warnings, glob, shutil
from natsort import natsorted
import numpy as np
Expand Down Expand Up @@ -590,7 +590,20 @@ def save_rois(masks, file_name):

roiwrite(file_name, rois)

def save_settings(file_name):
""" save settings to .json file and remove if it already exists

Args:
file_name (str): name to save the .json file to

Returns:
None
"""
file_name = os.path.splitext(file_name)[0] + "_cp_settings.json"
if os.path.exists(file_name):
os.remove(file_name)
with open(file_name, 'w') as f:
json.dump({}, f)

def save_masks(images, masks, flows, file_names, png=True, tif=False, channels=[0, 0],
suffix="", save_flows=False, save_outlines=False,
Expand Down