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

Improve handling of channel numbers in UI to prevent spurious errors #50

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions src/main/java/qupath/ext/instanseg/core/InstanSegModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ private Map<String, Double> getPixelSize() {
return map;
}

public int getNumChannels() {
assert getModel().getInputs().getFirst().getAxes().equals("bcyx");
int numChannels = getModel().getInputs().getFirst().getShape().getShapeMin()[1];
if (getModel().getInputs().getFirst().getShape().getShapeStep()[1] == 1) {
numChannels = Integer.MAX_VALUE;
}
return numChannels;
}

private void fetchModel() {
if (modelURL == null) {
throw new NullPointerException("Model URL should not be null for a local model!");
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/qupath/ext/instanseg/ui/InstanSegController.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ void promptForModelDirectory() {
private void configureChannelPicker() {
updateChannelPicker(qupath.getImageData());
qupath.imageDataProperty().addListener((v, o, n) -> updateChannelPicker(n));
comboChannels.disableProperty().bind(qupath.imageDataProperty().isNull());
comboChannels.setTitle(getCheckComboBoxText(comboChannels));
comboChannels.getItems().addListener((ListChangeListener<ChannelSelectItem>) c -> {
comboChannels.setTitle(getCheckComboBoxText(comboChannels));
Expand All @@ -170,13 +169,24 @@ private void configureChannelPicker() {
addSetFromVisible(comboChannels);
}


private void updateChannelPicker(ImageData<BufferedImage> imageData) {
if (imageData == null) {
return;
}
comboChannels.getCheckModel().clearChecks();
comboChannels.getItems().clear();
comboChannels.getItems().setAll(getAvailableChannels(imageData));
comboChannels.getCheckModel().checkIndices(IntStream.range(0, imageData.getServer().nChannels()).toArray());
if (imageData.isBrightfield()) {
comboChannels.getCheckModel().checkIndices(IntStream.range(0, 3).toArray());
var model = modelChoiceBox.getSelectionModel().selectedItemProperty().get();
if (model != null && model.getNumChannels() != Integer.MAX_VALUE) {
comboChannels.getCheckModel().clearChecks();
comboChannels.getCheckModel().checkIndices(0, 1, 2);
}
} else {
comboChannels.getCheckModel().checkIndices(IntStream.range(0, imageData.getServer().nChannels()).toArray());
}
}

private static String getCheckComboBoxText(CheckComboBox<ChannelSelectItem> comboBox) {
Expand Down Expand Up @@ -279,6 +289,15 @@ private void configureRunning() {
.or(modelChoiceBox.getSelectionModel().selectedItemProperty().isNull())
.or(messageTextHelper.hasWarning())
.or(deviceChoices.getSelectionModel().selectedItemProperty().isNull())
.or(Bindings.createBooleanBinding(() -> {
var model = modelChoiceBox.getSelectionModel().selectedItemProperty().get();
if (model == null) {
return true;
}
int numSelected = comboChannels.getCheckModel().getCheckedIndices().size();
int numAllowed = model.getNumChannels();
return !(numSelected == numAllowed || numAllowed == Integer.MAX_VALUE);
}, modelChoiceBox.getSelectionModel().selectedItemProperty()))
);
pendingTask.addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
Expand All @@ -292,6 +311,13 @@ private void configureModelChoices() {
tfModelDirectory.textProperty().bindBidirectional(InstanSegPreferences.modelDirectoryProperty());
handleModelDirectory(tfModelDirectory.getText());
tfModelDirectory.textProperty().addListener((v, o, n) -> handleModelDirectory(n));
// for brightfield models, we want to disable the picker and set it to use RGB only
modelChoiceBox.getSelectionModel().selectedItemProperty().addListener((v, o, n) -> {
if (qupath.getImageData().isBrightfield() && n != null && n.getNumChannels() != Integer.MAX_VALUE) {
comboChannels.getCheckModel().clearChecks();
comboChannels.getCheckModel().checkIndices(0, 1, 2);
}
});
}

private static void addRemoteModels(ComboBox<InstanSegModel> comboBox) {
Expand Down Expand Up @@ -352,7 +378,7 @@ private void addDeviceChoices() {
}

private void configureMessageLabel() {
messageTextHelper = new MessageTextHelper(modelChoiceBox, deviceChoices);
messageTextHelper = new MessageTextHelper(modelChoiceBox, deviceChoices, comboChannels);
labelMessage.textProperty().bind(messageTextHelper.messageLabelText());
if (messageTextHelper.hasWarning().get()) {
labelMessage.getStyleClass().setAll("warning-message");
Expand Down Expand Up @@ -455,7 +481,6 @@ protected Void call() {
.build();
instanSeg.detectObjects(selectedObjects);


String cmd = String.format("""
import qupath.ext.instanseg.core.InstanSeg
import static qupath.lib.gui.scripting.QPEx.*
Expand Down Expand Up @@ -499,8 +524,7 @@ protected Void call() {
if (model.nFailed() > 0) {
var errorMessage = String.format(resources.getString("error.tiles-failed"), model.nFailed());
logger.error(errorMessage);
Dialogs.showErrorMessage(resources.getString("title"),
errorMessage);
Dialogs.showErrorMessage(resources.getString("title"), errorMessage);
}
return null;
}
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/qupath/ext/instanseg/ui/MessageTextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ChoiceBox;
import org.controlsfx.control.CheckComboBox;
import org.controlsfx.control.SearchableComboBox;
import qupath.ext.instanseg.core.InstanSegModel;
import qupath.lib.gui.QuPathGUI;
Expand All @@ -32,6 +33,7 @@ class MessageTextHelper {
private final SelectedObjectCounter selectedObjectCounter;
private final SearchableComboBox<InstanSegModel> modelChoiceBox;
private final ChoiceBox<String> deviceChoiceBox;
private final CheckComboBox<ChannelSelectItem> comboChannels;

/**
* Text to display a warning (because inference can't be run)
Expand All @@ -53,9 +55,10 @@ class MessageTextHelper {
*/
private BooleanBinding hasWarning;

MessageTextHelper(SearchableComboBox<InstanSegModel> modelChoiceBox, ChoiceBox<String> deviceChoiceBox) {
MessageTextHelper(SearchableComboBox<InstanSegModel> modelChoiceBox, ChoiceBox<String> deviceChoiceBox, CheckComboBox<ChannelSelectItem> comboChannels) {
this.modelChoiceBox = modelChoiceBox;
this.deviceChoiceBox = deviceChoiceBox;
this.comboChannels = comboChannels;
this.selectedObjectCounter = new SelectedObjectCounter(qupath.imageDataProperty());
configureMessageTextBindings();
}
Expand Down Expand Up @@ -104,10 +107,10 @@ private StringBinding createWarningTextBinding() {
return Bindings.createStringBinding(this::getWarningText,
qupath.imageDataProperty(),
modelChoiceBox.getSelectionModel().selectedItemProperty(),
comboChannels.getCheckModel().getCheckedItems(),
deviceChoiceBox.getSelectionModel().selectedItemProperty(),
selectedObjectCounter.numSelectedAnnotations,
selectedObjectCounter.numSelectedTMACores,
selectedObjectCounter.numSelectedDetections);
selectedObjectCounter.numSelectedTMACores);
}

private String getWarningText() {
Expand All @@ -116,11 +119,20 @@ private String getWarningText() {
if (modelChoiceBox.getSelectionModel().isEmpty())
return resources.getString("ui.error.no-model");
if (selectedObjectCounter.numSelectedAnnotations.get() == 0 &&
selectedObjectCounter.numSelectedDetections.get() == 0 &&
selectedObjectCounter.numSelectedTMACores.get() == 0)
return resources.getString("ui.error.no-selection");
if (deviceChoiceBox.getSelectionModel().isEmpty())
return resources.getString("ui.error.no-device");
int modelChannels = modelChoiceBox.getSelectionModel().getSelectedItem().getNumChannels();
int selectedChannels = comboChannels.getCheckModel().getCheckedItems().size();
if (modelChannels != Integer.MAX_VALUE) {
if (modelChannels != selectedChannels) {
return String.format(
resources.getString("ui.error.num-channels-dont-match"),
modelChannels,
selectedChannels);
}
}
return null;
}

Expand Down
51 changes: 38 additions & 13 deletions src/main/resources/qupath/ext/instanseg/ui/instanseg_control.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<?import javafx.scene.text.*?>
<?import org.controlsfx.control.*?>

<?import javafx.geometry.Insets?>
<fx:root type="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="330" stylesheets="@instanseg.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1">
<center>
<VBox fx:id="vBox">
Expand Down Expand Up @@ -86,19 +87,28 @@
<TitledPane animated="false" maxHeight="Infinity" text="%ui.options.pane" VBox.vgrow="NEVER">
<VBox alignment="TOP_CENTER" spacing="7.5" styleClass="standard-padding">
<children>

<!-- Device Selection-->
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" top="10"/>
</padding>
<children>
<Label styleClass="regular" text="%ui.options.device" />
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<ChoiceBox fx:id="deviceChoices">
<tooltip><Tooltip text="%ui.options.device.tooltip" /></tooltip>
</ChoiceBox>
</children>
</HBox>
<!-- Thread Selection-->
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" />
</padding>
<children>
<Label styleClass="regular" text="%ui.options.threads" />
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<Spinner fx:id="threadSpinner" prefWidth="75.0">
<tooltip>
<Tooltip text="%ui.options.threads.tooltip" />
Expand All @@ -109,20 +119,28 @@
</Spinner>
</children>
</HBox>
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" />
</padding>
<children>
<Label styleClass="regular" text="%ui.options.nuclei-only" />
<CheckBox fx:id="nucleiOnlyCheckBox" prefWidth="75.0">
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<CheckBox fx:id="nucleiOnlyCheckBox">
<tooltip>
<Tooltip text="%ui.options.nuclei-only.tooltip" />
</tooltip>
</CheckBox>
</children>
</HBox>
<!-- Tile Size Selection-->
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" />
</padding>
<children>
<Label styleClass="regular" text="%ui.options.tilesize" />
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<ChoiceBox prefWidth="75.0" fx:id="tileSizeChoiceBox">
<tooltip>
<Tooltip text="%ui.options.tilesize.tooltip" />
Expand All @@ -131,25 +149,37 @@
</children>
</HBox>
<!-- Channel Selection-->
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" />
</padding>
<children>
<Label styleClass="regular" text="%ui.options.channel" />
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<CheckComboBox fx:id="comboChannels">
<tooltip>
<Tooltip text="%ui.options.channel.tooltip" />
</tooltip>
</CheckComboBox>
</children>
</HBox>
<HBox alignment="CENTER" styleClass="standard-spacing">
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets left="10" right="10" bottom="10"/>
</padding>
<children>
<CheckBox styleClass="regular" fx:id="makeMeasurementsCheckBox" text="%ui.options.makeMeasurements">
<Label text="%ui.options.makeMeasurements"/>
<Pane minWidth="5" HBox.hgrow="ALWAYS"/>
<CheckBox styleClass="regular" fx:id="makeMeasurementsCheckBox">
<tooltip>
<Tooltip text="%ui.options.makeMeasurements.tooltip" />
</tooltip>
</CheckBox>
</children>
</HBox>



<Separator prefWidth="200.0" />

<!-- Model Directory Selection-->
Expand All @@ -175,11 +205,6 @@
</HBox>
</children>
</VBox>
<VBox alignment="CENTER" styleClass="standard-spacing">
<children>
<HBox styleClass="standard-spacing" />
</children>
</VBox>
</children>
</VBox>
</TitledPane>
Expand Down
13 changes: 7 additions & 6 deletions src/main/resources/qupath/ext/instanseg/ui/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ui.error.no-selection = No annotation, TMA core, or detection selected
ui.error.no-model = No model selected
ui.error.no-device = No device selected
ui.error.no-image = No image selected
ui.error.num-channels-dont-match = Model requires %d channels, input has %d

ui.selection.annotations-single = 1 annotation selected
ui.selection.annotations-multiple = %d annotations selected
Expand All @@ -45,22 +46,22 @@ ui.selection.empty = No valid objects selected

# Hardware tab
ui.options.pane = Additional Options
ui.options.device = Preferred device:
ui.options.device = Preferred device
ui.options.device.tooltip = Select the preferred device for model running (choose CPU if other options are not available)
ui.options.nuclei-only = Nuclei only:
ui.options.nuclei-only = Nuclei only
ui.options.nuclei-only.tooltip = Segment nuclei only, or nuclei and cell boundaries
ui.options.threads = Threads:
ui.options.threads = Threads
ui.options.threads.tooltip = Define the preferred number of threads
ui.options.tilesize = Tile Size:
ui.options.tilesize = Tile size
ui.options.tilesize.tooltip = Define the approximate tile size
ui.options.channel = Channels:
ui.options.channel = Channels
ui.options.channel.tooltip = Define the set of channels to be used in inference
ui.options.makeMeasurements = Make measurements
ui.options.makeMeasurements.tooltip = Make shape and intensity measurements after detecting objects
ui.options.noChannelSelected = No channels selected!
ui.options.oneChannelSelected = 1 channel selected
ui.options.nChannelSelected = %d channels selected
ui.options.directory = Downloaded model directory:
ui.options.directory = Downloaded model directory
ui.options.directory.tooltip = Choose the directory where models should be stored

# Model directories
Expand Down
Loading