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

490 finish settings for embedding widget #493

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions micro_sam/sam_annotator/_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import z5py

from qtpy import QtWidgets
from qtpy.QtCore import QObject, Signal
from qtpy.QtCore import QObject, Signal, QFileInfo
from superqt import QCollapsible
from magicgui import magic_factory
from magicgui.widgets import ComboBox, Container, create_widget
Expand Down Expand Up @@ -123,6 +123,58 @@ def _add_shape_param(self, names, values, min_val, max_val, step=1):

return x_param, y_param, layout

# # New method for directory selection
# def _add_directory_param(self, name, value, title=None):
# layout = QtWidgets.QHBoxLayout()
# layout.addWidget(QtWidgets.QLabel(name if title is None else title))

# directory_textbox = QtWidgets.QLineEdit()
# directory_textbox.setText(value)
# layout.addWidget(directory_textbox)

# directory_button = QtWidgets.QPushButton("Browse")
# directory_button.clicked.connect(lambda: self._get_directory(name, directory_textbox))
# layout.addWidget(directory_button)

# return layout
def _add_directory_param(self, name, value, title=None, select_file=False):
constantinpape marked this conversation as resolved.
Show resolved Hide resolved
layout = QtWidgets.QHBoxLayout()
layout.addWidget(QtWidgets.QLabel(name if title is None else title))

directory_textbox = QtWidgets.QLineEdit()
directory_textbox.setText(value)
layout.addWidget(directory_textbox)

button_text = "Browse Directory" if not select_file else "Browse File" # Adjust button text
constantinpape marked this conversation as resolved.
Show resolved Hide resolved
directory_button = QtWidgets.QPushButton(button_text)
# Call appropriate function based on select_file
directory_button.clicked.connect(lambda: getattr(self, "_get_{}_path".format(
constantinpape marked this conversation as resolved.
Show resolved Hide resolved
"directory" if not select_file else "file"))(name, directory_textbox))
layout.addWidget(directory_button)

return layout

def _get_directory(self, name, directory_textbox):
constantinpape marked this conversation as resolved.
Show resolved Hide resolved
directory = QtWidgets.QFileDialog.getExistingDirectory(
self, "Select Directory", "", QtWidgets.QFileDialog.ShowDirsOnly)
if directory:
path = Path(directory) # Create a Path object from the string

if path.is_dir(): # Check if it's a valid directory
directory_textbox.setText(directory)
setattr(self, name, path)
else:
# Handle the case where the selected path is not a directory
print("Invalid directory selected. Please try again.")

def _get_file_path(self, name, directory_textbox):
file_path, _ = QtWidgets.QFileDialog.getOpenFileName(
self, "Select File", "", "All Files (*)"
)
if file_path:
directory_textbox.setText(file_path)
setattr(self, name, file_path)


# Custom signals for managing progress updates.
class PBarSignals(QObject):
Expand Down Expand Up @@ -587,10 +639,18 @@ def _create_settings_widget(self):
# TODO
# save_path: Optional[Path] = None, # where embeddings for this image are cached (optional, zarr file = folder)
# custom_weights: Optional[Path] = None, # A filepath or URL to custom model weights.

# Create UI for the save path.
self.save_path = None
self.embeddings_save_path = None
layout = self._add_directory_param(
"embeddings_save_path", self.embeddings_save_path, title="embeddings save path:")
setting_values.layout().addLayout(layout)

# Create UI for the custom weights.
self.custom_weights = None
self.custom_weights = None # select_file
layout = self._add_directory_param(
"custom_weights", self.custom_weights, title="custom weights path:", select_file=True)
setting_values.layout().addLayout(layout)

# Create UI for the tile shape.
self.tile_x, self.tile_y = 0, 0
Expand Down Expand Up @@ -632,7 +692,7 @@ def __call__(self):

# Process tile_shape and halo, set other data.
tile_shape, halo = _process_tiling_inputs(self.tile_x, self.tile_y, self.halo_x, self.halo_y)
save_path = self.save_path
save_path = self.embeddings_save_path
image_data = image.data

# Set up progress bar and signals for using it within a threadworker.
Expand Down
2 changes: 1 addition & 1 deletion micro_sam/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def _compute_tiled_features_3d(predictor, input_, tile_shape, halo, f, pbar_init

def _compute_2d(input_, predictor, f, save_path, pbar_init, pbar_update):
# Check if the embeddings are already cached.
if save_path is not None and "input_size" in f.attrs:
if save_path is not None and "features" in f.attrs:
constantinpape marked this conversation as resolved.
Show resolved Hide resolved
# In this case we load the embeddings..
features = f["features"][:]
original_size, input_size = f.attrs["original_size"], f.attrs["input_size"]
Expand Down