Skip to content
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
66 changes: 63 additions & 3 deletions src/main/java/com/glencoesoftware/bioformats2raw/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,24 @@ public class Converter implements Callable<Integer> {
/** NGFF specification version.*/
public static final String NGFF_VERSION = "0.4";

/** Units allowed by NGFF specification for length/space axes. */
private static final List<String> LENGTH_UNITS = Arrays.asList(
"angstrom", "attometer", "centimeter", "decimeter", "exameter",
"femtometer", "foot", "gigameter", "hectometer", "inch", "kilometer",
"megameter", "meter", "micrometer", "mile", "millimeter", "nanometer",
"parsec", "petameter", "picometer", "terameter", "yard", "yoctometer",
"yottameter", "zeptometer", "zettameter"
);

/** Units allowed by NGFF specification for time axes. */
private static final List<String> TIME_UNITS = Arrays.asList(
"attosecond", "centisecond", "day", "decisecond", "exasecond",
"femtosecond", "gigasecond", "hectosecond", "hour", "kilosecond",
"megasecond", "microsecond", "millisecond", "minute", "nanosecond",
"petasecond", "picosecond", "second", "terasecond", "yoctosecond",
"yottasecond", "zeptosecond", "zettasecond"
);

private volatile Path inputPath;
private volatile String outputLocation;

Expand Down Expand Up @@ -2767,16 +2785,20 @@ else if (axis.equals("c")) {
String unitName = null;
try {
if (scale instanceof Length) {
unitName = UnitsLength.fromString(symbol).name().toLowerCase();
unitName = getLengthUnit(
UnitsLength.fromString(symbol).name().toLowerCase());
}
else if (scale instanceof Time) {
unitName = UnitsTime.fromString(symbol).name().toLowerCase();
unitName = getTimeUnit(
UnitsTime.fromString(symbol).name().toLowerCase());
}
}
catch (EnumerationException e) {
LOGGER.warn("Could not identify unit '{}'", symbol);
}
thisAxis.put("unit", unitName);
if (unitName != null) {
thisAxis.put("unit", unitName);
}
}
axes.add(thisAxis);
}
Expand Down Expand Up @@ -3210,6 +3232,44 @@ private String getNameFromWavelength(Length v) {
return v.toString();
}

/**
* Get a valid unit name for a length/space axis according to NGFF spec.
* OME-XML schema allows for some units that are not valid according to
* NGFF, e.g. 'pixel'.
*
* @param unitName length unit from OME schema
* @return valid NGFF unit or null
*/
private String getLengthUnit(String unitName) {
if (unitName == null) {
return null;
}
if (LENGTH_UNITS.contains(unitName)) {
return unitName;
}
LOGGER.debug("Omitting unit '{}'", unitName);
return null;
}

/**
* Get a valid unit name for a time axis according to NGFF spec.
* OME-XML schema allows for some units that are not valid according to
* NGFF, e.g. 'decasecond'.
*
* @param unitName length unit from OME schema
* @return valid NGFF unit or null
*/
private String getTimeUnit(String unitName) {
if (unitName == null) {
return null;
}
if (TIME_UNITS.contains(unitName)) {
return unitName;
}
LOGGER.debug("Omitting unit '{}'", unitName);
return null;
}

private int calculateResolutions(int width, int height) {
int resolutions = 1;
while (width > minSize || height > minSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,59 @@ public void testPhysicalSizes() throws Exception {
}
}

/**
* Test that physical sizes with invalid units are not saved in
* axes metadata.
*/
@Test
public void testInvalidUnitPhysicalSizes() throws Exception {
input = fake("physicalSizeX", "1pixel",
"physicalSizeY", "0.5thou",
"physicalSizeZ", "2dam");
assertTool();

List<Map<String, Object>> multiscales = getMultiscales("0");
assertEquals(1, multiscales.size());
Map<String, Object> multiscale = multiscales.get(0);
checkMultiscale(multiscale, "image");
List<Map<String, Object>> axes =
(List<Map<String, Object>>) multiscale.get("axes");
checkAxes(axes, "TCZYX",
new String[] {null, null, null, null, null});

List<Map<String, Object>> datasets =
(List<Map<String, Object>>) multiscale.get("datasets");
assertEquals(2, datasets.size());

for (int r=0; r<datasets.size(); r++) {
Map<String, Object> dataset = datasets.get(r);
List<Map<String, Object>> transforms =
(List<Map<String, Object>>) dataset.get("coordinateTransformations");
assertEquals(1, transforms.size());
Map<String, Object> scale = transforms.get(0);
assertEquals("scale", scale.get("type"));
List<Double> axisValues = (List<Double>) scale.get("scale");

assertEquals(5, axisValues.size());
double factor = Math.pow(2, r);
// X and Y are the only dimensions that are downsampled,
// so the TCZ physical scales remain the same across all resolutions
assertEquals(axisValues, Arrays.asList(new Double[] {
1.0, 1.0, 2.0, 0.5 * factor, factor}));
}

// Check dimensions and special pixels
ZarrGroup z = ZarrGroup.open(output.toString());
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());
int[] shape = new int[] {1, 1, 1, 512, 512};
byte[] tile = new byte[512 * 512];
series0.read(tile, shape);
int[] seriesPlaneNumberZCT = FakeReader.readSpecialPixels(tile);
assertArrayEquals(new int[] {0, 0, 0, 0, 0}, seriesPlaneNumberZCT);
}

/**
* Test using a different tile size from the default (1024).
*/
Expand Down