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

Check models are downloaded on selection #119

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -92,17 +92,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();
}
}

/**
Expand Down Expand Up @@ -266,10 +269,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())) {
Expand All @@ -295,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));
Expand All @@ -306,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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,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;
Expand Down Expand Up @@ -543,7 +549,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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Loading