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

Class cast exception when parsing metadata #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 11 additions & 9 deletions src/loci/formats/in/ZarrReader.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package loci.formats.in;

/*-
Expand Down Expand Up @@ -903,7 +902,7 @@ private void parseImageLabels(String root, Map<String, Object> attr) throws IOEx
for (int p = 0; p < properties.size(); p++) {
Map<String, Object> prop = (Map<String, Object>) properties.get(p);
Integer labelValue = (Integer) prop.get("label-value");
Double area = (Double) prop.get("area (pixels)");
Number area = (Number) prop.get("area (pixels)");
String propClass = (String) prop.get("class");
}
}
Expand All @@ -930,17 +929,17 @@ private void parseOmeroMetadata(String root, Map<String, Object> attr) throws IO
for (int i = 0; i < channels.size(); i++) {
Map<String, Object> channel = (Map<String, Object>) channels.get(i);
Boolean channelActive = (Boolean) channel.get("active");
Double channelCoefficient = (Double) channel.get("coefficient");
Number channelCoefficient = (Number) channel.get("coefficient");
String channelColor = (String) channel.get("color");
String channelFamily = (String) channel.get("family");
Boolean channelInverted = (Boolean) channel.get("inverted");
String channelLabel = (String) channel.get("label");
Map<String, Object> window = (Map<String, Object>)channel.get("window");
if (window != null) {
Double windowStart = getDouble(window, "start");
Double windowEnd = getDouble(window, "end");
Double windowMin = getDouble(window, "min");
Double windowMax = getDouble(window, "max");
Number windowStart = getDouble(window, "start");
Number windowEnd = getDouble(window, "end");
Number windowMin = getDouble(window, "min");
Number windowMax = getDouble(window, "max");
}
}
Map<String, Object> rdefs = (Map<String, Object>)omeroMetadata.get("rdefs");
Expand Down Expand Up @@ -1112,11 +1111,14 @@ public static String getRowString(int rowIndex) {
return sb.reverse().toString();
}

private Double getDouble(Map<String, Object> src, String key) {
private Number getDouble(Map<String, Object> src, String key) {
Number val = (Number) src.get(key);
if (val == null) {
return null;
}
if (val instanceof Integer) {
return ((Integer) val).doubleValue();
}
return val.doubleValue();
}

Expand Down Expand Up @@ -1205,7 +1207,7 @@ public boolean quickRead() {
}
return QUICK_READ_DEFAULT;
}

/**
* Used to decide if images stored in the label sub folder should be included in the list of images
* @return boolean true if images in the label folder should be included, default is false
Expand Down
77 changes: 77 additions & 0 deletions test/loci/formats/utests/ZarrReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,81 @@ public void testResolutionCount() {
assertEquals(3, reader.getSeriesCount());
}

@Test
public void testParseOmeroMetadataWithIntegerValues() {
Map<String, Object> omeroMetadata = new HashMap<>();
omeroMetadata.put("id", 1);
omeroMetadata.put("name", "Test Image");
omeroMetadata.put("version", "0.1");

ArrayList<Object> channels = new ArrayList<>();
Map<String, Object> channel = new HashMap<>();
channel.put("active", true);
channel.put("coefficient", 1);
channel.put("color", "FFFFFF");
channel.put("family", "linear");
channel.put("inverted", false);
channel.put("label", "Channel 1");

Map<String, Object> window = new HashMap<>();
window.put("start", 0);
window.put("end", 255);
window.put("min", 0);
window.put("max", 255);
channel.put("window", window);

channels.add(channel);
omeroMetadata.put("channels", channels);

Map<String, Object> rdefs = new HashMap<>();
rdefs.put("defaultT", 0);
rdefs.put("defaultZ", 0);
rdefs.put("model", "color");
omeroMetadata.put("rdefs", rdefs);

try {
reader.parseOmeroMetadata(file.getAbsolutePath(), omeroMetadata);
} catch (IOException | FormatException e) {
fail("Unexpected exception while parsing Omero metadata with Integer values");
}
}

@Test
public void testParseOmeroMetadataWithDoubleValues() {
Map<String, Object> omeroMetadata = new HashMap<>();
omeroMetadata.put("id", 1);
omeroMetadata.put("name", "Test Image");
omeroMetadata.put("version", "0.1");

ArrayList<Object> channels = new ArrayList<>();
Map<String, Object> channel = new HashMap<>();
channel.put("active", true);
channel.put("coefficient", 1.0);
channel.put("color", "FFFFFF");
channel.put("family", "linear");
channel.put("inverted", false);
channel.put("label", "Channel 1");

Map<String, Object> window = new HashMap<>();
window.put("start", 0.0);
window.put("end", 255.0);
window.put("min", 0.0);
window.put("max", 255.0);
channel.put("window", window);

channels.add(channel);
omeroMetadata.put("channels", channels);

Map<String, Object> rdefs = new HashMap<>();
rdefs.put("defaultT", 0);
rdefs.put("defaultZ", 0);
rdefs.put("model", "color");
omeroMetadata.put("rdefs", rdefs);

try {
reader.parseOmeroMetadata(file.getAbsolutePath(), omeroMetadata);
} catch (IOException | FormatException e) {
fail("Unexpected exception while parsing Omero metadata with Double values");
}
}
}
Loading