Search in sources :

Example 1 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class ImportProcess method getOMEXML.

/**
 * Valid only after {@link ImportStep#READER}.
 */
public String getOMEXML() {
    if (omeXML == null) {
        // NB: Extract the OME-XML once, then keep it cached.
        try {
            ServiceFactory factory = new ServiceFactory();
            OMEXMLService service = factory.getInstance(OMEXMLService.class);
            omeXML = service.getOMEXML(getOMEMetadata());
        } catch (DependencyException de) {
        } catch (ServiceException se) {
        }
    }
    return omeXML;
}
Also used : ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService)

Example 2 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class MINCReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        netcdf = factory.getInstance(NetCDFService.class);
        netcdf.setFile(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(e);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Vector<String> variableList = netcdf.getVariableList();
        for (String variable : variableList) {
            Hashtable<String, Object> attributes = netcdf.getVariableAttributes(variable);
            String[] keys = attributes.keySet().toArray(new String[0]);
            Arrays.sort(keys);
            for (String key : keys) {
                if (attributes.get(key) instanceof Object[]) {
                    final StringBuilder sb = new StringBuilder();
                    Object[] o = (Object[]) attributes.get(key);
                    for (Object q : o) {
                        sb.append(q.toString());
                    }
                    addGlobalMeta(variable + " " + key, sb.toString());
                } else {
                    addGlobalMeta(variable + " " + key, attributes.get(key));
                }
            }
        }
    }
    CoreMetadata m = core.get(0);
    try {
        Object pixels = netcdf.getVariableValue("/image");
        if (pixels == null) {
            pixels = netcdf.getVariableValue("/minc-2.0/image/0/image");
            isMINC2 = true;
        }
        m.littleEndian = isMINC2;
        boolean signed = false;
        if (isMINC2) {
            Hashtable<String, Object> attrs = netcdf.getVariableAttributes("/minc-2.0/image/0/image");
            String unsigned = attrs.get("_Unsigned").toString();
            if (!unsigned.startsWith("true")) {
                signed = true;
            }
        } else {
            Hashtable<String, Object> attrs = netcdf.getVariableAttributes("/image");
            String signtype = attrs.get("signtype").toString();
            if (signtype.startsWith("signed")) {
                signed = true;
            }
        }
        if (pixels instanceof byte[][][]) {
            m.pixelType = signed ? FormatTools.INT8 : FormatTools.UINT8;
            pixelData = (byte[][][]) pixels;
        } else if (pixels instanceof byte[][][][]) {
            byte[][][][] actualPixels = (byte[][][][]) pixels;
            m.pixelType = signed ? FormatTools.INT8 : FormatTools.UINT8;
            pixelData = new byte[actualPixels.length * actualPixels[0].length][][];
            int nextPlane = 0;
            for (int t = 0; t < actualPixels.length; t++) {
                for (int z = 0; z < actualPixels[t].length; z++) {
                    pixelData[nextPlane++] = actualPixels[t][z];
                }
            }
        } else if (pixels instanceof short[][][]) {
            m.pixelType = signed ? FormatTools.INT16 : FormatTools.UINT16;
            short[][][] s = (short[][][]) pixels;
            pixelData = new byte[s.length][][];
            for (int i = 0; i < s.length; i++) {
                pixelData[i] = new byte[s[i].length][];
                for (int j = 0; j < s[i].length; j++) {
                    pixelData[i][j] = DataTools.shortsToBytes(s[i][j], isLittleEndian());
                }
            }
        } else if (pixels instanceof int[][][]) {
            m.pixelType = signed ? FormatTools.INT32 : FormatTools.UINT32;
            int[][][] s = (int[][][]) pixels;
            pixelData = new byte[s.length][][];
            for (int i = 0; i < s.length; i++) {
                pixelData[i] = new byte[s[i].length][];
                for (int j = 0; j < s[i].length; j++) {
                    pixelData[i][j] = DataTools.intsToBytes(s[i][j], isLittleEndian());
                }
            }
        } else if (pixels instanceof float[][][]) {
            m.pixelType = FormatTools.FLOAT;
            float[][][] s = (float[][][]) pixels;
            pixelData = new byte[s.length][][];
            for (int i = 0; i < s.length; i++) {
                pixelData[i] = new byte[s[i].length][];
                for (int j = 0; j < s[i].length; j++) {
                    pixelData[i][j] = DataTools.floatsToBytes(s[i][j], isLittleEndian());
                }
            }
        } else if (pixels instanceof double[][][]) {
            m.pixelType = FormatTools.DOUBLE;
            double[][][] s = (double[][][]) pixels;
            pixelData = new byte[s.length][][];
            for (int i = 0; i < s.length; i++) {
                pixelData[i] = new byte[s[i].length][];
                for (int j = 0; j < s[i].length; j++) {
                    pixelData[i][j] = DataTools.doublesToBytes(s[i][j], isLittleEndian());
                }
            }
        }
    } catch (ServiceException e) {
        throw new FormatException(e);
    }
    Length physicalX = null;
    Length physicalY = null;
    Length physicalZ = null;
    Length xPosition = null;
    Length yPosition = null;
    Length zPosition = null;
    if (isMINC2) {
        Hashtable<String, Object> attrs = netcdf.getVariableAttributes("/minc-2.0/dimensions/xspace");
        m.sizeX = Integer.parseInt(attrs.get("length").toString());
        physicalX = getStepSize(attrs);
        xPosition = getStart(attrs);
        attrs = netcdf.getVariableAttributes("/minc-2.0/dimensions/yspace");
        m.sizeY = Integer.parseInt(attrs.get("length").toString());
        physicalY = getStepSize(attrs);
        yPosition = getStart(attrs);
        attrs = netcdf.getVariableAttributes("/minc-2.0/dimensions/zspace");
        m.sizeZ = Integer.parseInt(attrs.get("length").toString());
        physicalZ = getStepSize(attrs);
        zPosition = getStart(attrs);
    } else {
        m.sizeX = netcdf.getDimension("/xspace");
        m.sizeY = netcdf.getDimension("/yspace");
        m.sizeZ = netcdf.getDimension("/zspace");
        Hashtable<String, Object> attrs = netcdf.getVariableAttributes("/xspace");
        physicalX = getStepSize(attrs);
        xPosition = getStart(attrs);
        attrs = netcdf.getVariableAttributes("/yspace");
        physicalY = getStepSize(attrs);
        yPosition = getStart(attrs);
        attrs = netcdf.getVariableAttributes("/zspace");
        physicalZ = getStepSize(attrs);
        zPosition = getStart(attrs);
    }
    try {
        m.sizeT = netcdf.getDimension("/time");
    } catch (NullPointerException e) {
        m.sizeT = 1;
    }
    m.sizeC = 1;
    m.imageCount = getSizeZ() * getSizeT() * getSizeC();
    m.rgb = false;
    m.indexed = false;
    m.dimensionOrder = "XYZCT";
    String history = null;
    if (isMINC2) {
        history = netcdf.getAttributeValue("/minc-2.0/ident");
    } else {
        history = netcdf.getAttributeValue("/history");
    }
    addGlobalMeta("Comment", history);
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, xPosition != null || yPosition != null || zPosition != null);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        store.setImageDescription(history, 0);
        if (physicalX != null) {
            store.setPixelsPhysicalSizeX(physicalX, 0);
        }
        if (physicalY != null) {
            store.setPixelsPhysicalSizeY(physicalY, 0);
        }
        if (physicalZ != null) {
            store.setPixelsPhysicalSizeZ(physicalZ, 0);
        }
        for (int i = 0; i < getImageCount(); i++) {
            if (xPosition != null) {
                store.setPlanePositionX(xPosition, 0, i);
            }
            if (yPosition != null) {
                store.setPlanePositionY(yPosition, 0, i);
            }
            if (zPosition != null) {
                int z = getZCTCoords(i)[0];
                Double pos = zPosition.value().doubleValue();
                if (physicalZ != null && z > 0) {
                    pos += z * physicalZ.value().doubleValue();
                }
                store.setPlanePositionZ(FormatTools.createLength(pos, zPosition.unit()), 0, i);
            }
        }
    }
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) ServiceException(loci.common.services.ServiceException) Length(ome.units.quantity.Length) MissingLibraryException(loci.formats.MissingLibraryException) NetCDFService(loci.formats.services.NetCDFService)

Example 3 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class PCIReader method initPOIService.

// -- Helper methods --
private void initPOIService() throws FormatException, IOException {
    try {
        ServiceFactory factory = new ServiceFactory();
        poi = factory.getInstance(POIService.class);
    } catch (DependencyException de) {
        throw new FormatException("POI library not found", de);
    }
    poi.initialize(Location.getMappedId(getCurrentFile()));
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) POIService(loci.formats.services.POIService) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException)

Example 4 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class ImarisHDFReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        netcdf = factory.getInstance(NetCDFService.class);
        netcdf.setFile(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(NetCDFServiceImpl.NO_NETCDF_MSG, e);
    }
    pixelSizeX = pixelSizeY = pixelSizeZ = 1;
    emWave = new ArrayList<String>();
    exWave = new ArrayList<String>();
    channelMin = new ArrayList<String>();
    channelMax = new ArrayList<String>();
    gain = new ArrayList<String>();
    pinhole = new ArrayList<String>();
    channelName = new ArrayList<String>();
    microscopyMode = new ArrayList<String>();
    colors = new ArrayList<double[]>();
    seriesCount = 0;
    // read all of the metadata key/value pairs
    parseAttributes();
    CoreMetadata ms0 = core.get(0);
    if (seriesCount > 1) {
        for (int i = 1; i < seriesCount; i++) {
            core.add(new CoreMetadata());
        }
        for (int i = 1; i < seriesCount; i++) {
            CoreMetadata ms = core.get(i);
            String groupPath = "DataSet/ResolutionLevel_" + i + "/TimePoint_0/Channel_0";
            ms.sizeX = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeX"));
            ms.sizeY = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeY"));
            ms.sizeZ = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeZ"));
            ms.imageCount = ms.sizeZ * getSizeC() * getSizeT();
            ms.sizeC = getSizeC();
            ms.sizeT = getSizeT();
            ms.thumbnail = true;
            if (ms.sizeZ == ms0.sizeZ && ms.sizeC == ms0.sizeC && ms.sizeT == ms0.sizeT) {
                // do not assume that all series will have the same dimensions
                // if the Z, C or T size is different, then it cannot
                // be a subresolution
                ms0.resolutionCount++;
            }
        }
    }
    ms0.imageCount = getSizeZ() * getSizeC() * getSizeT();
    ms0.thumbnail = false;
    ms0.dimensionOrder = "XYZCT";
    // determine pixel type - this isn't stored in the metadata, so we need
    // to check the pixels themselves
    int type = -1;
    Object pix = getImageData(0, 0, 0, 1, 1);
    if (pix instanceof byte[][])
        type = FormatTools.UINT8;
    else if (pix instanceof short[][])
        type = FormatTools.UINT16;
    else if (pix instanceof int[][])
        type = FormatTools.UINT32;
    else if (pix instanceof float[][])
        type = FormatTools.FLOAT;
    else if (pix instanceof double[][])
        type = FormatTools.DOUBLE;
    else {
        throw new FormatException("Unknown pixel type: " + pix);
    }
    for (int i = 0; i < core.size(); i++) {
        CoreMetadata ms = core.get(i);
        ms.pixelType = type;
        ms.dimensionOrder = "XYZCT";
        ms.rgb = false;
        ms.thumbSizeX = 128;
        ms.thumbSizeY = 128;
        ms.orderCertain = true;
        ms.littleEndian = true;
        ms.interleaved = false;
        ms.indexed = colors.size() >= getSizeC();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String imageName = new Location(getCurrentFile()).getName();
    for (int s = 0; s < getSeriesCount(); s++) {
        store.setImageName(imageName + " Resolution Level " + (s + 1), s);
    }
    if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM) {
        return;
    }
    int cIndex = 0;
    for (int s = 0; s < getSeriesCount(); s++) {
        setSeries(s);
        double px = pixelSizeX, py = pixelSizeY, pz = pixelSizeZ;
        if (px == 1)
            px = (maxX - minX) / getSizeX();
        if (py == 1)
            py = (maxY - minY) / getSizeY();
        if (pz == 1)
            pz = (maxZ - minZ) / getSizeZ();
        Length sizeX = FormatTools.getPhysicalSizeX(px);
        Length sizeY = FormatTools.getPhysicalSizeY(py);
        Length sizeZ = FormatTools.getPhysicalSizeZ(pz);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, s);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, s);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, s);
        }
        for (int i = 0; i < getSizeC(); i++, cIndex++) {
            Float gainValue = null;
            Integer pinholeValue = null, emWaveValue = null, exWaveValue;
            if (cIndex < gain.size()) {
                try {
                    gainValue = new Float(gain.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < pinhole.size()) {
                try {
                    pinholeValue = new Integer(pinhole.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < emWave.size()) {
                try {
                    emWaveValue = new Integer(emWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < exWave.size()) {
                try {
                    exWaveValue = new Integer(exWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            Double minValue = null, maxValue = null;
            if (cIndex < channelMin.size()) {
                try {
                    minValue = new Double(channelMin.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < channelMax.size()) {
                try {
                    maxValue = new Double(channelMax.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (i < colors.size()) {
                double[] color = colors.get(i);
                Color realColor = new Color((int) (color[0] * 255), (int) (color[1] * 255), (int) (color[2] * 255), 255);
                store.setChannelColor(realColor, s, i);
            }
        }
    }
    setSeries(0);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) Color(ome.xml.model.primitives.Color) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) MissingLibraryException(loci.formats.MissingLibraryException) NetCDFService(loci.formats.services.NetCDFService) Location(loci.common.Location)

Example 5 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class Memoizer method getService.

// Copied from OMETiffReader.
protected OMEXMLService getService() throws MissingLibraryException {
    if (service == null) {
        try {
            ServiceFactory factory = new ServiceFactory();
            service = factory.getInstance(OMEXMLService.class);
        } catch (DependencyException de) {
            throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
        }
    }
    return service;
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

DependencyException (loci.common.services.DependencyException)51 ServiceFactory (loci.common.services.ServiceFactory)49 FormatException (loci.formats.FormatException)39 ServiceException (loci.common.services.ServiceException)32 OMEXMLService (loci.formats.services.OMEXMLService)32 MissingLibraryException (loci.formats.MissingLibraryException)15 MetadataStore (loci.formats.meta.MetadataStore)14 CoreMetadata (loci.formats.CoreMetadata)13 IMetadata (loci.formats.meta.IMetadata)13 IOException (java.io.IOException)12 Location (loci.common.Location)10 ImageReader (loci.formats.ImageReader)10 Length (ome.units.quantity.Length)10 PositiveInteger (ome.xml.model.primitives.PositiveInteger)9 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)8 ArrayList (java.util.ArrayList)7 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)6 EnumerationException (ome.xml.model.enums.EnumerationException)6 File (java.io.File)5 IFormatWriter (loci.formats.IFormatWriter)5