Skip to content

Commit 6b706a5

Browse files
Move axis management to PyramidSeries
1 parent 9b9da05 commit 6b706a5

3 files changed

Lines changed: 85 additions & 87 deletions

File tree

src/main/java/com/glencoesoftware/pyramid/PyramidFromDirectoryWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,9 @@ private byte[] getInputTileBytes(PyramidSeries s, int resolution,
668668
realHeight = region.height;
669669
}
670670

671-
int[] gridPosition = descriptor.getArray(pos[2], pos[1], pos[0],
671+
int[] gridPosition = s.getArray(pos[2], pos[1], pos[0],
672672
y * descriptor.tileSizeY, x * descriptor.tileSizeX);
673-
int[] shape = descriptor.getArray(1, 1, 1, realHeight, realWidth);
673+
int[] shape = s.getArray(1, 1, 1, realHeight, realWidth);
674674

675675
ZarrArray block = reader.openArray(descriptor.path);
676676

src/main/java/com/glencoesoftware/pyramid/PyramidSeries.java

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,71 @@ public class PyramidSeries {
5555
/** Description of each resolution in the pyramid. */
5656
List<ResolutionDescriptor> resolutions;
5757

58+
/** Axes in the underlying array, in order. */
59+
ArrayList<String> axes = new ArrayList<String>();
60+
61+
/**
62+
* Add named axis to ordered list of axes in this resolution.
63+
* Names are stored as upper-case only.
64+
*
65+
* @param axis name e.g. "x"
66+
*/
67+
public void addAxis(String axis) {
68+
axes.add(axis.toUpperCase());
69+
}
70+
71+
/**
72+
* Find the index in the ordered list of the named axis.
73+
*
74+
* @param axis name e.g. "x"
75+
* @return index into list of axes
76+
*/
77+
public int getIndex(String axis) {
78+
return axes.indexOf(axis.toUpperCase());
79+
}
80+
81+
/**
82+
* Create an indexing array (e.g. shape or offset) for this resolution,
83+
* which represents the given 5D values.
84+
* Since the resolution's underlying array may have less than 5 dimensions,
85+
* this is mapping from the 5D space of the OME data model to the
86+
* ND space of this resolution's array.
87+
*
88+
* @param ti T index
89+
* @param ci C index
90+
* @param zi Z index
91+
* @param yi Y index
92+
* @param xi X index
93+
* @return array representing the given indexes, in this resolution's
94+
* dimensional space
95+
*/
96+
public int[] getArray(int ti, int ci, int zi, int yi, int xi) {
97+
int[] returnArray = new int[axes.size()];
98+
for (int i=0; i<axes.size(); i++) {
99+
char axis = axes.get(i).charAt(0);
100+
switch (axis) {
101+
case 'X':
102+
returnArray[i] = xi;
103+
break;
104+
case 'Y':
105+
returnArray[i] = yi;
106+
break;
107+
case 'Z':
108+
returnArray[i] = zi;
109+
break;
110+
case 'C':
111+
returnArray[i] = ci;
112+
break;
113+
case 'T':
114+
returnArray[i] = ti;
115+
break;
116+
default:
117+
throw new IllegalArgumentException("Unexpected axis: " + axis);
118+
}
119+
}
120+
return returnArray;
121+
}
122+
58123
/**
59124
* Calculate image width and height for each resolution.
60125
* Uses the first tile in the resolution to find the tile size.
@@ -71,9 +136,22 @@ public void describePyramid(ZarrGroup reader, OMEPyramidStore metadata)
71136
(List<Map<String, Object>>) reader.openSubGroup(path).getAttributes().get(
72137
"multiscales");
73138
Map<String, Object> multiscale = multiscales.get(0);
74-
List<Map<String, Object>> axes = null;
139+
List<Map<String, Object>> storedAxes = null;
75140
if (multiscales != null) {
76-
axes = (List<Map<String, Object>>) multiscale.get("axes");
141+
storedAxes = (List<Map<String, Object>>) multiscale.get("axes");
142+
}
143+
144+
if (storedAxes != null) {
145+
for (Map<String, Object> axis : storedAxes) {
146+
addAxis(axis.get("name").toString());
147+
}
148+
}
149+
else {
150+
addAxis("T");
151+
addAxis("C");
152+
addAxis("Z");
153+
addAxis("Y");
154+
addAxis("X");
77155
}
78156

79157
resolutions = new ArrayList<ResolutionDescriptor>();
@@ -82,25 +160,12 @@ public void describePyramid(ZarrGroup reader, OMEPyramidStore metadata)
82160
descriptor.resolutionNumber = resolution;
83161
descriptor.path = path + "/" + resolution;
84162

85-
if (axes != null) {
86-
for (Map<String, Object> axis : axes) {
87-
descriptor.addAxis(axis.get("name").toString());
88-
}
89-
}
90-
else {
91-
descriptor.addAxis("T");
92-
descriptor.addAxis("C");
93-
descriptor.addAxis("Z");
94-
descriptor.addAxis("Y");
95-
descriptor.addAxis("X");
96-
}
97-
98163
ZarrArray array = reader.openArray(descriptor.path);
99164
int[] dimensions = array.getShape();
100165
int[] blockSizes = array.getChunks();
101166

102-
int xIndex = descriptor.getIndex("X");
103-
int yIndex = descriptor.getIndex("Y");
167+
int xIndex = getIndex("X");
168+
int yIndex = getIndex("Y");
104169

105170
descriptor.sizeX = dimensions[xIndex];
106171
descriptor.sizeY = dimensions[yIndex];
@@ -147,7 +212,7 @@ public void describePyramid(ZarrGroup reader, OMEPyramidStore metadata)
147212
// dimensionLengths is in ZCT order, independent of dimensionOrder
148213
// the two orders may be different if the --rgb flag was used
149214
String axis = "ZCT".substring(i, i + 1);
150-
int axisIndex = descriptor.getIndex(axis);
215+
int axisIndex = getIndex(axis);
151216
LOG.debug("Checking axis {} with index {}, position {}",
152217
axis, axisIndex, i);
153218

src/main/java/com/glencoesoftware/pyramid/ResolutionDescriptor.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*/
88
package com.glencoesoftware.pyramid;
99

10-
import java.util.ArrayList;
11-
1210
public class ResolutionDescriptor {
1311
/** Path to resolution. */
1412
String path;
@@ -34,69 +32,4 @@ public class ResolutionDescriptor {
3432
/** Number of tiles along Y axis. */
3533
Integer numberOfTilesY;
3634

37-
/** Axes in the underlying array, in order. */
38-
ArrayList<String> axes = new ArrayList<String>();
39-
40-
/**
41-
* Add named axis to ordered list of axes in this resolution.
42-
* Names are stored as upper-case only.
43-
*
44-
* @param axis name e.g. "x"
45-
*/
46-
public void addAxis(String axis) {
47-
axes.add(axis.toUpperCase());
48-
}
49-
50-
/**
51-
* Find the index in the ordered list of the named axis.
52-
*
53-
* @param axis name e.g. "x"
54-
* @return index into list of axes
55-
*/
56-
public int getIndex(String axis) {
57-
return axes.indexOf(axis.toUpperCase());
58-
}
59-
60-
/**
61-
* Create an indexing array (e.g. shape or offset) for this resolution,
62-
* which represents the given 5D values.
63-
* Since the resolution's underlying array may have less than 5 dimensions,
64-
* this is mapping from the 5D space of the OME data model to the
65-
* ND space of this resolution's array.
66-
*
67-
* @param t T index
68-
* @param c C index
69-
* @param z Z index
70-
* @param y Y index
71-
* @param x X index
72-
* @return array representing the given indexes, in this resolution's
73-
* dimensional space
74-
*/
75-
public int[] getArray(int t, int c, int z, int y, int x) {
76-
int[] returnArray = new int[axes.size()];
77-
for (int i=0; i<axes.size(); i++) {
78-
char axis = axes.get(i).charAt(0);
79-
switch (axis) {
80-
case 'X':
81-
returnArray[i] = x;
82-
break;
83-
case 'Y':
84-
returnArray[i] = y;
85-
break;
86-
case 'Z':
87-
returnArray[i] = z;
88-
break;
89-
case 'C':
90-
returnArray[i] = c;
91-
break;
92-
case 'T':
93-
returnArray[i] = t;
94-
break;
95-
default:
96-
throw new IllegalArgumentException("Unexpected axis: " + axis);
97-
}
98-
}
99-
return returnArray;
100-
}
101-
10235
}

0 commit comments

Comments
 (0)