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

Use canonical path for calls to ZarrService #57

Merged
merged 4 commits into from
Jul 7, 2023
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
42 changes: 26 additions & 16 deletions src/loci/formats/in/ZarrReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.FileVisitOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
Expand Down Expand Up @@ -156,14 +157,15 @@ protected void initFile(String id) throws FormatException, IOException {
String zarrRootPath = zarrPath.substring(0, zarrPath.indexOf(".zarr") + 5);
String name = zarrRootPath.substring(zarrRootPath.lastIndexOf(File.separator)+1, zarrRootPath.length() - 5);
Location omeMetaFile = new Location( zarrRootPath + File.separator + "OME", "METADATA.ome.xml" );
String canonicalPath = new Location(zarrRootPath).getCanonicalPath();

initializeZarrService(zarrRootPath);
initializeZarrService(canonicalPath);

if(omeMetaFile.exists()) {
parseOMEXML(omeMetaFile, store);
}
// Parse base level attributes
Map<String, Object> attr = zarrService.getGroupAttr(zarrRootPath);
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
int attrIndex = 0;
if (attr != null && !attr.isEmpty()) {
parseResolutionCount(zarrRootPath, "");
Expand All @@ -181,8 +183,8 @@ protected void initFile(String id) throws FormatException, IOException {
}

// Parse group attributes
for (String key: zarrService.getGroupKeys(zarrRootPath)) {
Map<String, Object> attributes = zarrService.getGroupAttr(zarrRootPath+File.separator+key);
for (String key: zarrService.getGroupKeys(canonicalPath)) {
Map<String, Object> attributes = zarrService.getGroupAttr(canonicalPath+File.separator+key);
if (attributes != null && !attributes.isEmpty()) {
parseResolutionCount(zarrRootPath, key);
parseLabels(zarrRootPath, key);
Expand All @@ -202,8 +204,8 @@ protected void initFile(String id) throws FormatException, IOException {
}

// Parse array attributes
for (String key: zarrService.getArrayKeys(zarrRootPath)) {
Map<String, Object> attributes = zarrService.getArrayAttr(zarrRootPath+File.separator+key);
for (String key: zarrService.getArrayKeys(canonicalPath)) {
Map<String, Object> attributes = zarrService.getArrayAttr(canonicalPath+File.separator+key);
if (attributes != null && !attributes.isEmpty()) {
attrIndex++;
String jsonAttr;
Expand All @@ -220,7 +222,7 @@ protected void initFile(String id) throws FormatException, IOException {
}

arrayPaths = new ArrayList<String>();
arrayPaths.addAll(zarrService.getArrayKeys(zarrRootPath));
arrayPaths.addAll(zarrService.getArrayKeys(canonicalPath));
orderArrayPaths(zarrRootPath);

core.clear();
Expand Down Expand Up @@ -328,7 +330,8 @@ private int[] getOriginalShape(int [] shape5D, int size) {
@Override
public void reopenFile() throws IOException {
try {
initializeZarrService(currentId);
String canonicalPath = new Location(currentId).getCanonicalPath();
initializeZarrService(canonicalPath);
}
catch (FormatException e) {
throw new IOException(e);
Expand Down Expand Up @@ -433,7 +436,8 @@ private void openZarr() {
seriesIndex += resolution;
}
newZarrPath += File.separator + arrayPaths.get(seriesIndex);
zarrService.open(newZarrPath);
String canonicalPath = new Location(newZarrPath).getCanonicalPath();
zarrService.open(canonicalPath);
}
}
} catch (IOException | FormatException e) {
Expand All @@ -454,7 +458,8 @@ private void orderArrayPaths(String root) {

private void parseResolutionCount(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
ArrayList<Object> multiscales = (ArrayList<Object>) attr.get("multiscales");
if (multiscales != null) {
for (int x = 0; x < multiscales.size(); x++) {
Expand Down Expand Up @@ -521,7 +526,8 @@ else if (multiscaleAxes.get(i) instanceof HashMap) {

private void parsePlate(String root, String key, MetadataStore store) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
Map<Object, Object> plates = (Map<Object, Object>) attr.get("plate");
if (plates != null) {
ArrayList<Object> columns = (ArrayList<Object>)plates.get("columns");
Expand Down Expand Up @@ -608,7 +614,8 @@ private void parsePlate(String root, String key, MetadataStore store) throws IOE
private void parseWells(String root, String key, MetadataStore store, int plateIndex, int wellIndex,
HashMap<Integer, Integer> acqIdsIndexMap) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
Map<Object, Object> wells = (Map<Object, Object>) attr.get("well");
if (wells != null) {
ArrayList<Object> images = (ArrayList<Object>)wells.get("images");
Expand Down Expand Up @@ -641,7 +648,8 @@ private void parseWells(String root, String key, MetadataStore store, int plateI

private void parseLabels(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
ArrayList<Object> labels = (ArrayList<Object>) attr.get("labels");
if (labels != null) {
for (int l = 0; l < labels.size(); l++) {
Expand All @@ -652,7 +660,8 @@ private void parseLabels(String root, String key) throws IOException, FormatExce

private void parseImageLabels(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
Map<String, Object> imageLabel = (Map<String, Object>) attr.get("image-label");
if (imageLabel != null) {
String version = (String) imageLabel.get("version");
Expand Down Expand Up @@ -691,7 +700,8 @@ private void parseImageLabels(String root, String key) throws IOException, Forma

private void parseOmeroMetadata(String root, String key) throws IOException, FormatException {
String path = key.isEmpty() ? root : root + File.separator + key;
Map<String, Object> attr = zarrService.getGroupAttr(path);
String canonicalPath = new Location(path).getCanonicalPath();
Map<String, Object> attr = zarrService.getGroupAttr(canonicalPath);
Map<String, Object> omeroMetadata = (Map<String, Object>) attr.get("omero");
if (omeroMetadata != null) {
Integer id = (Integer) omeroMetadata.get("id");
Expand Down Expand Up @@ -853,7 +863,7 @@ public String[] getUsedFiles(boolean noPixels) {
FormatTools.assertId(currentId, true, 1);
String zarrRootPath = currentId.substring(0, currentId.indexOf(".zarr") + 5);
ArrayList<String> usedFiles = new ArrayList<String>();
try (Stream<Path> paths = Files.walk(Paths.get(zarrRootPath))) {
try (Stream<Path> paths = Files.walk(Paths.get(zarrRootPath), FileVisitOption.FOLLOW_LINKS)) {
paths.filter(Files::isRegularFile)
.forEach(path -> usedFiles.add(path.toFile().getAbsolutePath()));
} catch (IOException e) {
Expand Down
8 changes: 5 additions & 3 deletions test/loci/formats/utests/ZarrReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.testng.annotations.Test;

import loci.common.DataTools;
import loci.common.Location;
import loci.formats.FormatException;
import loci.formats.FormatTools;
import loci.formats.in.ZarrReader;
Expand All @@ -79,7 +80,8 @@ public void setUp() throws Exception {
reader = new ZarrReaderMock(zarrService);
file = File.createTempFile("tileTest", ".zarr");
String rootPath = file.getAbsolutePath();

String canonicalPath = new Location(rootPath).getCanonicalPath();

Map<String, Object> topLevelAttributes = new HashMap<String, Object>();
ArrayList<Object> multiscales = new ArrayList<Object>();
Map<String, Object> datasets = new HashMap<String, Object>();
Expand All @@ -97,8 +99,8 @@ public void setUp() throws Exception {
datasets.put("axes", Arrays.asList("t", "c", "z", "y", "x"));
multiscales.add(datasets);
topLevelAttributes.put("multiscales", multiscales);
when(zarrService.getGroupAttr(rootPath)).thenReturn(topLevelAttributes);

when(zarrService.getGroupAttr(canonicalPath)).thenReturn(topLevelAttributes);
when(zarrService.getShape()).thenReturn(shape);
when(zarrService.getPixelType()).thenReturn(0);
reader.setId(file.getAbsolutePath());
Expand Down