use of loci.formats.AxisGuesser in project bioformats by openmicroscopy.
the class LeicaReader method parseFilenames.
private void parseFilenames(int seriesIndex) throws IOException {
int maxPlanes = 0;
final List<String> f = new ArrayList<String>();
int tempImages = in.readInt();
if (((long) tempImages * nameLength) > in.length()) {
in.order(!isLittleEndian());
tempImages = in.readInt();
in.order(isLittleEndian());
}
CoreMetadata ms = core.get(seriesIndex);
ms.sizeX = in.readInt();
ms.sizeY = in.readInt();
in.skipBytes(4);
int samplesPerPixel = in.readInt();
ms.rgb = samplesPerPixel > 1;
ms.sizeC = samplesPerPixel;
boolean tiffsExist = false;
String dirPrefix = new Location(currentId).getAbsoluteFile().getParent();
if (!dirPrefix.endsWith(File.separator))
dirPrefix += File.separator;
String prefix = "";
for (int j = 0; j < tempImages; j++) {
// read in each filename
prefix = getString(nameLength);
f.add(dirPrefix + prefix);
// test to make sure the path is valid
Location test = new Location(f.get(f.size() - 1)).getAbsoluteFile();
LOGGER.debug("Expected to find TIFF file {}", test.getAbsolutePath());
if (!test.exists()) {
LOGGER.debug(" file does not exist");
}
if (!tiffsExist)
tiffsExist = test.exists();
}
if (!tiffsExist) {
// Strategy for handling renamed files:
// 1) Assume that files for each series follow a pattern.
// 2) Assign each file group to the first series with the correct count.
LOGGER.info("Handling renamed TIFF files");
String[] listing = getTIFFList();
// grab the file patterns
final List<String> filePatterns = new ArrayList<String>();
for (String q : listing) {
Location l = new Location(q).getAbsoluteFile();
if (!l.exists()) {
l = new Location(dirPrefix, q).getAbsoluteFile();
}
FilePattern pattern = new FilePattern(l);
if (!pattern.isValid())
continue;
AxisGuesser guess = new AxisGuesser(pattern, "XYZCT", 1, 1, 1, false);
String fp = pattern.getPattern();
if (guess.getAxisCountS() >= 1) {
String pre = pattern.getPrefix(guess.getAxisCountS());
final List<String> fileList = new ArrayList<String>();
for (int n = 0; n < listing.length; n++) {
Location p = new Location(dirPrefix, listing[n]);
if (p.getAbsolutePath().startsWith(pre)) {
fileList.add(listing[n]);
}
}
fp = FilePattern.findPattern(l.getAbsolutePath(), dirPrefix, fileList.toArray(new String[fileList.size()]));
}
if (fp != null && !filePatterns.contains(fp)) {
filePatterns.add(fp);
}
}
for (String q : filePatterns) {
String[] pattern = new FilePattern(q).getFiles();
if (pattern.length == tempImages) {
// make sure that this pattern hasn't already been used
boolean validPattern = true;
for (int n = 0; n < seriesIndex; n++) {
if (files[n] == null)
continue;
if (files[n].contains(pattern[0])) {
validPattern = false;
break;
}
}
if (validPattern) {
files[seriesIndex] = new ArrayList<String>();
files[seriesIndex].addAll(Arrays.asList(pattern));
}
}
}
} else {
files[seriesIndex] = f;
for (int s = 0; s < seriesIndex; s++) {
if (files[s] != null) {
if (files[s].get(0).equals(f.get(0))) {
valid[s] = false;
files[s] = null;
}
}
}
}
if (files[seriesIndex] == null)
valid[seriesIndex] = false;
else {
ms.imageCount = files[seriesIndex].size();
maxPlanes = (int) Math.max(maxPlanes, ms.imageCount);
}
}
use of loci.formats.AxisGuesser in project bioformats by openmicroscopy.
the class AxisGuesserTest method check.
private void check(// IN
String p, // IN
String order, // IN
int sZ, // IN
int sT, // IN
int sC, // IN
boolean cert, String newOrder, int[] types) {
// OUT
FilePattern fp = new FilePattern(p);
AxisGuesser ag = new AxisGuesser(fp, order, sZ, sT, sC, cert);
assertEquals(ag.getFilePattern().getPattern(), p);
assertEquals(ag.getOriginalOrder(), order);
assertEquals(ag.getAdjustedOrder(), newOrder);
assertEquals(ag.getAxisTypes(), types);
checkAxisCount(ag, types);
}
use of loci.formats.AxisGuesser in project bioformats by openmicroscopy.
the class TCSReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
Location l = new Location(id).getAbsoluteFile();
Location parent = l.getParentFile();
String[] list = parent.list();
Arrays.sort(list);
boolean isXML = checkSuffix(id, XML_SUFFIX);
if (list != null) {
for (String file : list) {
if (checkSuffix(file, XML_SUFFIX) && !isXML && isGroupFiles()) {
xmlFile = new Location(parent, file).getAbsolutePath();
break;
} else if (checkSuffix(file, TiffReader.TIFF_SUFFIXES) && isXML) {
initFile(new Location(parent, file).getAbsolutePath());
return;
}
}
}
if (isXML)
xmlFile = l.getAbsolutePath();
super.initFile(id);
MetadataStore store = makeFilterMetadata();
in = new RandomAccessInputStream(id, 16);
tiffParser = new TiffParser(in);
tiffs = new ArrayList<String>();
IFDList ifds = tiffParser.getIFDs();
String date = ifds.get(0).getIFDStringValue(IFD.DATE_TIME);
if (date != null) {
datestamp = DateTools.getTime(date, "yyyy:MM:dd HH:mm:ss");
}
groupFiles();
addGlobalMeta("Number of image files", tiffs.size());
tiffReaders = new TiffReader[tiffs.size()];
for (int i = 0; i < tiffReaders.length; i++) {
tiffReaders[i] = new TiffReader();
}
tiffReaders[0].setId(tiffs.get(0));
int[] ch = new int[ifds.size()];
int[] idx = new int[ifds.size()];
long[] stamp = new long[ifds.size()];
int channelCount = 0;
CoreMetadata ms0 = core.get(0);
ms0.sizeZ = 1;
ms0.sizeC = tiffReaders[0].getSizeC();
ms0.dimensionOrder = isRGB() ? "XYC" : "XY";
if (isGroupFiles()) {
try {
FilePattern fp = new FilePattern(new Location(currentId).getAbsoluteFile());
AxisGuesser guesser = new AxisGuesser(fp, "XYTZC", 1, ifds.size(), 1, true);
int[] axisTypes = guesser.getAxisTypes();
int[] count = fp.getCount();
for (int i = axisTypes.length - 1; i >= 0; i--) {
if (axisTypes[i] == AxisGuesser.Z_AXIS) {
if (getDimensionOrder().indexOf('Z') == -1) {
ms0.dimensionOrder += 'Z';
}
ms0.sizeZ *= count[i];
} else if (axisTypes[i] == AxisGuesser.C_AXIS) {
if (getDimensionOrder().indexOf('C') == -1) {
ms0.dimensionOrder += 'C';
}
ms0.sizeC *= count[i];
}
}
} catch (NullPointerException e) {
}
}
for (int i = 0; i < ifds.size(); i++) {
String document = ifds.get(i).getIFDStringValue(IFD.DOCUMENT_NAME);
if (document == null)
continue;
int index = document.indexOf("INDEX");
String s = document.substring(8, index).trim();
ch[i] = Integer.parseInt(s);
if (ch[i] > channelCount)
channelCount = ch[i];
int space = document.indexOf(" ", index + 6);
if (space < 0)
continue;
String n = document.substring(index + 6, space).trim();
idx[i] = Integer.parseInt(n);
date = document.substring(space, document.indexOf("FORMAT")).trim();
stamp[i] = DateTools.getTime(date, DATE_FORMAT, ".");
addGlobalMetaList("Timestamp for plane", stamp[i]);
}
ms0.sizeT = 0;
// determine the axis sizes and ordering
boolean unique = true;
for (int i = 0; i < stamp.length; i++) {
for (int j = i + 1; j < stamp.length; j++) {
if (stamp[j] == stamp[i]) {
unique = false;
break;
}
}
if (unique) {
ms0.sizeT++;
if (getDimensionOrder().indexOf('T') < 0) {
ms0.dimensionOrder += 'T';
}
} else if (i > 0) {
if ((ch[i] != ch[i - 1]) && getDimensionOrder().indexOf('C') < 0) {
ms0.dimensionOrder += 'C';
} else if (getDimensionOrder().indexOf('Z') < 0) {
ms0.dimensionOrder += 'Z';
}
}
unique = true;
}
if (getDimensionOrder().indexOf('Z') < 0)
ms0.dimensionOrder += 'Z';
if (getDimensionOrder().indexOf('C') < 0)
ms0.dimensionOrder += 'C';
if (getDimensionOrder().indexOf('T') < 0)
ms0.dimensionOrder += 'T';
if (getSizeC() == 0)
ms0.sizeC = 1;
if (getSizeT() == 0)
ms0.sizeT = 1;
if (channelCount == 0)
channelCount = 1;
if (getSizeZ() <= 1) {
ms0.sizeZ = ifds.size() / (getSizeT() * channelCount);
}
ms0.sizeC *= channelCount;
ms0.imageCount = getSizeZ() * getSizeT() * getSizeC();
// cut up comment
String comment = ifds.get(0).getComment();
if (comment != null && comment.startsWith("[") && getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String[] lines = comment.split("\n");
for (String line : lines) {
if (!line.startsWith("[")) {
int eq = line.indexOf('=');
if (eq < 0)
continue;
String key = line.substring(0, eq).trim();
String value = line.substring(eq + 1).trim();
if (key.equals("VoxelSizeX")) {
try {
voxelX = Double.parseDouble(value);
} catch (NumberFormatException e) {
}
} else if (key.equals("VoxelSizeY")) {
try {
voxelY = Double.parseDouble(value);
} catch (NumberFormatException e) {
}
} else if (key.equals("VoxelSizeZ")) {
try {
voxelZ = Double.parseDouble(value);
} catch (NumberFormatException e) {
}
}
addGlobalMeta(key, value);
}
}
metadata.remove("Comment");
}
ms0.sizeX = tiffReaders[0].getSizeX();
ms0.sizeY = tiffReaders[0].getSizeY();
ms0.rgb = tiffReaders[0].isRGB();
ms0.pixelType = tiffReaders[0].getPixelType();
ms0.littleEndian = tiffReaders[0].isLittleEndian();
ms0.interleaved = tiffReaders[0].isInterleaved();
ms0.falseColor = true;
ms0.indexed = tiffReaders[0].isIndexed();
if (isRGB())
ms0.imageCount /= (getSizeC() / channelCount);
if (getSizeZ() * getSizeT() * getEffectiveSizeC() != (ifds.size() * tiffReaders.length)) {
int c = getEffectiveSizeC();
if (c == 0)
c = 1;
ms0.sizeT = (ifds.size() * tiffReaders.length) / (c * getSizeZ());
ms0.imageCount = getSizeT() * c * getSizeZ();
if (getSizeT() == 0) {
ms0.sizeT = 1;
ms0.imageCount = ifds.size() * tiffReaders.length;
}
}
if (getImageCount() == ifds.size() * getSizeZ() * getSizeT() && ifds.size() > 1) {
if (getSizeZ() == 1) {
ms0.sizeZ = ifds.size();
} else if (getSizeT() == 1) {
ms0.sizeT = ifds.size();
} else
ms0.sizeZ *= ifds.size();
}
if (xmlFile != null) {
// parse XML metadata
String xml = DataTools.readFile(xmlFile);
xml = XMLTools.sanitizeXML(PREFIX + xml + SUFFIX);
LeicaHandler handler = new LeicaHandler(store, getMetadataOptions().getMetadataLevel());
XMLTools.parseXML(xml, handler);
metadata = handler.getGlobalMetadata();
MetadataTools.merge(handler.getGlobalMetadata(), metadata, "");
core = handler.getCoreMetadataList();
for (int i = 0; i < getSeriesCount(); i++) {
CoreMetadata ms = core.get(i);
if (tiffs.size() < ms.imageCount) {
int div = ms.imageCount / ms.sizeC;
ms.imageCount = tiffs.size();
if (div >= ms.sizeZ)
ms.sizeZ /= div;
else if (div >= ms.sizeT)
ms.sizeT /= div;
}
ms.dimensionOrder = getSizeZ() > getSizeT() ? "XYCZT" : "XYCTZ";
ms.rgb = false;
ms.interleaved = false;
ms.indexed = tiffReaders[0].isIndexed();
}
}
MetadataTools.populatePixels(store, this, true);
Length sizeX = FormatTools.getPhysicalSizeX(voxelX);
Length sizeY = FormatTools.getPhysicalSizeY(voxelY);
Length sizeZ = FormatTools.getPhysicalSizeZ(voxelZ);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}
}
Aggregations