Skip to content

Commit

Permalink
Add random colors checkbox
Browse files Browse the repository at this point in the history
Also make random colors and measurement preferences persistent.
  • Loading branch information
petebankhead committed Sep 5, 2024
1 parent 3ec93b1 commit c98dd6e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/main/java/qupath/ext/instanseg/ui/InstanSegController.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public class InstanSegController extends BorderPane {
@FXML
private CheckBox makeMeasurementsCheckBox;
@FXML
private CheckBox randomColorsCheckBox;
@FXML
private Button infoButton;
@FXML
private Label modelDirLabel;
Expand Down Expand Up @@ -179,6 +181,7 @@ private InstanSegController(QuPathGUI qupath) throws IOException {
);
configureChannelPicker();
configureOutputChannelCombo();
configureDefaultValues();
}

private void configureOutputChannelCombo() {
Expand All @@ -197,6 +200,11 @@ else if (list.size() == 1) {
});
}

private void configureDefaultValues() {
makeMeasurementsCheckBox.selectedProperty().bindBidirectional(InstanSegPreferences.makeMeasurementsProperty());
randomColorsCheckBox.selectedProperty().bindBidirectional(InstanSegPreferences.randomColorsProperty());
}

private BooleanBinding createModelDownloadedBinding() {
return Bindings.createBooleanBinding(
() -> {
Expand Down Expand Up @@ -373,7 +381,7 @@ private static Collection<ChannelSelectItem> getAvailableChannels(ImageData<?> i
private void configureThreadSpinner() {
SpinnerValueFactory.IntegerSpinnerValueFactory factory = (SpinnerValueFactory.IntegerSpinnerValueFactory) threadSpinner.getValueFactory();
factory.setMax(Runtime.getRuntime().availableProcessors());
threadSpinner.getValueFactory().valueProperty().bindBidirectional(InstanSegPreferences.numThreadsProperty());
threadSpinner.getValueFactory().valueProperty().bindBidirectional(InstanSegPreferences.numThreadsProperty().asObject());
}

private void configureRunning() {
Expand Down Expand Up @@ -767,6 +775,8 @@ protected Void call() {
if (nChecked > 0 && nChecked < nOutputs) {
outputChannels = checkComboOutputs.getCheckModel().getCheckedIndices().stream().mapToInt(Integer::intValue).toArray();
}
boolean makeMeasurements = makeMeasurementsCheckBox.isSelected();
boolean randomColors = randomColorsCheckBox.isSelected();

var instanSeg = InstanSeg.builder()
.model(model)
Expand All @@ -775,10 +785,10 @@ protected Void call() {
.outputChannels(outputChannels)
.tileDims(InstanSegPreferences.tileSizeProperty().get())
.taskRunner(taskRunner)
.makeMeasurements(makeMeasurementsCheckBox.isSelected())
.makeMeasurements(makeMeasurements)
.randomColors(randomColors)
.build();

boolean makeMeasurements = makeMeasurementsCheckBox.isSelected();
String cmd = String.format("""
qupath.ext.instanseg.core.InstanSeg.builder()
.modelPath("%s")
Expand All @@ -788,6 +798,7 @@ protected Void call() {
.tileDims(%d)
.nThreads(%d)
.makeMeasurements(%s)
.randomColors(%s)
.build()
.detectObjects()
""",
Expand All @@ -799,7 +810,8 @@ protected Void call() {
.collect(Collectors.joining(", ")),
InstanSegPreferences.tileSizeProperty().get(),
InstanSegPreferences.numThreadsProperty().getValue(),
makeMeasurements
makeMeasurements,
randomColors
).strip();
InstanSegResults results = instanSeg.detectObjects(imageData, selectedObjects);
imageData.getHierarchy().fireHierarchyChangedEvent(this);
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/qupath/ext/instanseg/ui/InstanSegPreferences.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package qupath.ext.instanseg.ui;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.Property;
Expand Down Expand Up @@ -32,14 +33,22 @@ enum OnlinePermission {
"instanseg.pref.device",
getDefaultDevice());

private static final Property<Integer> numThreadsProperty = PathPrefs.createPersistentPreference(
private static final IntegerProperty numThreadsProperty = PathPrefs.createPersistentPreference(
"instanseg.num.threads",
Math.min(4, Runtime.getRuntime().availableProcessors())).asObject();
Math.min(4, Runtime.getRuntime().availableProcessors()));

private static final IntegerProperty tileSizeProperty = PathPrefs.createPersistentPreference(
"intanseg.tile.size",
512);

private static final BooleanProperty makeMeasurementsProperty = PathPrefs.createPersistentPreference(
"intanseg.measurements",
true);

private static final BooleanProperty randomColorsProperty = PathPrefs.createPersistentPreference(
"intanseg.random.colors",
true);

/**
* MPS should work reliably (and much faster) on Apple Silicon, so set as default.
* Everywhere else, use CPU as we can't count on a GPU/CUDA being available.
Expand All @@ -65,11 +74,20 @@ static StringProperty preferredDeviceProperty() {
return preferredDeviceProperty;
}

static Property<Integer> numThreadsProperty() {
static IntegerProperty numThreadsProperty() {
return numThreadsProperty;
}

static IntegerProperty tileSizeProperty() {
return tileSizeProperty;
}

static BooleanProperty makeMeasurementsProperty() {
return makeMeasurementsProperty;
}

static BooleanProperty randomColorsProperty() {
return randomColorsProperty;
}

}
19 changes: 17 additions & 2 deletions src/main/resources/qupath/ext/instanseg/ui/instanseg_control.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<children>
<Label styleClass="regular" text="%ui.options.device" />
<Pane minWidth="5" HBox.hgrow="ALWAYS" />
<ChoiceBox fx:id="deviceChoices">
<ChoiceBox fx:id="deviceChoices" prefWidth="75.0">
<tooltip><Tooltip text="%ui.options.device.tooltip" /></tooltip>
</ChoiceBox>
</children>
Expand Down Expand Up @@ -180,7 +180,7 @@
</HBox>
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets bottom="10" left="10" right="10" />
<Insets left="10.0" right="10.0" />
</padding>
<children>
<Label text="%ui.options.makeMeasurements" />
Expand All @@ -192,6 +192,21 @@
</CheckBox>
</children>
</HBox>
<HBox alignment="CENTER_RIGHT" styleClass="standard-spacing">
<padding>
<Insets bottom="5.0" left="10.0" right="10.0" />
</padding>
<children>
<Label text="%ui.options.randomColors">
</Label>
<Pane minWidth="5" HBox.hgrow="ALWAYS" />
<CheckBox fx:id="randomColorsCheckBox" styleClass="regular">
<tooltip>
<Tooltip text="%ui.options.randomColors.tooltip" />
</tooltip>
</CheckBox>
</children>
</HBox>



Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/qupath/ext/instanseg/ui/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ ui.options.input-channels = Input channels
ui.options.input-channels.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.randomColors = Random colors
ui.options.randomColors.tooltip = Assign random colors to detected objects
ui.options.noChannelSelected = No channels selected!
ui.options.oneChannelSelected = 1 channel selected
ui.options.nChannelSelected = %d channels selected
Expand Down

0 comments on commit c98dd6e

Please sign in to comment.