Skip to content

Commit

Permalink
Merge pull request #144 from melissalinkert/unit-unit-test
Browse files Browse the repository at this point in the history
Expand axes tests to check for correct units
  • Loading branch information
chris-allan authored May 6, 2022
2 parents ca1ddc1 + 72b9149 commit b5f6c76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/glencoesoftware/bioformats2raw/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@
import loci.formats.ome.OMEXMLMetadata;
import loci.formats.services.OMEXMLService;
import loci.formats.services.OMEXMLServiceImpl;
import ome.units.quantity.Length;
import ome.units.quantity.Quantity;
import ome.units.quantity.Time;
import ome.xml.meta.OMEXMLMetadataRoot;
import ome.xml.model.enums.DimensionOrder;
import ome.xml.model.enums.EnumerationException;
import ome.xml.model.enums.PixelType;
import ome.xml.model.enums.UnitsLength;
import ome.xml.model.enums.UnitsTime;
import ome.xml.model.primitives.PositiveInteger;

import org.perf4j.slf4j.Slf4JStopWatch;
Expand Down Expand Up @@ -1617,7 +1621,20 @@ else if (axis.equals("c")) {
thisAxis.put("name", axis);
thisAxis.put("type", type);
if (scale != null) {
thisAxis.put("unit", scale.unit().getSymbol());
String symbol = scale.unit().getSymbol();
String unitName = null;
try {
if (scale instanceof Length) {
unitName = UnitsLength.fromString(symbol).name().toLowerCase();
}
else if (scale instanceof Time) {
unitName = UnitsTime.fromString(symbol).name().toLowerCase();
}
}
catch (EnumerationException e) {
LOGGER.warn("Could not identify unit '{}'", symbol);
}
thisAxis.put("unit", unitName);
}
axes.add(thisAxis);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void testMultiscalesMetadata() throws Exception {

List<Map<String, Object>> axes =
(List<Map<String, Object>>) multiscale.get("axes");
checkAxes(axes, "TCZYX");
checkAxes(axes, "TCZYX", null);

for (int r=0; r<datasets.size(); r++) {
Map<String, Object> dataset = datasets.get(r);
Expand Down Expand Up @@ -320,7 +320,7 @@ public void testSetXYCZTDimensionOrder() throws Exception {
Map<String, Object> multiscale = multiscales.get(0);
List<Map<String, Object>> axes =
(List<Map<String, Object>>) multiscale.get("axes");
checkAxes(axes, "TZCYX");
checkAxes(axes, "TZCYX", null);
}

/**
Expand All @@ -341,15 +341,15 @@ public void testSetOriginalDimensionOrder() throws Exception {
Map<String, Object> multiscale = multiscales.get(0);
List<Map<String, Object>> axes =
(List<Map<String, Object>>) multiscale.get("axes");
checkAxes(axes, "TZCYX");
checkAxes(axes, "TZCYX", null);
}

/**
* Test that physical sizes are saved in axes/transformations metadata.
*/
@Test
public void testPhysicalSizes() throws Exception {
input = fake("physicalSizeX", "1.0mm",
input = fake("physicalSizeX", "1.",
"physicalSizeY", "0.5mm",
"physicalSizeZ", "2cm");
assertTool();
Expand All @@ -361,7 +361,8 @@ public void testPhysicalSizes() throws Exception {
Map<String, Object> multiscale = multiscales.get(0);
List<Map<String, Object>> axes =
(List<Map<String, Object>>) multiscale.get("axes");
checkAxes(axes, "TCZYX");
checkAxes(axes, "TCZYX",
new String[] {null, null, "centimeter", "millimeter", "angstrom"});

List<Map<String, Object>> datasets =
(List<Map<String, Object>>) multiscale.get("datasets");
Expand Down Expand Up @@ -1631,12 +1632,20 @@ private void checkWell(
}
}

private void checkAxes(List<Map<String, Object>> axes, String order) {
private void checkAxes(List<Map<String, Object>> axes, String order,
String[] units)
{
assertEquals(axes.size(), order.length());
for (int i=0; i<axes.size(); i++) {
String name = axes.get(i).get("name").toString();
assertEquals(name, order.toLowerCase().substring(i, i + 1));
assertTrue(axes.get(i).containsKey("type"));
if (units != null) {
assertEquals(axes.get(i).get("unit"), units[i]);
}
else {
assertTrue(!axes.get(i).containsKey("unit"));
}
}
}

Expand Down

0 comments on commit b5f6c76

Please sign in to comment.