use of loci.formats.IFormatReader in project bioformats by openmicroscopy.
the class FormatReaderTest method testSaneUsedFiles.
@Test(groups = { "all", "type", "automated" })
public void testSaneUsedFiles() {
if (!initFile())
return;
String file = reader.getCurrentFile();
String testName = "testSaneUsedFiles";
boolean success = true;
String msg = null;
try {
String[] base = reader.getUsedFiles();
if (base.length == 1) {
if (!base[0].equals(file))
success = false;
} else {
Arrays.sort(base);
IFormatReader r = /*config.noStitching() ? new ImageReader() :*/
new FileStitcher();
int maxFiles = (int) Math.min(base.length, 100);
if (DataTools.indexOf(reader.getDomains(), FormatTools.HCS_DOMAIN) >= 0 || file.toLowerCase().endsWith(".czi")) {
maxFiles = (int) Math.min(maxFiles, 10);
}
for (int i = 0; i < maxFiles && success; i++) {
// than one dataset
if (reader.getFormat().equals("InCell 1000/2000")) {
if (!base[i].toLowerCase().endsWith(".xdce") && !base[i].toLowerCase().endsWith(".xml")) {
continue;
}
}
// Volocity datasets can only be detected with the .mvd2 file
if (file.toLowerCase().endsWith(".mvd2") && !base[i].toLowerCase().endsWith(".mvd2")) {
continue;
}
// 'fid' and 'acqp' files
if ((file.toLowerCase().endsWith("fid") || file.toLowerCase().endsWith("acqp")) && !base[i].toLowerCase().endsWith("fid") && !base[i].toLowerCase().endsWith("acqp") && reader.getFormat().equals("Bruker")) {
continue;
}
// CellR datasets cannot be detected with a TIFF file
if (reader.getFormat().equals("Olympus APL") && base[i].toLowerCase().endsWith("tif")) {
continue;
}
// Micromanager datasets cannot be detected with an OME-TIFF file
if (reader.getFormat().equals("Micro-Manager") && (base[i].toLowerCase().endsWith(".ome.tiff") || base[i].toLowerCase().endsWith(".ome.tif"))) {
continue;
}
// DICOM companion files may not be detected
if (reader.getFormat().equals("DICOM") && !base[i].equals(file)) {
continue;
}
// QuickTime resource forks are not detected
if (reader.getFormat().equals("QuickTime") && !base[i].equals(file)) {
continue;
}
// SVS files in AFI datasets are detected as SVS
if (reader.getFormat().equals("Aperio AFI") && base[i].toLowerCase().endsWith(".svs")) {
continue;
}
if (reader.getFormat().equals("BD Pathway") && (base[i].endsWith(".adf") || base[i].endsWith(".txt")) || base[i].endsWith(".roi")) {
continue;
}
// Hamamatsu VMS datasets cannot be detected with non-.vms files
if (reader.getFormat().equals("Hamamatsu VMS") && !base[i].toLowerCase().endsWith(".vms")) {
continue;
}
if (reader.getFormat().equals("CellVoyager")) {
continue;
}
if (reader.getFormat().equals("Leica Image File Format")) {
continue;
}
// pattern datasets can only be detected with the pattern file
if (reader.getFormat().equals("File pattern")) {
continue;
}
r.setId(base[i]);
String[] comp = r.getUsedFiles();
// are not grouped. This is expected behavior; see ticket #3701.
if (base[i].toLowerCase().endsWith(".lsm") && comp.length == 1) {
r.close();
continue;
}
// will give different results.
if (file.toLowerCase().endsWith(".dv") && base[i].toLowerCase().endsWith(".log")) {
r.close();
continue;
}
// used file count from the text file.
if (reader.getFormat().equals("Hitachi")) {
r.close();
continue;
}
// separately
if (reader.getFormat().equals("Trestle")) {
r.close();
continue;
}
// TIFF files in CellR datasets are detected separately
if (reader.getFormat().equals("Olympus APL") && base[i].toLowerCase().endsWith(".tif")) {
r.close();
continue;
}
// TIFF files in Li-Cor datasets are detected separately
if (reader.getFormat().equals("Li-Cor L2D") && !base[i].toLowerCase().endsWith("l2d")) {
r.close();
continue;
}
// TIFF files in Prairie datasets may be detected as OME-TIFF
if (reader.getFormat().equals("Prairie TIFF") && base[i].toLowerCase().endsWith(".tif") && r.getFormat().equals("OME-TIFF")) {
r.close();
continue;
}
if (reader.getFormat().equals("Hamamatsu NDPIS") && r.getFormat().equals("Hamamatsu NDPI")) {
r.close();
continue;
}
if (base[i].endsWith(".bmp") && reader.getFormat().equals("BD Pathway")) {
r.close();
continue;
}
if (comp.length != base.length) {
success = false;
msg = base[i] + " (file list length was " + comp.length + "; expected " + base.length + ")";
}
if (success)
Arrays.sort(comp);
// a test failure (which we can do nothing about).
if (file.toLowerCase().endsWith(".nhdr") || base[i].toLowerCase().endsWith(".nhdr")) {
r.close();
continue;
}
for (int j = 0; j < comp.length && success; j++) {
if (!comp[j].equals(base[j])) {
if (base[j].equals(new Location(comp[j]).getCanonicalPath())) {
continue;
}
success = false;
msg = base[i] + "(file @ " + j + " was '" + comp[j] + "', expected '" + base[j] + "')";
}
}
r.close();
}
}
} catch (Throwable t) {
LOGGER.info("", t);
success = false;
}
result(testName, success, msg);
}
use of loci.formats.IFormatReader in project bioformats by openmicroscopy.
the class FormatReaderTest method testUnflattenedSubimagePixelsHashes.
/*
@Test(groups = {"all", "pixels"})
public void testReorderedPixelsHashes() {
if (config == null) throw new SkipException("No config tree");
String testName = "testReorderedPixelsHashes";
if (!initFile()) result(testName, false, "initFile");
boolean success = true;
String msg = null;
try {
for (int i=0; i<reader.getSeriesCount() && success; i++) {
reader.setSeries(i);
config.setSeries(i);
for (int j=0; j<3; j++) {
int index = (int) (Math.random() * reader.getImageCount());
reader.openBytes(index);
}
String md5 = TestTools.md5(reader.openBytes(0));
String expected1 = config.getMD5();
String expected2 = config.getAlternateMD5();
if (!md5.equals(expected1) && !md5.equals(expected2)) {
success = false;
msg = expected1 == null && expected2 == null ? "no configuration" :
"series " + i;
}
}
}
catch (Throwable t) {
LOGGER.info("", t);
success = false;
}
result(testName, success, msg);
}
*/
@Test(groups = { "all", "pixels", "automated" })
public void testUnflattenedSubimagePixelsHashes() {
if (config == null)
throw new SkipException("No config tree");
String testName = "testUnflattenedSubimagePixelsHashes";
if (!initFile())
result(testName, false, "initFile");
boolean success = true;
String msg = null;
try {
IFormatReader resolutionReader = new BufferedImageReader(new FileStitcher());
resolutionReader.setFlattenedResolutions(false);
resolutionReader.setNormalized(true);
resolutionReader.setOriginalMetadataPopulated(false);
resolutionReader.setMetadataFiltered(true);
resolutionReader.setId(id);
// check the MD5 of the first plane in each resolution
for (int i = 0; i < resolutionReader.getSeriesCount() && success; i++) {
resolutionReader.setSeries(i);
for (int r = 0; r < resolutionReader.getResolutionCount() && success; r++) {
resolutionReader.setResolution(r);
config.setSeries(resolutionReader.getCoreIndex());
int w = (int) Math.min(Configuration.TILE_SIZE, resolutionReader.getSizeX());
int h = (int) Math.min(Configuration.TILE_SIZE, resolutionReader.getSizeY());
String expected1 = config.getTileMD5();
String expected2 = config.getTileAlternateMD5();
String md5 = null;
try {
md5 = TestTools.md5(resolutionReader.openBytes(0, 0, 0, w, h));
} catch (Throwable e) {
if (TestTools.isOutOfMemory(e)) {
result(testName, true, "Image too large");
return;
}
LOGGER.warn("", e);
}
if (md5 == null && expected1 == null && expected2 == null) {
success = true;
} else if (!md5.equals(expected1) && !md5.equals(expected2) && (expected1 != null || expected2 != null)) {
success = false;
msg = "series " + i + ", resolution " + r;
}
}
}
resolutionReader.close();
} catch (Throwable t) {
LOGGER.info("", t);
success = false;
}
result(testName, success, msg);
}
use of loci.formats.IFormatReader in project bioformats by openmicroscopy.
the class FormatReaderTest method testOMEXML.
@Test(groups = { "all", "xml", "fast", "automated" })
public void testOMEXML() {
String testName = "testOMEXML";
if (!initFile())
result(testName, false, "initFile");
String msg = null;
try {
MetadataRetrieve retrieve = (MetadataRetrieve) reader.getMetadataStore();
boolean success = omexmlService.isOMEXMLMetadata(retrieve);
if (!success)
msg = TestTools.shortClassName(retrieve);
for (int i = 0; i < reader.getSeriesCount() && msg == null; i++) {
reader.setSeries(i);
String type = FormatTools.getPixelTypeString(reader.getPixelType());
if (reader.getSizeX() != retrieve.getPixelsSizeX(i).getValue().intValue()) {
msg = "SizeX";
}
if (reader.getSizeY() != retrieve.getPixelsSizeY(i).getValue().intValue()) {
msg = "SizeY";
}
if (reader.getSizeZ() != retrieve.getPixelsSizeZ(i).getValue().intValue()) {
msg = "SizeZ";
}
if (reader.getSizeC() != retrieve.getPixelsSizeC(i).getValue().intValue()) {
msg = "SizeC";
}
if (reader.getSizeT() != retrieve.getPixelsSizeT(i).getValue().intValue()) {
msg = "SizeT";
}
// NB: OME-TIFF files do not have a BinData element under Pixels
IFormatReader r = reader.unwrap();
if (r instanceof FileStitcher)
r = ((FileStitcher) r).getReader();
if (r instanceof ReaderWrapper)
r = ((ReaderWrapper) r).unwrap();
if (!(r instanceof OMETiffReader)) {
boolean littleEndian = false;
if (retrieve.getPixelsBigEndian(i) != null) {
littleEndian = !retrieve.getPixelsBigEndian(i).booleanValue();
} else if (retrieve.getPixelsBinDataCount(i) == 0) {
littleEndian = !retrieve.getPixelsBinDataBigEndian(i, 0).booleanValue();
}
if (reader.isLittleEndian() != littleEndian) {
msg = "BigEndian";
}
}
if (!reader.getDimensionOrder().equals(retrieve.getPixelsDimensionOrder(i).toString())) {
msg = "DimensionOrder";
}
if (!type.equalsIgnoreCase(retrieve.getPixelsType(i).toString())) {
msg = "PixelType";
}
}
} catch (Throwable t) {
LOGGER.info("", t);
msg = t.getMessage();
}
result(testName, msg == null, msg);
}
use of loci.formats.IFormatReader in project bioformats by openmicroscopy.
the class FormatReaderTest method testRequiredDirectories.
@Test(groups = { "all", "type", "automated" })
public void testRequiredDirectories() {
if (!initFile())
return;
if (reader.getFormat().equals("Woolz") || reader.getFormat().startsWith("CellH5")) {
throw new SkipException(SKIP_MESSAGE);
}
String testName = "testRequiredDirectories";
String file = reader.getCurrentFile();
LOGGER.debug("testRequiredDirectories({})", file);
int directories = -1;
try {
directories = reader.getRequiredDirectories(reader.getUsedFiles());
} catch (Exception e) {
LOGGER.warn("Could not retrieve directory count", e);
}
LOGGER.debug("directories = {}", directories);
if (directories < 0) {
result(testName, false, "Invalid directory count (" + directories + ")");
} else {
// make sure the directory count is not too small
// we can't reliably test for the directory count being too large,
// since a different fileset in the same format may need more directories
String[] usedFiles = reader.getUsedFiles();
String[] newFiles = new String[usedFiles.length];
// find the common parent
String commonParent = new Location(usedFiles[0]).getAbsoluteFile().getParent();
for (int i = 1; i < usedFiles.length; i++) {
while (!usedFiles[i].startsWith(commonParent)) {
commonParent = commonParent.substring(0, commonParent.lastIndexOf(File.separator));
}
}
LOGGER.debug("commonParent = {}", commonParent);
// remove extra directories
String split = File.separatorChar == '\\' ? "\\\\" : File.separator;
LOGGER.debug("split = {}", split);
String[] f = commonParent.split(split);
StringBuilder toRemove = new StringBuilder();
for (int i = 0; i < f.length - directories - 1; i++) {
toRemove.append(f[i]);
if (i < f.length - directories - 2) {
toRemove.append(split);
}
}
// map new file names and verify that setId still works
String newFile = null;
for (int i = 0; i < usedFiles.length; i++) {
newFiles[i] = usedFiles[i].replaceAll(toRemove.toString(), "");
LOGGER.debug("mapping {} to {}", newFiles[i], usedFiles[i]);
Location.mapId(newFiles[i], usedFiles[i]);
if (usedFiles[i].equals(file)) {
newFile = newFiles[i];
}
}
if (newFile == null) {
newFile = newFiles[0];
}
LOGGER.debug("newFile = {}", newFile);
IFormatReader check = new FileStitcher();
try {
check.setId(newFile);
int nFiles = check.getUsedFiles().length;
result(testName, nFiles == usedFiles.length, "Found " + nFiles + "; expected " + usedFiles.length);
} catch (Exception e) {
LOGGER.info("Initialization failed", e);
result(testName, false, e.getMessage());
} finally {
try {
check.close();
} catch (IOException e) {
LOGGER.warn("Could not close reader", e);
}
for (int i = 0; i < newFiles.length; i++) {
Location.mapId(newFiles[i], null);
}
}
}
}
use of loci.formats.IFormatReader in project bioformats by openmicroscopy.
the class Configuration method populateINI.
// -- Helper methods --
private void populateINI(IFormatReader reader) {
IMetadata retrieve = (IMetadata) reader.getMetadataStore();
ini = new IniList();
IniTable globalTable = new IniTable();
putTableName(globalTable, reader, " global");
int seriesCount = reader.getSeriesCount();
globalTable.put(SERIES_COUNT, String.valueOf(seriesCount));
IFormatReader r = reader;
if (r instanceof ImageReader) {
r = ((ImageReader) r).getReader();
} else if (r instanceof ReaderWrapper) {
try {
r = ((ReaderWrapper) r).unwrap();
} catch (FormatException e) {
} catch (IOException e) {
}
}
globalTable.put(READER, TestTools.shortClassName(r));
globalTable.put(TEST, "true");
globalTable.put(MEMORY, String.valueOf(TestTools.getUsedMemory()));
long planeSize = (long) FormatTools.getPlaneSize(reader) * 3;
boolean canOpenImages = planeSize > 0 && TestTools.canFitInMemory(planeSize);
long t0 = System.currentTimeMillis();
if (canOpenImages) {
try {
reader.openBytes(0);
} catch (FormatException e) {
} catch (IOException e) {
}
}
long t1 = System.currentTimeMillis();
globalTable.put(ACCESS_TIME, String.valueOf(t1 - t0));
ini.add(globalTable);
for (int series = 0; series < seriesCount; series++) {
reader.setSeries(series);
IniTable seriesTable = new IniTable();
putTableName(seriesTable, reader, SERIES + series);
seriesTable.put(SIZE_X, String.valueOf(reader.getSizeX()));
seriesTable.put(SIZE_Y, String.valueOf(reader.getSizeY()));
seriesTable.put(SIZE_Z, String.valueOf(reader.getSizeZ()));
seriesTable.put(SIZE_C, String.valueOf(reader.getSizeC()));
seriesTable.put(SIZE_T, String.valueOf(reader.getSizeT()));
seriesTable.put(DIMENSION_ORDER, reader.getDimensionOrder());
seriesTable.put(IS_INTERLEAVED, String.valueOf(reader.isInterleaved()));
seriesTable.put(IS_INDEXED, String.valueOf(reader.isIndexed()));
seriesTable.put(IS_FALSE_COLOR, String.valueOf(reader.isFalseColor()));
seriesTable.put(IS_RGB, String.valueOf(reader.isRGB()));
seriesTable.put(THUMB_SIZE_X, String.valueOf(reader.getThumbSizeX()));
seriesTable.put(THUMB_SIZE_Y, String.valueOf(reader.getThumbSizeY()));
seriesTable.put(PIXEL_TYPE, FormatTools.getPixelTypeString(reader.getPixelType()));
seriesTable.put(IS_LITTLE_ENDIAN, String.valueOf(reader.isLittleEndian()));
seriesTable.put(CHANNEL_COUNT, String.valueOf(retrieve.getChannelCount(series)));
try {
planeSize = DataTools.safeMultiply32(reader.getSizeX(), reader.getSizeY(), reader.getEffectiveSizeC(), FormatTools.getBytesPerPixel(reader.getPixelType()));
canOpenImages = planeSize > 0 && TestTools.canFitInMemory(planeSize);
} catch (IllegalArgumentException e) {
canOpenImages = false;
}
if (canOpenImages) {
try {
byte[] plane = reader.openBytes(0);
seriesTable.put(MD5, TestTools.md5(plane));
} catch (FormatException e) {
// TODO
} catch (IOException e) {
// TODO
}
}
try {
int w = (int) Math.min(TILE_SIZE, reader.getSizeX());
int h = (int) Math.min(TILE_SIZE, reader.getSizeY());
byte[] tile = reader.openBytes(0, 0, 0, w, h);
seriesTable.put(TILE_MD5, TestTools.md5(tile));
} catch (FormatException e) {
// TODO
} catch (IOException e) {
// TODO
}
seriesTable.put(NAME, retrieve.getImageName(series));
seriesTable.put(DESCRIPTION, retrieve.getImageDescription(series));
Length physicalX = retrieve.getPixelsPhysicalSizeX(series);
if (physicalX != null) {
seriesTable.put(PHYSICAL_SIZE_X, physicalX.value().toString());
seriesTable.put(PHYSICAL_SIZE_X_UNIT, physicalX.unit().getSymbol());
}
Length physicalY = retrieve.getPixelsPhysicalSizeY(series);
if (physicalY != null) {
seriesTable.put(PHYSICAL_SIZE_Y, physicalY.value().toString());
seriesTable.put(PHYSICAL_SIZE_Y_UNIT, physicalY.unit().getSymbol());
}
Length physicalZ = retrieve.getPixelsPhysicalSizeZ(series);
if (physicalZ != null) {
seriesTable.put(PHYSICAL_SIZE_Z, physicalZ.value().toString());
seriesTable.put(PHYSICAL_SIZE_Z_UNIT, physicalZ.unit().getSymbol());
}
Time timeIncrement = retrieve.getPixelsTimeIncrement(series);
if (timeIncrement != null) {
seriesTable.put(TIME_INCREMENT, timeIncrement.value().toString());
seriesTable.put(TIME_INCREMENT_UNIT, timeIncrement.unit().getSymbol());
}
Timestamp acquisition = retrieve.getImageAcquisitionDate(series);
if (acquisition != null) {
String date = acquisition.getValue();
if (date != null) {
seriesTable.put(DATE, date);
}
}
for (int c = 0; c < retrieve.getChannelCount(series); c++) {
seriesTable.put(CHANNEL_NAME + c, retrieve.getChannelName(series, c));
try {
seriesTable.put(LIGHT_SOURCE + c, retrieve.getChannelLightSourceSettingsID(series, c));
} catch (NullPointerException e) {
}
try {
int plane = reader.getIndex(0, c, 0);
if (plane < retrieve.getPlaneCount(series)) {
seriesTable.put(EXPOSURE_TIME + c, retrieve.getPlaneExposureTime(series, plane).value().toString());
seriesTable.put(EXPOSURE_TIME_UNIT + c, retrieve.getPlaneExposureTime(series, plane).unit().getSymbol());
}
} catch (NullPointerException e) {
}
Length emWavelength = retrieve.getChannelEmissionWavelength(series, c);
if (emWavelength != null) {
seriesTable.put(EMISSION_WAVELENGTH + c, emWavelength.value().toString());
seriesTable.put(EMISSION_WAVELENGTH_UNIT + c, emWavelength.unit().getSymbol());
}
Length exWavelength = retrieve.getChannelExcitationWavelength(series, c);
if (exWavelength != null) {
seriesTable.put(EXCITATION_WAVELENGTH + c, exWavelength.value().toString());
seriesTable.put(EXCITATION_WAVELENGTH_UNIT + c, exWavelength.unit().getSymbol());
}
try {
seriesTable.put(DETECTOR + c, retrieve.getDetectorSettingsID(series, c));
} catch (NullPointerException e) {
}
}
for (int p = 0; p < reader.getImageCount(); p++) {
try {
Time deltaT = retrieve.getPlaneDeltaT(series, p);
if (deltaT != null) {
seriesTable.put(DELTA_T + p, deltaT.value(UNITS.SECOND).toString());
}
Length xPos = retrieve.getPlanePositionX(series, p);
if (xPos != null) {
seriesTable.put(X_POSITION + p, String.valueOf(xPos.value().doubleValue()));
seriesTable.put(X_POSITION_UNIT + p, xPos.unit().getSymbol());
}
Length yPos = retrieve.getPlanePositionY(series, p);
if (yPos != null) {
seriesTable.put(Y_POSITION + p, String.valueOf(yPos.value().doubleValue()));
seriesTable.put(Y_POSITION_UNIT + p, yPos.unit().getSymbol());
}
Length zPos = retrieve.getPlanePositionZ(series, p);
if (zPos != null) {
seriesTable.put(Z_POSITION + p, String.valueOf(zPos.value().doubleValue()));
seriesTable.put(Z_POSITION_UNIT + p, zPos.unit().getSymbol());
}
} catch (IndexOutOfBoundsException e) {
// only happens if no Plane elements were populated
}
}
ini.add(seriesTable);
}
}
Aggregations