Skip to content

Commit

Permalink
use methods from GeneralTools for getting filename (#1483)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: SACHIDANAND ALLE <[email protected]>
  • Loading branch information
hubutui and SachidanandAlle authored Oct 12, 2023
1 parent 0251684 commit c37627f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> names = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c37627f

Please sign in to comment.