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

Fix channel selector when duplicate channels present (resolve #11) #13

Merged
merged 9 commits into from
Apr 25, 2024
27 changes: 15 additions & 12 deletions src/main/java/qupath/ext/instanseg/ui/InstanSegController.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -153,7 +152,7 @@ private void addSetFromVisible(CheckComboBox<ColorTransforms.ColorTransform> com
var channelNames = activeChannels.stream().map(ChannelDisplayInfo::getName).toList();
var comboItems = comboChannels.getItems();
for (int i = 0; i < comboItems.size(); i++) {
if (channelNames.contains(comboItems.get(i).getName() + " (C" + (i+1) + ")")) {
if (channelNames.contains(comboItems.get(i).getName())) {
comboChannels.getCheckModel().check(i);
}
}
Expand All @@ -178,7 +177,7 @@ private void updateChannelPicker(ImageData<BufferedImage> imageData) {

private static Collection<ColorTransforms.ColorTransform> getAvailableChannels(ImageData<?> imageData) {
List<ColorTransforms.ColorTransform> list = new ArrayList<>();
for (var name : getAvailableUniqueChannelNames(imageData.getServer()))
for (var name : getAvailableChannelNames(imageData.getServer()))
list.add(ColorTransforms.createChannelExtractor(name));
var stains = imageData.getColorDeconvolutionStains();
if (stains != null) {
Expand All @@ -190,19 +189,23 @@ private static Collection<ColorTransforms.ColorTransform> getAvailableChannels(I
}

/**
* Create a collection representing available unique channel names, logging a warning if a channel name is duplicated
* @param server server containing channels
* @return set of channel names
* Create a collection representing available (possibly duplicate)
* channel names, logging a warning if a channel name is duplicated.
* @param server server that contains channels
* @return Collection of channel names
*/
private static Collection<String> getAvailableUniqueChannelNames(ImageServer<?> server) {
var set = new LinkedHashSet<String>();
private static Collection<String> getAvailableChannelNames(ImageServer<?> server) {
var set = new ArrayList<String>();
int i = 1;
for (var c : server.getMetadata().getChannels()) {
var name = c.getName();
if (!set.contains(name))
set.add(name);
else
logger.warn("Found duplicate channel name! Will skip channel " + i + " (name '" + name + "')");
if (server.isRGB()) {
alanocallaghan marked this conversation as resolved.
Show resolved Hide resolved
name += "(C" + i + ")";
}
set.add(name);
if (set.contains(name)) {
logger.warn("Found duplicate channel name! Channel " + i + " (name '" + name + "')");
}
i++;
}
return set;
Expand Down
Loading