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

FakeReader: add support for populating channel wavelengths and instrument #4272

Open
wants to merge 2 commits into
base: develop
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
49 changes: 49 additions & 0 deletions components/formats-bsd/src/loci/formats/in/FakeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

package loci.formats.in;

import static ome.xml.model.Channel.getEmissionWavelengthUnitXsdDefault;
import static ome.xml.model.Channel.getExcitationWavelengthUnitXsdDefault;
import static ome.xml.model.Pixels.getPhysicalSizeXUnitXsdDefault;
import static ome.xml.model.Pixels.getPhysicalSizeYUnitXsdDefault;
import static ome.xml.model.Pixels.getPhysicalSizeZUnitXsdDefault;
Expand Down Expand Up @@ -607,6 +609,7 @@ protected void initFile(String id) throws FormatException, IOException {
boolean metadataComplete = true;
boolean thumbnail = false;
boolean withMicrobeam = false;
boolean withInstrument = false;

int seriesCount = 1;
int resolutionCount = 1;
Expand All @@ -617,6 +620,8 @@ protected void initFile(String id) throws FormatException, IOException {

Integer defaultColor = null;
ArrayList<Integer> color = new ArrayList<Integer>();
ArrayList<Length> excitationWavelengths = new ArrayList<Length>();
ArrayList<Length> emissionWavelengths = new ArrayList<Length>();

ArrayList<IniTable> seriesTables = new ArrayList<IniTable>();

Expand Down Expand Up @@ -716,6 +721,7 @@ else if (key.equals("pixelType")) {
else if (key.equals("fields")) fields = intValue;
else if (key.equals("plateAcqs")) plateAcqs = intValue;
else if (key.equals("withMicrobeam")) withMicrobeam = boolValue;
else if (key.equals("withInstrument")) withInstrument = boolValue;
else if (key.equals("annLong")) annLong = intValue;
else if (key.equals("annDouble")) annDouble = intValue;
else if (key.equals("annMap")) annMap = intValue;
Expand Down Expand Up @@ -755,6 +761,20 @@ else if (key.startsWith("color_")) {
color.add(null);
}
color.set(index, parseColor(value));
} else if (key.startsWith("emission_")) {
int index = Integer.parseInt(key.substring(key.indexOf('_') + 1));
while (index >= emissionWavelengths.size()) {
emissionWavelengths.add(null);
}
emissionWavelengths.set(index, parseWavelength(
value, getEmissionWavelengthUnitXsdDefault()));
} else if (key.startsWith("excitation_")) {
int index = Integer.parseInt(key.substring(key.indexOf('_') + 1));
while (index >= excitationWavelengths.size()) {
excitationWavelengths.add(null);
}
excitationWavelengths.set(index, parseWavelength(
value, getExcitationWavelengthUnitXsdDefault()));
} else if (key.equals("sleepOpenBytes")) {
sleepOpenBytes = intValue;
} else if (key.equals("sleepInitFile")) {
Expand Down Expand Up @@ -834,6 +854,8 @@ else if (dimOrder == null) {
populateSPW(store, screens, plates, plateRows, plateCols, fields, plateAcqs, withMicrobeam);
if (imageCount > 0) seriesCount = imageCount;
else hasSPW = false; // failed to generate SPW metadata
} else if (withInstrument) {
populateInstrument(store);
}

// populate core metadata
Expand Down Expand Up @@ -895,6 +917,12 @@ else if (dimOrder == null) {
if (channel != null) {
store.setChannelColor(channel, currentImageIndex, c);
}
if (c < emissionWavelengths.size() && emissionWavelengths.get(c) != null) {
store.setChannelEmissionWavelength(emissionWavelengths.get(c), currentImageIndex, c);
}
if (c < excitationWavelengths.size() && excitationWavelengths.get(c) != null) {
store.setChannelExcitationWavelength(excitationWavelengths.get(c), currentImageIndex, c);
}
}
fillAnnotations(store, currentImageIndex);
fillRegions(store, currentImageIndex);
Expand Down Expand Up @@ -1376,6 +1404,15 @@ private int populateSPW(MetadataStore store, int screens, int plates, int rows,
return ome.sizeOfImageList();
}

private void populateInstrument(MetadataStore store)
{
final XMLMockObjects xml = new XMLMockObjects();
OME ome = xml.getRoot();
ome.addInstrument(xml.createInstrument(true));
getOmeXmlMetadata().setRoot(new OMEXMLMetadataRoot(ome));
getOmeXmlService().convertMetadata(omeXmlMetadata, store);
}

/** Creates a mapping between indices and color values. */
private void createIndexMap(int num) {
int sizeC = core.get(0).sizeC;
Expand Down Expand Up @@ -1484,4 +1521,16 @@ private Length parsePhysicalSize(String s, String defaultUnit) {
return physicalSize;
}

private Length parseWavelength(String s, String defaultUnit) {
Length wavelength = FormatTools.parseLength(s, defaultUnit);
if (wavelength == null) {
throw new RuntimeException("Invalid wavelength: " + s);
}
if (!FormatTools.isPositiveValue(wavelength.value().doubleValue())) {
LOGGER.warn("Invalid wavelength value: {}", wavelength.value());
return null;
}
return wavelength;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,52 @@ public void testInvalidDimensionOrder() throws Exception {
reader.setId("test&dimOrder=CXYZT.fake");
}

@Test
public void testExcitationWavelengths() throws Exception {
File fakeIni = mkIni("excitationWavelengths.fake.ini",
"sizeC=5",
"excitation_0 = 502nm",
"excitation_1 = 502.0nm",
"excitation_2 = 502",
"excitation_4 = 5020Å");
reader.setId(wd.resolve("excitationWavelengths.fake").toString());
assertEquals(reader.getSizeC(), 5);
m = service.asRetrieve(reader.getMetadataStore());
assertTrue(service.validateOMEXML(service.getOMEXML(m)));
assertEquals(m.getChannelExcitationWavelength(0, 0), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelExcitationWavelength(0, 1), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelExcitationWavelength(0, 2), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelExcitationWavelength(0, 3), null);
assertEquals(m.getChannelExcitationWavelength(0, 4), new Length(5020.0, UNITS.ANGSTROM));
reader.close();
}

@Test
public void testEmissionWavelengths() throws Exception {
File fakeIni = mkIni("emissionWavelengths.fake.ini",
"sizeC=5",
"emission_0 = 502nm",
"emission_1 = 502.0nm",
"emission_2 = 502",
"emission_4 = 5020Å");
reader.setId(wd.resolve("emissionWavelengths.fake").toString());
assertEquals(reader.getSizeC(), 5);
m = service.asRetrieve(reader.getMetadataStore());
assertTrue(service.validateOMEXML(service.getOMEXML(m)));
assertEquals(m.getChannelEmissionWavelength(0, 0), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelEmissionWavelength(0, 1), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelEmissionWavelength(0, 2), new Length(502.0, UNITS.NANOMETER));
assertEquals(m.getChannelEmissionWavelength(0, 3), null);
assertEquals(m.getChannelEmissionWavelength(0, 4), new Length(5020.0, UNITS.ANGSTROM));
reader.close();
}

@Test
public void testInstrument() throws Exception {
reader.setId("test&withInstrument=true.fake");
m = service.asRetrieve(reader.getMetadataStore());
assertTrue(service.validateOMEXML(service.getOMEXML(m)));
assertEquals(m.getInstrumentCount(), 1);
reader.close();
}
}