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

Add group path to series index mapping in root attributes #157

Merged
merged 3 commits into from
Sep 23, 2022
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
41 changes: 33 additions & 8 deletions src/main/java/com/glencoesoftware/bioformats2raw/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,39 @@ public void convert()
scaleFormatString = "%s/%s/%d/%d";
}

// fileset level metadata
if (!noRootGroup) {
final ZarrGroup root = ZarrGroup.create(getRootPath());
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("bioformats2raw.layout", LAYOUT);

root.writeAttributes(attributes);
}
if (!noOMEMeta) {
Path metadataPath = getRootPath().resolve("OME");
final ZarrGroup root = ZarrGroup.create(metadataPath);
Map<String, Object> attributes = new HashMap<String, Object>();

// record the path to each series (multiscales) and the corresponding
// series (OME-XML Image) index
// using the index as the key would mean that the index is stored
// as a string instead of an integer
List<String> groups = new ArrayList<String>();
for (Integer index : seriesList) {
String resolutionString = String.format(
scaleFormatString, getScaleFormatStringArgs(index, 0));
String seriesString = "";
if (resolutionString.indexOf('/') >= 0) {
seriesString = resolutionString.substring(0,
resolutionString.lastIndexOf('/'));
}
groups.add(seriesString);
}
attributes.put("series", groups);

root.writeAttributes(attributes);
}

for (Integer index : seriesList) {
try {
write(index);
Expand Down Expand Up @@ -1215,14 +1248,6 @@ public void saveResolutions(int series)
sizeX, tileWidth, sizeY, tileHeight, sizeZ, chunkDepth, imageCount
);

// fileset level metadata
if (!noRootGroup) {
final ZarrGroup root = ZarrGroup.create(getRootPath());
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("bioformats2raw.layout", LAYOUT);
root.writeAttributes(attributes);
}

// series level metadata
setSeriesLevelMetadata(series, resolutions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ public void testAdditionalScaleFormatStringArgs() throws Exception {
series0.openArray("0");
series0 = ZarrGroup.open(output.resolve("ghi/999/1").toString());
series0.openArray("0");

Path omePath = output.resolve("OME");
ZarrGroup z = ZarrGroup.open(omePath.toString());
List<String> groupMap = (List<String>) z.getAttributes().get("series");
assertEquals(groupMap.size(), 2);
assertEquals(groupMap.get(0), "abc/888/0");
assertEquals(groupMap.get(1), "ghi/999/1");
}

/**
Expand Down Expand Up @@ -240,6 +247,14 @@ public void testDefaultLayoutIsSetAndIsNested() throws Exception {
ZarrGroup z = ZarrGroup.open(output.toString());
Integer layout = (Integer)
z.getAttributes().get("bioformats2raw.layout");

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 1);
assertEquals(groupMap.get(0), "0");

ZarrArray series0 = ZarrGroup.open(output.resolve("0")).openArray("0");

// no getter for DimensionSeparator in ZarrArray
Expand Down Expand Up @@ -428,6 +443,14 @@ public void testMultiSeries() throws Exception {
assertTool();
ZarrGroup z = ZarrGroup.open(output.toString());

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 2);
assertEquals(groupMap.get(0), "0");
assertEquals(groupMap.get(1), "1");

// Check series 0 dimensions and special pixels
ZarrArray series0 = z.openArray("0/0");
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getShape());
Expand Down Expand Up @@ -462,6 +485,16 @@ public void testSingleBeginningSeries() throws Exception {
assertTool("-s", "0");
ZarrGroup z = ZarrGroup.open(output.toString());

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 1);
assertEquals(groupMap.get(0), "0");

OME ome = getOMEMetadata();
assertEquals(1, ome.sizeOfImageList());

// Check series 0 dimensions and special pixels
ZarrArray series0 = z.openArray("0/0");
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getShape());
Expand Down Expand Up @@ -489,6 +522,16 @@ public void testSingleEndSeries() throws Exception {
assertTool("-s", "1");
ZarrGroup z = ZarrGroup.open(output.toString());

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 1);
assertEquals(groupMap.get(0), "0");

OME ome = getOMEMetadata();
assertEquals(1, ome.sizeOfImageList());

// Check series 1 dimensions and special pixels
ZarrArray series0 = z.openArray("0/0");
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getShape());
Expand Down Expand Up @@ -516,6 +559,16 @@ public void testSingleMiddleSeries() throws Exception {
assertTool("-s", "1");
ZarrGroup z = ZarrGroup.open(output.toString());

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 1);
assertEquals(groupMap.get(0), "0");

OME ome = getOMEMetadata();
assertEquals(1, ome.sizeOfImageList());

// Check series 1 dimensions and special pixels
ZarrArray series0 = z.openArray("0/0");
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getShape());
Expand Down Expand Up @@ -912,11 +965,21 @@ public void testNoHCSOption() throws Exception {

ZarrGroup z = ZarrGroup.open(output);

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 12);
for (int i=0; i<12; i++) {
assertEquals(groupMap.get(i), String.valueOf(i));
}

// Check dimensions and block size
ZarrArray series0 = z.openArray("0/0");
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getShape());
assertArrayEquals(new int[] {1, 1, 1, 512, 512}, series0.getChunks());
assertEquals(12, z.getGroupKeys().size());
// 12 series + OME group
assertEquals(13, z.getGroupKeys().size());

// Check OME metadata
OME ome = getOMEMetadata();
Expand Down Expand Up @@ -952,6 +1015,22 @@ public void testHCSMetadata() throws Exception {
int rowCount = 2;
int colCount = 3;
int fieldCount = 2;

Path omePath = output.resolve("OME");
ZarrGroup omeGroup = ZarrGroup.open(omePath.toString());
List<String> groupMap =
(List<String>) omeGroup.getAttributes().get("series");
assertEquals(groupMap.size(), 12);
int index = 0;
for (int r=0; r<rowCount; r++) {
for (int c=0; c<colCount; c++) {
for (int f=0; f<fieldCount; f++) {
String groupPath = (char) (r + 'A') + "/" + (c + 1) + "/" + f;
assertEquals(groupMap.get(index++), groupPath);
}
}
}

Map<String, List<String>> plateMap = new HashMap<String, List<String>>();
plateMap.put("A", Arrays.asList("1", "2", "3"));
plateMap.put("B", Arrays.asList("1", "2", "3"));
Expand Down Expand Up @@ -1390,6 +1469,8 @@ public void testNoOmeMetaExportOption() throws Exception {

assertTrue(!Files.exists(
output.resolve("OME").resolve("METADATA.ome.xml")));
assertTrue(!Files.exists(
output.resolve("OME").resolve(".zattrs")));
}

/**
Expand Down