Skip to content

Commit

Permalink
Merge pull request #391 from quantumjot/fix-issue-389
Browse files Browse the repository at this point in the history
Clarify some variable names and save by file stem as default
  • Loading branch information
quantumjot authored Aug 9, 2023
2 parents a9609bc + b4f1be7 commit 7ef5d75
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
29 changes: 16 additions & 13 deletions btrack/napari/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from btrack.napari.config import TrackerConfigs

import logging
from pathlib import Path

import napari

Expand Down Expand Up @@ -76,7 +77,7 @@ def create_btrack_widget() -> btrack.napari.widgets.BtrackWidget:
),
)

btrack_widget.config.currentTextChanged.connect(
btrack_widget.config_name.currentTextChanged.connect(
lambda selected: select_config(btrack_widget, all_configs, selected),
)

Expand All @@ -88,7 +89,7 @@ def create_btrack_widget() -> btrack.napari.widgets.BtrackWidget:
lambda is_checked: btrack_widget._tabs.setTabEnabled(tab, is_checked)
)

btrack_widget.call_button.clicked.connect(
btrack_widget.track_button.clicked.connect(
lambda: run(btrack_widget, all_configs),
)

Expand Down Expand Up @@ -199,18 +200,15 @@ def select_config(
"""Set widget values from a newly-selected base config"""

# first update the previous config with the current widget values
previous_config_name = configs.current_config
previous_config = configs[previous_config_name]
previous_config = btrack.napari.sync.update_config_from_widgets(
unscaled_config=previous_config,
_ = btrack.napari.sync.update_config_from_widgets(
unscaled_config=configs[configs.current_config],
btrack_widget=btrack_widget,
)

# now load the newly-selected config and set widget values
configs.current_config = new_config_name
new_config = configs[new_config_name]
new_config = btrack.napari.sync.update_widgets_from_config(
unscaled_config=new_config,
_ = btrack.napari.sync.update_widgets_from_config(
unscaled_config=configs[new_config_name],
btrack_widget=btrack_widget,
)

Expand Down Expand Up @@ -239,7 +237,7 @@ def run(
)
return

unscaled_config = configs[btrack_widget.config.currentText()]
unscaled_config = configs[btrack_widget.config_name.currentText()]
unscaled_config = btrack.napari.sync.update_config_from_widgets(
unscaled_config=unscaled_config,
btrack_widget=btrack_widget,
Expand Down Expand Up @@ -349,13 +347,18 @@ def save_config_to_json(
logger.info(_msg)
return

unscaled_config = configs[btrack_widget.config.currentText()]
unscaled_config = configs[btrack_widget.config_name.currentText()]
btrack.napari.sync.update_config_from_widgets(
unscaled_config=unscaled_config,
btrack_widget=btrack_widget,
)
config = unscaled_config.scale_config()

# set the config name to match the filename
config.name = Path(save_path).stem
config.hypothesis_model.name = config.name
config.motion_model.name = config.name

btrack.config.save_config(save_path, config)


Expand All @@ -372,5 +375,5 @@ def load_config_from_json(
return

config_name = configs.add_config(filename=load_path, overwrite=False)
btrack_widget.config.addItem(config_name)
btrack_widget.config.setCurrentText(config_name)
btrack_widget.config_name.addItem(config_name)
btrack_widget.config_name.setCurrentText(config_name)
15 changes: 7 additions & 8 deletions btrack/napari/widgets/_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def create_logo_widgets() -> dict[str, QtWidgets.QWidget]:
def create_input_widgets() -> dict[str, tuple[str, QtWidgets.QWidget]]:
"""Create widgets for selecting labels layer and TrackerConfig"""

# TODO: annotation=napari.layers.Labels,
segmentation = QtWidgets.QComboBox()
segmentation.setToolTip(
"Select a 'Labels' layer to use for tracking.\n"
Expand All @@ -49,12 +48,12 @@ def create_input_widgets() -> dict[str, tuple[str, QtWidgets.QWidget]]:
)
widgets = {"segmentation": ("segmentation", segmentation)}

config = QtWidgets.QComboBox()
config.addItems(["cell", "particle"])
config.setToolTip(
config_name = QtWidgets.QComboBox()
config_name.addItems(["cell", "particle"])
config_name.setToolTip(
"Select a loaded configuration.\nNote, this will update values set below."
)
widgets["config"] = ("config name", config)
widgets["config_name"] = ("config name", config_name)

return widgets

Expand Down Expand Up @@ -152,9 +151,9 @@ def create_config_widgets() -> dict[str, QtWidgets.QWidget]:


def create_track_widgets() -> dict[str, QtWidgets.QWidget]:
call_button = QtWidgets.QPushButton("Track")
call_button.setToolTip(
track_button = QtWidgets.QPushButton("Track")
track_button.setToolTip(
"Run the tracking analysis with the current configuration.",
)

return {"call_button": call_button}
return {"track_button": track_button}
21 changes: 12 additions & 9 deletions tests/napari/test_dock_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,28 @@ def test_config_to_widgets_round_trip(track_widget, config):
assert json.loads(actual_config) == json.loads(expected_config)


def test_save_button(track_widget):
@pytest.mark.parametrize("filename", ["user_config"])
def test_save_button(track_widget, filename):
"""Tests that clicking the save configuration button
triggers a call to btrack.config.save_config with expected arguments.
"""

unscaled_config = btrack.napari.config.UnscaledTrackerConfig(
btrack.datasets.cell_config()
)
# this is done in in the gui too
unscaled_config.tracker_config.name = "cell"
# default config name matches the filename
unscaled_config.tracker_config.name = filename
unscaled_config.tracker_config.hypothesis_model.name = filename
unscaled_config.tracker_config.motion_model.name = filename
expected_config = unscaled_config.scale_config().json()

with patch(
"btrack.napari.widgets.save_path_dialogue_box"
) as save_path_dialogue_box:
save_path_dialogue_box.return_value = "user_config.json"
save_path_dialogue_box.return_value = f"{filename}.json"
track_widget.save_config_button.click()

actual_config = btrack.config.load_config("user_config.json").json()
actual_config = btrack.config.load_config(f"{filename}.json").json()

# use json.loads to avoid failure in string comparison because e.g "100.0" != "100"
assert json.loads(expected_config) == json.loads(actual_config)
Expand All @@ -78,7 +81,7 @@ def test_load_config(track_widget):
"""Tests that another TrackerConfig can be loaded and made the current config."""

# this is set to be 'cell' rather than 'Default'
original_config_name = track_widget.config.currentText()
original_config_name = track_widget.config_name.currentText()

with patch(
"btrack.napari.widgets.load_path_dialogue_box"
Expand All @@ -87,9 +90,9 @@ def test_load_config(track_widget):
track_widget.load_config_button.click()

# We didn't override the name, so it should be 'Default'
new_config_name = track_widget.config.currentText()
new_config_name = track_widget.config_name.currentText()

assert track_widget.config.currentText() == "Default"
assert track_widget.config_name.currentText() == "Default"
assert new_config_name != original_config_name


Expand Down Expand Up @@ -135,7 +138,7 @@ def test_run_button(track_widget, simplistic_tracker_outputs):
)

assert len(track_widget.viewer.layers) == OLD_WIDGET_LAYERS
track_widget.call_button.click()
track_widget.track_button.click()

assert run_tracker.called
assert len(track_widget.viewer.layers) == NEW_WIDGET_LAYERS
Expand Down

0 comments on commit 7ef5d75

Please sign in to comment.