Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:qupath/qupath-extension-instanseg i…
Browse files Browse the repository at this point in the history
…nto multi-output-model
alanocallaghan committed Jan 6, 2025
2 parents 1a7bd67 + 7d03de1 commit 4691b23
Showing 3 changed files with 23 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/main/java/qupath/ext/instanseg/core/InstanSegModel.java
Original file line number Diff line number Diff line change
@@ -95,17 +95,20 @@ 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();
if (this.path != null) {
this.model = BioimageIoSpec.parseModel(path.toFile());
this.version = model.getVersion();
}
}

/**
@@ -315,10 +318,13 @@ private Optional<BioimageIoSpec.BioimageIoModel> 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 (!downloadIfNot) {
return null;
}
try (InputStream stream = url.openStream()) {
try (ReadableByteChannel readableByteChannel = Channels.newChannel(stream)) {
try (FileOutputStream fos = new FileOutputStream(zipFile.toFile())) {
@@ -344,6 +350,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));
@@ -355,7 +364,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);
}
}
Original file line number Diff line number Diff line change
@@ -485,6 +485,12 @@ private void refreshModelChoice() {
return;

var modelDir = InstanSegUtils.getModelDirectory().orElse(null);
try {
model.checkIfDownloaded(modelDir.resolve("downloaded"), false);
} catch (IOException 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();
if (!isDownloaded || qupath.getImageData() == null) {
return;
@@ -542,7 +548,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(() -> {
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 4691b23

Please sign in to comment.