Skip to content

Commit

Permalink
Merge pull request #4252 from melissalinkert/tcs-used-files
Browse files Browse the repository at this point in the history
Leica TCS: make sure XML file is on used files list
  • Loading branch information
sbesson authored Nov 20, 2024
2 parents ee508a0 + 10b48d2 commit 862723a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
61 changes: 53 additions & 8 deletions components/formats-gpl/src/loci/formats/in/LeicaHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public class LeicaHandler extends BaseHandler {
private MetadataLevel level;
private int laserCount = 0;

private boolean setPlaneMetadataInStore = true;
private Map<String, Time> exposureTimes = new HashMap<String, Time>();
private Map<String, Time> deltaT = new HashMap<String, Time>();

// -- Constructor --

public LeicaHandler(MetadataStore store, MetadataLevel level) {
Expand All @@ -136,6 +140,11 @@ public LeicaHandler(MetadataStore store, MetadataLevel level) {
this.level = level;
}

public LeicaHandler(MetadataStore store, MetadataLevel level, boolean planesInStore) {
this(store, level);
setPlaneMetadataInStore = planesInStore;
}

// -- LeicaHandler API methods --

public List<CoreMetadata> getCoreMetadataList() { return core; }
Expand All @@ -144,6 +153,10 @@ public LeicaHandler(MetadataStore store, MetadataLevel level) {

public Vector<String> getLutNames() { return lutNames; }

public Map<String, Time> getExposureTimes() { return exposureTimes; }

public Map<String, Time> getDeltaT() { return deltaT; }

// -- DefaultHandler API methods --

@Override
Expand Down Expand Up @@ -202,8 +215,10 @@ public void endElement(String uri, String localName, String qName) {
if (level != MetadataLevel.MINIMUM) {
int nChannels = coreMeta.rgb ? 0 : numChannels;

for (int c=0; c<nChannels; c++) {
store.setChannelPinholeSize(new Length(pinhole, UNITS.MICROMETER), numDatasets, c);
if (pinhole != null) {
for (int c=0; c<nChannels; c++) {
store.setChannelPinholeSize(new Length(pinhole, UNITS.MICROMETER), numDatasets, c);
}
}

for (int i=0; i<xPos.size(); i++) {
Expand Down Expand Up @@ -256,8 +271,10 @@ else if (qName.equals("Element") && level != MetadataLevel.MINIMUM) {
String id = MetadataTools.createLSID("Detector", numDatasets, index);
store.setDetectorSettingsID(id, numDatasets, index);
}
for (int c=0; c<nChannels; c++) {
store.setChannelPinholeSize(new Length(pinhole, UNITS.MICROMETER), numDatasets, c);
if (pinhole != null) {
for (int c=0; c<nChannels; c++) {
store.setChannelPinholeSize(new Length(pinhole, UNITS.MICROMETER), numDatasets, c);
}
}
}
}
Expand Down Expand Up @@ -545,7 +562,13 @@ else if (id.indexOf("WFC") == 1) {
try {
Double exposureTime = DataTools.parseDouble(value);
if (exposureTime != null) {
store.setPlaneExposureTime(new Time(exposureTime, UNITS.SECOND), numDatasets, c);
Time expTime = new Time(exposureTime, UNITS.SECOND);
if (setPlaneMetadataInStore) {
store.setPlaneExposureTime(expTime, numDatasets, c);
}
else {
exposureTimes.put(numDatasets + "-" + c, expTime);
}
}
}
catch (IndexOutOfBoundsException e) { }
Expand Down Expand Up @@ -874,14 +897,28 @@ else if (qName.equals("TimeStamp") && numDatasets >= 0) {
store.setImageAcquisitionDate(new Timestamp(date), numDatasets);
}
firstStamp = ms;
store.setPlaneDeltaT(new Time(0.0, UNITS.SECOND), numDatasets, count);

Time stamp = new Time(0.0, UNITS.SECOND);
if (setPlaneMetadataInStore) {
store.setPlaneDeltaT(stamp, numDatasets, count);
}
else {
deltaT.put(numDatasets + "-" + count, stamp);
}
}
else if (level != MetadataLevel.MINIMUM) {
CoreMetadata coreMeta = core.get(numDatasets);
int nImages = coreMeta.sizeZ * coreMeta.sizeT * coreMeta.sizeC;
if (count < nImages) {
ms -= firstStamp;
store.setPlaneDeltaT(new Time(ms / 1000.0, UNITS.SECOND), numDatasets, count);

Time stamp = new Time(ms / 1000.00, UNITS.SECOND);
if (setPlaneMetadataInStore) {
store.setPlaneDeltaT(stamp, numDatasets, count);
}
else {
deltaT.put(numDatasets + "-" + count, stamp);
}
}
}

Expand All @@ -893,7 +930,15 @@ else if (qName.equals("RelTimeStamp") && level != MetadataLevel.MINIMUM) {
if (count < nImages) {
Double time = DataTools.parseDouble(attributes.getValue("Time"));
if (time != null) {
store.setPlaneDeltaT(new Time(time, UNITS.SECOND), numDatasets, count++);
Time stamp = new Time(time, UNITS.SECOND);
if (setPlaneMetadataInStore) {
store.setPlaneDeltaT(stamp, numDatasets, count);
}
else {
deltaT.put(numDatasets + "-" + count, stamp);
}

count++;
}
}
}
Expand Down
33 changes: 29 additions & 4 deletions components/formats-gpl/src/loci/formats/in/TCSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import loci.common.DataTools;
import loci.common.DateTools;
Expand All @@ -49,6 +50,7 @@
import loci.formats.tiff.TiffParser;

import ome.units.quantity.Length;
import ome.units.quantity.Time;

/**
* TCSReader is the file format reader for Leica TCS TIFF files and their
Expand Down Expand Up @@ -246,12 +248,15 @@ protected void initFile(String id) throws FormatException, IOException {
String[] list = parent.list();
Arrays.sort(list);

String currentXMLFile = null;

boolean isXML = checkSuffix(id, XML_SUFFIX);
if (isXML) currentXMLFile = l.getAbsolutePath();

if (list != null) {
for (String file : list) {
if (checkSuffix(file, XML_SUFFIX) && !isXML && isGroupFiles()) {
xmlFile = new Location(parent, file).getAbsolutePath();
currentXMLFile = new Location(parent, file).getAbsolutePath();
break;
}
else if (checkSuffix(file, TiffReader.TIFF_SUFFIXES) && isXML) {
Expand All @@ -261,9 +266,11 @@ else if (checkSuffix(file, TiffReader.TIFF_SUFFIXES) && isXML) {
}
}

if (isXML) xmlFile = l.getAbsolutePath();

// super.initFile(...) calls close, which resets xmlFile
// so xmlFile must be set after super.initFile(...) returns,
// otherwise the XML file will not appear on the used files list
super.initFile(id);
xmlFile = currentXMLFile;

MetadataStore store = makeFilterMetadata();

Expand Down Expand Up @@ -460,15 +467,19 @@ else if (getSizeT() == 1) {
else ms0.sizeZ *= ifds.size();
}

Map<String, Time> exposureTime = null;
Map<String, Time> deltaT = null;
if (xmlFile != null) {
// parse XML metadata

String xml = DataTools.readFile(xmlFile);
xml = XMLTools.sanitizeXML(PREFIX + xml + SUFFIX);

LeicaHandler handler =
new LeicaHandler(store, getMetadataOptions().getMetadataLevel());
new LeicaHandler(store, getMetadataOptions().getMetadataLevel(), false);
XMLTools.parseXML(xml, handler);
exposureTime = handler.getExposureTimes();
deltaT = handler.getDeltaT();

metadata = handler.getGlobalMetadata();
MetadataTools.merge(handler.getGlobalMetadata(), metadata, "");
Expand Down Expand Up @@ -504,6 +515,20 @@ else if (getSizeT() == 1) {
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}

for (int s=0; s<getSeriesCount(); s++) {
setSeries(s);
for (int i=0; i<getImageCount(); i++) {
String key = s + "-" + i;
if (exposureTime != null && exposureTime.containsKey(key)) {
store.setPlaneExposureTime(exposureTime.get(key), s, i);
}
if (deltaT != null && deltaT.containsKey(key)) {
store.setPlaneDeltaT(deltaT.get(key), s, i);
}
}
}
setSeries(0);
}

// -- Helper methods --
Expand Down

0 comments on commit 862723a

Please sign in to comment.