Search in sources :

Example 61 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.

the class FormatReaderTest method testSaneOMEXML.

@Test(groups = { "all", "xml", "automated" })
public void testSaneOMEXML() {
    String testName = "testSaneOMEXML";
    if (!initFile())
        result(testName, false, "initFile");
    if (!config.hasValidXML()) {
        LOGGER.debug("Skipping valid XML test");
        result(testName, true);
        return;
    }
    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++) {
            // total number of ChannelComponents should match SizeC
            int sizeC = retrieve.getPixelsSizeC(i).getValue().intValue();
            int nChannelComponents = retrieve.getChannelCount(i);
            int samplesPerPixel = retrieve.getChannelSamplesPerPixel(i, 0).getValue();
            if (sizeC != nChannelComponents * samplesPerPixel) {
                msg = "ChannelComponent";
            }
            // Z, C and T indices should be populated if PlaneTiming is present
            Time deltaT = null;
            Time exposure = null;
            Integer z = null, c = null, t = null;
            if (retrieve.getPlaneCount(i) > 0) {
                deltaT = retrieve.getPlaneDeltaT(i, 0);
                exposure = retrieve.getPlaneExposureTime(i, 0);
                z = retrieve.getPlaneTheZ(i, 0).getValue();
                c = retrieve.getPlaneTheC(i, 0).getValue();
                t = retrieve.getPlaneTheT(i, 0).getValue();
            }
            if ((deltaT != null || exposure != null) && (z == null || c == null || t == null)) {
                msg = "PlaneTiming";
            }
            // if CreationDate is before 1990, it's probably invalid
            String date = null;
            if (retrieve.getImageAcquisitionDate(i) != null) {
                date = retrieve.getImageAcquisitionDate(i).getValue();
            }
            config.setSeries(i);
            String configDate = config.getDate();
            if (date != null && !date.equals(configDate)) {
                date = date.trim();
                long acquiredDate = new Timestamp(date).asInstant().getMillis();
                long saneDate = new Timestamp("1990-01-01T00:00:00").asInstant().getMillis();
                long fileDate = new Location(reader.getCurrentFile()).getAbsoluteFile().lastModified();
                if (acquiredDate < saneDate && fileDate >= saneDate) {
                    msg = "CreationDate (date=" + date + " acquiredDate=" + acquiredDate + " fileDate=" + fileDate + " saneDate=" + saneDate + ")";
                }
            }
        }
    } catch (Throwable t) {
        LOGGER.info("", t);
        msg = t.getMessage();
    }
    result(testName, msg == null, msg);
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) Time(ome.units.quantity.Time) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location) Test(org.testng.annotations.Test)

Example 62 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve 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);
}
Also used : IFormatReader(loci.formats.IFormatReader) FileStitcher(loci.formats.FileStitcher) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) ReaderWrapper(loci.formats.ReaderWrapper) Test(org.testng.annotations.Test)

Example 63 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.

the class FormatReaderTest method testValidXML.

@Test(groups = { "all", "xml", "fast", "automated" })
public void testValidXML() {
    if (config == null)
        throw new SkipException("No config tree");
    String testName = "testValidXML";
    if (!initFile())
        result(testName, false, "initFile");
    if (!config.hasValidXML()) {
        LOGGER.debug("Skipping valid XML test");
        result(testName, true);
        return;
    }
    String format = config.getReader();
    boolean success = true;
    try {
        MetadataStore store = reader.getMetadataStore();
        MetadataRetrieve retrieve = omexmlService.asRetrieve(store);
        String xml = omexmlService.getOMEXML(retrieve);
        // javax.xml.validation.Validator as used during XML validation
        synchronized (configTree) {
            success = xml != null && omexmlService.validateOMEXML(xml);
        }
    } catch (Throwable t) {
        LOGGER.info("", t);
        success = false;
    }
    result(testName, success);
    try {
        close();
    } catch (IOException e) {
        LOGGER.info("", e);
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) SkipException(org.testng.SkipException) IOException(java.io.IOException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) Test(org.testng.annotations.Test)

Example 64 with MetadataRetrieve

use of loci.formats.meta.MetadataRetrieve in project bioformats by openmicroscopy.

the class FormatReaderTest method writeXML.

@Test(groups = { "config-xml" })
public void writeXML() {
    setupReader();
    if (!initFile(false))
        return;
    String file = reader.getCurrentFile();
    LOGGER.info("Generating XML: {}", file);
    try {
        Location l = new Location(file);
        File f = new File(l.getParent(), l.getName() + ".ome.xml");
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(f), Constants.ENCODING);
        MetadataStore store = reader.getMetadataStore();
        MetadataRetrieve retrieve = omexmlService.asRetrieve(store);
        String xml = omexmlService.getOMEXML(retrieve);
        writer.write(xml);
        writer.close();
        reader.close();
    } catch (Throwable t) {
        LOGGER.info("", t);
        assert false;
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) Location(loci.common.Location) Test(org.testng.annotations.Test)

Aggregations

MetadataRetrieve (loci.formats.meta.MetadataRetrieve)64 FormatException (loci.formats.FormatException)11 MetadataStore (loci.formats.meta.MetadataStore)11 Length (ome.units.quantity.Length)10 ServiceFactory (loci.common.services.ServiceFactory)8 DependencyException (loci.common.services.DependencyException)7 OMEXMLService (loci.formats.services.OMEXMLService)6 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 Time (ome.units.quantity.Time)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5 ServiceException (loci.common.services.ServiceException)4 MissingLibraryException (loci.formats.MissingLibraryException)4 Test (org.testng.annotations.Test)4 IndexColorModel (java.awt.image.IndexColorModel)3 Location (loci.common.Location)3 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)3 ImageReader (loci.formats.ImageReader)3 BufferedImage (java.awt.image.BufferedImage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2