Skip to content

Commit

Permalink
Merge pull request #1241 from petebankhead/late-fixes
Browse files Browse the repository at this point in the history
Fix memoization on Windows, directory prefs
  • Loading branch information
petebankhead committed Feb 23, 2023
2 parents 5ab6fb9 + 7d33939 commit 1368912
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Version 0.4.3-SNAPSHOT
## Version 0.4.3

This is a *minor release* that aims to be fully compatible with previous v0.4.x releases, while fixing bugs.

Expand All @@ -11,8 +11,10 @@ This is a *minor release* that aims to be fully compatible with previous v0.4.x
### Bugs fixed
* Opening the same image in multiple viewers results in detections being wrongly shown in both (https://github.com/qupath/qupath/issues/1217)
* 'Transform annotations' can't be applied to selected objects when double-clicking to end the transform (https://github.com/qupath/qupath/issues/1231)
* Wrong script generated for 'Add intensity features' with some tile shapes (https://github.com/qupath/qupath/issues/1227)
* Unusable script generated for 'Add intensity features' with some tile shapes (https://github.com/qupath/qupath/issues/1227)
* Unusable generated for 'Expand annotations' with some line caps (https://github.com/qupath/qupath/issues/1227)
* 'Tile classifications to annotations' is broken (https://github.com/qupath/qupath/issues/1226)
* Directory preferences cannot be reset (https://github.com/qupath/qupath/issues/1240)

### Dependency updates
* Bio-Formats 6.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public class BioFormatsImageServer extends AbstractTileableImageServer {
private String[] args;

/**
* Path to the base image file - will be the same as path, unless the path encodes the name of a specific series, in which case this refers to the file without the series included
* File path if possible, or a URI otherwise.
*/
private String filePath;
private String filePathOrUrl;

/**
* Fix issue related to VSI images having (wrong) z-slices
*/
Expand Down Expand Up @@ -314,20 +314,20 @@ static BioFormatsImageServer checkSupport(URI uri, final BioFormatsServerOptions
// Try to get a local file path, but accept something else (since Bio-Formats handles other URIs)
try {
var path = GeneralTools.toPath(uri);
if (path != null)
filePath = path.toString();
// filePath = Paths.get(uri).toString();
if (path != null) {
filePathOrUrl = path.toString();
}
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
} finally {
if (filePath == null) {
if (filePathOrUrl == null) {
logger.debug("Using URI as file path: {}", uri);
filePath = uri.toString();
filePathOrUrl = uri.toString();
}
}

// Create a reader & extract the metadata
readerPool = new ReaderPool(options, filePath, bfArgs);
readerPool = new ReaderPool(options, filePathOrUrl, bfArgs);
IFormatReader reader = readerPool.getMainReader();
var meta = (OMEPyramidStore)reader.getMetadataStore();

Expand Down Expand Up @@ -986,7 +986,7 @@ public BufferedImage getAssociatedImage(String name) {
* @return
*/
public File getFile() {
return filePath == null ? null : new File(filePath);
return filePathOrUrl == null ? null : new File(filePathOrUrl);
}


Expand Down Expand Up @@ -1160,8 +1160,10 @@ private IFormatReader createReader(final BioFormatsServerOptions options, final
Memoizer memoizer = null;
int memoizationTimeMillis = options.getMemoizationTimeMillis();
File dir = null;
// We can only use memoization if we don't have an illegal character
if (BioFormatsServerOptions.allowMemoization() && memoizationTimeMillis >= 0 && !id.contains(":")) {
File fileMemo = null;
boolean useTempMemoDirectory = false;
// Check if we want to (and can) use memoization
if (BioFormatsServerOptions.allowMemoization() && memoizationTimeMillis >= 0) {
// Try to use a specified directory
String pathMemoization = options.getPathMemoization();
if (pathMemoization != null && !pathMemoization.trim().isEmpty()) {
Expand All @@ -1173,10 +1175,22 @@ private IFormatReader createReader(final BioFormatsServerOptions options, final
}
if (dir == null) {
dir = getTempMemoDir(true);
useTempMemoDirectory = dir != null;
}
if (dir != null) {
memoizer = new Memoizer(imageReader, memoizationTimeMillis, dir);
imageReader = memoizer;
try {
memoizer = new Memoizer(imageReader, memoizationTimeMillis, dir);
fileMemo = memoizer.getMemoFile(id);
// The call to .toPath() should throw an InvalidPathException if there are illegal characters
// If so, we want to know that now before committing to the memoizer
if (fileMemo != null && fileMemo.toPath() != null)
imageReader = memoizer;
} catch (Exception e) {
logger.warn("Unable to use memoization: {}", e.getLocalizedMessage());
logger.debug(e.getLocalizedMessage(), e);
fileMemo = null;
memoizer = null;
}
}
}

Expand All @@ -1194,10 +1208,9 @@ private IFormatReader createReader(final BioFormatsServerOptions options, final


if (id != null) {
if (memoizer != null) {
File fileMemo = ((Memoizer)imageReader).getMemoFile(id);
// If we're using a temporary directory, delete the memo file
if (dir == getTempMemoDir(false))
if (fileMemo != null) {
// If we're using a temporary directory, delete the memo file when app closes
if (useTempMemoDirectory)
tempMemoFiles.add(fileMemo);

long memoizationFileSize = fileMemo == null ? 0L : fileMemo.length();
Expand Down Expand Up @@ -1248,6 +1261,7 @@ else if (!memoFileExists)

return imageReader;
}



private IFormatReader nextQueuedReader() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void installExtension(QuPathGUI qupath) {
// Add preferences to QuPath GUI
PreferencePane prefs = QuPathGUI.getInstance().getPreferencePane();
prefs.addPropertyPreference(enableBioformats, Boolean.class, "Enable Bio-Formats", "Bio-Formats", "Allow QuPath to use Bio-Formats for image reading");
prefs.addPropertyPreference(filesOnly, Boolean.class, "Local files only", "Bio-Formats", "Limit Bio-Formats to only opening local files, not other URLs.\n"
prefs.addPropertyPreference(filesOnly, Boolean.class, "Bio-Formats files only", "Bio-Formats", "Limit Bio-Formats to only opening files, not other URLs.\n"
+ "Allowing Bio-Formats to open URLs can cause performance issues if this results in attempting to open URLs intended to be read using other image servers.");
prefs.addPropertyPreference(useParallelization, Boolean.class, "Enable Bio-Formats tile parallelization", "Bio-Formats", "Enable reading image tiles in parallel when using Bio-Formats");
// prefs.addPropertyPreference(parallelizeMultichannel, Boolean.class, "Enable Bio-Formats channel parallelization (experimental)", "Bio-Formats", "Request multiple image channels in parallel, even if parallelization of tiles is turned off - "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private static float supportLevel(URI uri, String... args) {
BioFormatsServerOptions options = BioFormatsServerOptions.getInstance();

if (type.isURL() && options.getFilesOnly()) {
logger.debug("Cannot open URL with Bio-Formats - 'Bio-Formats files only' preference is turned on");
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ static class DirectoryPropertyItem extends PropertyItem {

DirectoryPropertyItem(final Property<String> prop) {
this.prop = prop;
fileValue = Bindings.createObjectBinding(() -> prop.getValue() == null ? null : new File(prop.getValue()), prop);
fileValue = Bindings.createObjectBinding(() -> prop.getValue() == null || prop.getValue().isEmpty() ? null : new File(prop.getValue()), prop);
}

@Override
Expand All @@ -818,9 +818,9 @@ public Object getValue() {

@Override
public void setValue(Object value) {
if (value instanceof String)
if (value instanceof String) {
prop.setValue((String)value);
else if (value instanceof File)
} else if (value instanceof File)
prop.setValue(((File)value).getAbsolutePath());
else if (value == null)
prop.setValue(null);
Expand Down

0 comments on commit 1368912

Please sign in to comment.