From 99c8539e0fd5db50475d36e935537f3664ef46cf Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Fri, 15 Nov 2024 12:01:49 +0000 Subject: [PATCH 1/4] Remove sout --- src/main/java/qupath/ext/instanseg/core/InstanSegModel.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java index 1b53975..ffb6373 100644 --- a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java +++ b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java @@ -193,7 +193,6 @@ public double getPreferredDownsample(PixelCalibration cal) { * @return The exact downsample, unless it's close to an integer, in which case the integer. */ static double getPreferredDownsample(double currentPixelSize, double requestedPixelSize) { - System.out.println(requestedPixelSize); double downsample = requestedPixelSize / currentPixelSize; double downsampleRounded = Math.round(downsample); if (GeneralTools.almostTheSame(downsample, Math.round(downsample), 0.01)) { From d0de35c713d6588fd81c3f12c97a26fc4e30eac1 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Fri, 3 Jan 2025 15:25:18 +0000 Subject: [PATCH 2/4] Resolve issue with fake model downloads mentioned in #118 --- .../qupath/ext/instanseg/core/InstanSegModel.java | 11 ++++++----- .../qupath/ext/instanseg/ui/InstanSegController.java | 10 +++++++++- .../qupath/ext/instanseg/ui/strings.properties | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java index 3b35262..c8cf55e 100644 --- a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java +++ b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java @@ -92,14 +92,15 @@ public boolean isValid() { * Trigger a download for a model * @throws IOException If an error occurs when downloading, unzipping, etc. */ - public void download(Path downloadedModelDir) throws IOException { + public void checkIfDownloaded(Path downloadedModelDir, boolean downloadIfNotValid) throws IOException { if (path != null && isValidModel(path) && model != null) { return; } - var zipFile = downloadZipIfNeeded( + var zipFile = checkZipExists( this.modelURL, downloadedModelDir, - getFolderName(name, version)); + getFolderName(name, version), + downloadIfNotValid); this.path = unzipIfNeeded(zipFile); this.model = BioimageIoSpec.parseModel(path.toFile()); this.version = model.getVersion(); @@ -266,10 +267,10 @@ private Optional getModel() { return Optional.ofNullable(model); } - private static Path downloadZipIfNeeded(URL url, Path downloadDirectory, String filename) throws IOException { + private static Path checkZipExists(URL url, Path downloadDirectory, String filename, boolean downloadIfNot) throws IOException { Files.createDirectories(downloadDirectory); var zipFile = downloadDirectory.resolve(filename + ".zip"); - if (!isDownloadedAlready(zipFile)) { + if (!isDownloadedAlready(zipFile) && downloadIfNot) { try (InputStream stream = url.openStream()) { try (ReadableByteChannel readableByteChannel = Channels.newChannel(stream)) { try (FileOutputStream fos = new FileOutputStream(zipFile.toFile())) { diff --git a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java index 09220ca..7d22e4b 100644 --- a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java +++ b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java @@ -486,6 +486,14 @@ private void refreshModelChoice() { return; var modelDir = InstanSegUtils.getModelDirectory().orElse(null); + try { + System.out.println(model.isValid()); + model.checkIfDownloaded(modelDir.resolve("downloaded"), false); + System.out.println(model.isValid()); + } catch (IOException e) { + logger.warn("Downloaded model is missing zip or RDF file(s); this shouldn't happen", e); + Dialogs.showErrorNotification(resources.getString("title"), resources.getString("error.checkingModel")); + } boolean isDownloaded = modelDir != null && model.isValid(); if (!isDownloaded || qupath.getImageData() == null) { return; @@ -543,7 +551,7 @@ private InstanSegModel downloadModel(InstanSegModel model, Path modelDir) { try { Dialogs.showInfoNotification(resources.getString("title"), String.format(resources.getString("ui.popup.fetching"), model.getName())); - model.download(modelDir.resolve("downloaded")); + model.checkIfDownloaded(modelDir.resolve("downloaded"), true); Dialogs.showInfoNotification(resources.getString("title"), String.format(resources.getString("ui.popup.available"), model.getName())); FXUtils.runOnApplicationThread(() -> { diff --git a/src/main/resources/qupath/ext/instanseg/ui/strings.properties b/src/main/resources/qupath/ext/instanseg/ui/strings.properties index 1c7026c..9f8b3e2 100644 --- a/src/main/resources/qupath/ext/instanseg/ui/strings.properties +++ b/src/main/resources/qupath/ext/instanseg/ui/strings.properties @@ -115,4 +115,4 @@ error.querying-local = Error querying local files error.localModel = Can't find file in user model directory error.tiles-failed = %d tiles failed. This could be a memory issue.\nConsider decreasing tile size or the number of threads used. error.modelPath = Unable to fetch model path - +error.checkingModel = Downloaded model is missing zip or RDF file(s).\n Try deleting files from your downloaded model directory and retrying. From 59eed24c9ae3cc1583554ddf406c9a65d3378b93 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Fri, 3 Jan 2025 15:29:03 +0000 Subject: [PATCH 3/4] Remove sout --- src/main/java/qupath/ext/instanseg/ui/InstanSegController.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java index 7d22e4b..0471f1f 100644 --- a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java +++ b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java @@ -487,9 +487,7 @@ private void refreshModelChoice() { var modelDir = InstanSegUtils.getModelDirectory().orElse(null); try { - System.out.println(model.isValid()); model.checkIfDownloaded(modelDir.resolve("downloaded"), false); - System.out.println(model.isValid()); } catch (IOException e) { logger.warn("Downloaded model is missing zip or RDF file(s); this shouldn't happen", e); Dialogs.showErrorNotification(resources.getString("title"), resources.getString("error.checkingModel")); From f62561955af574e0f53eb56191ee36aaaf460475 Mon Sep 17 00:00:00 2001 From: Alan O'Callaghan Date: Fri, 3 Jan 2025 15:39:04 +0000 Subject: [PATCH 4/4] Properly handle case of non-downloaded model --- .../qupath/ext/instanseg/core/InstanSegModel.java | 15 +++++++++++---- .../ext/instanseg/ui/InstanSegController.java | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java index c8cf55e..d887661 100644 --- a/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java +++ b/src/main/java/qupath/ext/instanseg/core/InstanSegModel.java @@ -102,8 +102,10 @@ public void checkIfDownloaded(Path downloadedModelDir, boolean downloadIfNotVali getFolderName(name, version), downloadIfNotValid); this.path = unzipIfNeeded(zipFile); - this.model = BioimageIoSpec.parseModel(path.toFile()); - this.version = model.getVersion(); + if (this.path != null) { + this.model = BioimageIoSpec.parseModel(path.toFile()); + this.version = model.getVersion(); + } } /** @@ -270,7 +272,10 @@ private Optional getModel() { private static Path checkZipExists(URL url, Path downloadDirectory, String filename, boolean downloadIfNot) throws IOException { Files.createDirectories(downloadDirectory); var zipFile = downloadDirectory.resolve(filename + ".zip"); - if (!isDownloadedAlready(zipFile) && downloadIfNot) { + if (!isDownloadedAlready(zipFile)) { + if (!downloadIfNot) { + return null; + } try (InputStream stream = url.openStream()) { try (ReadableByteChannel readableByteChannel = Channels.newChannel(stream)) { try (FileOutputStream fos = new FileOutputStream(zipFile.toFile())) { @@ -296,6 +301,9 @@ private static boolean isDownloadedAlready(Path zipFile) { } private Path unzipIfNeeded(Path zipFile) throws IOException { + if (zipFile == null) { + return null; + } var zipSpec = BioimageIoSpec.parseModel(zipFile); String version = zipSpec.getVersion(); var outdir = zipFile.resolveSibling(getFolderName(zipSpec.getName(), version)); @@ -307,7 +315,6 @@ private Path unzipIfNeeded(Path zipFile) throws IOException { logger.error("Error unzipping model", e); // clean up files just in case! Files.deleteIfExists(outdir); - } finally { Files.deleteIfExists(zipFile); } } diff --git a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java index 0471f1f..f92c5b6 100644 --- a/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java +++ b/src/main/java/qupath/ext/instanseg/ui/InstanSegController.java @@ -489,7 +489,7 @@ private void refreshModelChoice() { try { model.checkIfDownloaded(modelDir.resolve("downloaded"), false); } catch (IOException e) { - logger.warn("Downloaded model is missing zip or RDF file(s); this shouldn't happen", e); + logger.debug("Error checking zip or RDF file(s); this shouldn't happen", e); Dialogs.showErrorNotification(resources.getString("title"), resources.getString("error.checkingModel")); } boolean isDownloaded = modelDir != null && model.isValid();