From c37627f563bd83c1e6df7c444d06b823c3aef78e Mon Sep 17 00:00:00 2001 From: Butui Hu Date: Thu, 12 Oct 2023 23:21:40 +0800 Subject: [PATCH] use methods from GeneralTools for getting filename (#1483) 1. delete `Utils.getFileName` and `Utils.getNameWithoutExtension` 2. get filename by `GeneralTools.toPath` from URIs. 3. get file extension by `GeneralTools.getExtension(File)` this fix #1480 Signed-off-by: Butui Hu Co-authored-by: SACHIDANAND ALLE --- .../lib/extension/monailabel/InteractorTool.java | 8 +++++--- .../extension/monailabel/SegmentationTool.java | 8 +++++--- .../qupath/lib/extension/monailabel/Utils.java | 16 ---------------- .../monailabel/commands/NextSample.java | 8 +++++++- .../monailabel/commands/RunInference.java | 10 ++++++---- .../monailabel/commands/SubmitLabel.java | 11 ++++++----- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/InteractorTool.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/InteractorTool.java index d940ae824..e8c091379 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/InteractorTool.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/InteractorTool.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import javafx.scene.input.MouseEvent; +import qupath.lib.common.GeneralTools; import qupath.lib.extension.monailabel.MonaiLabelClient.ResponseInfo; import qupath.lib.extension.monailabel.commands.RunInference; import qupath.lib.gui.dialogs.Dialogs; @@ -59,9 +60,10 @@ public void mousePressed(MouseEvent e) { model = names.get(0); } - String imageFile = Utils.getFileName(viewer.getImageData().getServerPath()); - String im = imageFile.toLowerCase(); - boolean isWSI = (im.endsWith(".png") || im.endsWith(".jpg") || im.endsWith(".jpeg")) ? false : true; + var uris = viewer.getImageData().getServer().getURIs(); + String imageFile = GeneralTools.toPath(uris.iterator().next()).toString(); + String ext = GeneralTools.getExtension(imageFile).get().toLowerCase(); + boolean isWSI = !(ext.equals(".png") || ext.equals(".jpg") || ext.equals(".png")); logger.info("MONAILabel:: isWSI: " + isWSI + "; File: " + imageFile); if (model == null || model.isEmpty()) { diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/SegmentationTool.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/SegmentationTool.java index 3762f317b..94a579f9d 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/SegmentationTool.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/SegmentationTool.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import javafx.scene.input.MouseEvent; +import qupath.lib.common.GeneralTools; import qupath.lib.extension.monailabel.MonaiLabelClient.ResponseInfo; import qupath.lib.extension.monailabel.commands.RunInference; import qupath.lib.gui.dialogs.Dialogs; @@ -40,9 +41,10 @@ public void mouseReleased(MouseEvent e) { return; try { - String imageFile = Utils.getFileName(viewer.getImageData().getServerPath()); - String im = imageFile.toLowerCase(); - boolean isWSI = (im.endsWith(".png") || im.endsWith(".jpg") || im.endsWith(".jpeg")) ? false : true; + var uris = viewer.getImageData().getServer().getURIs(); + String imageFile = GeneralTools.toPath(uris.iterator().next()).toString(); + String ext = GeneralTools.getExtension(imageFile).get().toLowerCase(); + boolean isWSI = !(ext.equals(".png") || ext.equals(".jpg") || ext.equals(".png")); logger.info("MONAILabel:: isWSI: " + isWSI + "; File: " + imageFile); if (info == null) { diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/Utils.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/Utils.java index bd59981cd..7185e41dc 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/Utils.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/Utils.java @@ -13,7 +13,6 @@ package qupath.lib.extension.monailabel; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -25,21 +24,6 @@ public class Utils { - public static String getFileName(String file) { - if (file.indexOf("file:/") >= 0) - file = file.substring(file.indexOf("file:/") + "file:/".length()); - - int pos = file.indexOf("["); - file = file.substring(0, pos > 0 ? pos : file.length()); - return file; - } - - public static String getNameWithoutExtension(String file) { - String fileName = new File(file).getName(); - int dotIndex = fileName.lastIndexOf('.'); - return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex); - } - public static int[] getBBOX(ROI roi) { int x = 0, y = 0, w = 0, h = 0; if (roi != null && roi instanceof RectangleROI) { diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/NextSample.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/NextSample.java index e53428103..a27c1fb8a 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/NextSample.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/NextSample.java @@ -21,6 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import qupath.lib.common.GeneralTools; import qupath.lib.extension.monailabel.MonaiLabelClient; import qupath.lib.extension.monailabel.MonaiLabelClient.NextSampleInfo; import qupath.lib.extension.monailabel.MonaiLabelClient.ResponseInfo; @@ -50,7 +51,12 @@ public void run() { try { var viewer = qupath.getViewer(); var imageData = viewer.getImageData(); - String image = imageData != null ? Utils.getNameWithoutExtension(imageData.getServerPath()) : ""; + String image = ""; + if (imageData != null) { + var uris = imageData.getServer().getURIs(); + File imageFile = GeneralTools.toPath(uris.iterator().next()).toFile(); + image = GeneralTools.getNameWithoutExtension(imageFile); + } ResponseInfo info = MonaiLabelClient.info(); List names = new ArrayList(); diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/RunInference.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/RunInference.java index f40aaae99..827d64888 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/RunInference.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/RunInference.java @@ -35,6 +35,7 @@ import org.xml.sax.SAXException; import javafx.concurrent.Task; +import qupath.lib.common.GeneralTools; import qupath.lib.extension.monailabel.MonaiLabelClient; import qupath.lib.extension.monailabel.MonaiLabelClient.RequestInfer; import qupath.lib.extension.monailabel.MonaiLabelClient.ResponseInfo; @@ -84,9 +85,10 @@ public void run() { return; } - String imageFile = Utils.getFileName(viewer.getImageData().getServerPath()); - String im = imageFile.toLowerCase(); - boolean isWSI = !im.endsWith(".png") && !im.endsWith(".jpg") && !im.endsWith(".jpeg"); + var uris = imageData.getServer().getURIs(); + String imageFile = GeneralTools.toPath(uris.iterator().next()).toString(); + String ext = GeneralTools.getExtension(imageFile).get().toLowerCase(); + boolean isWSI = !(ext.equals(".png") || ext.equals(".jpg") || ext.equals(".png")); logger.info("MONAILabel:: isWSI: " + isWSI + "; File: " + imageFile); // Select first RectangleROI if not selected explicitly @@ -230,7 +232,7 @@ public static void runInference(String model, ResponseInfo info, int[] bbox, int ROI roi = ROIs.createRectangleROI(bbox[0], bbox[1], bbox[2], bbox[3], null); - String image = Utils.getNameWithoutExtension(imageFile); + String image = GeneralTools.getNameWithoutExtension(new File(imageFile)); String sessionId = null; int offsetX = 0; int offsetY = 0; diff --git a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/SubmitLabel.java b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/SubmitLabel.java index 9e50ba204..4df3f5074 100644 --- a/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/SubmitLabel.java +++ b/plugins/qupath/src/main/java/qupath/lib/extension/monailabel/commands/SubmitLabel.java @@ -37,6 +37,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; +import qupath.lib.common.GeneralTools; import qupath.lib.extension.monailabel.MonaiLabelClient; import qupath.lib.extension.monailabel.MonaiLabelClient.ImageInfo; import qupath.lib.extension.monailabel.Utils; @@ -67,11 +68,11 @@ public void run() { try { var viewer = qupath.getViewer(); var imageData = viewer.getImageData(); - String imageFile = Utils.getFileName(imageData.getServerPath()); - String image = Utils.getNameWithoutExtension(imageFile); - - String im = imageFile.toLowerCase(); - boolean isWSI = (im.endsWith(".png") || im.endsWith(".jpg") || im.endsWith(".jpeg")) ? false : true; + var uris = imageData.getServer().getURIs(); + String imageFile = GeneralTools.toPath(uris.iterator().next()).toString(); + String image = GeneralTools.getNameWithoutExtension(new File(imageFile)); + String ext = GeneralTools.getExtension(imageFile).get().toLowerCase(); + boolean isWSI = !(ext.equals(".png") || ext.equals(".jpg") || ext.equals(".png")); logger.info("MONAILabel:: isWSI: " + isWSI + "; File: " + imageFile); boolean validImage = MonaiLabelClient.imageExists(image);