Skip to content

Instantly share code, notes, and snippets.

@chris-allan
Last active August 29, 2015 14:08
Show Gist options
  • Save chris-allan/830a8001a07cf757d4c8 to your computer and use it in GitHub Desktop.
Save chris-allan/830a8001a07cf757d4c8 to your computer and use it in GitHub Desktop.
Jython version of some showinf functionality
#!/usr/bin/env jython
# encoding: utf-8
"""
Dumps a basic set of metadata for an image file using Bio-Formats.
Copyright (C) 2014 Glencoe Software, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys
from getopt import getopt, GetoptError
from loci.formats.services import OMEXMLService
from loci.common.services import ServiceFactory
from loci.formats import ImageReader, FormatTools
from loci.common import DebugTools
def usage(error=None):
"""
Prints usage so that we don't have to. :)
"""
cmd = sys.argv[0]
if error:
print error
print """Usage: %(cmd)s <filename>
Dumps a basic set of metadata for an image file using Bio-Formats.
Options:
-d Enable debugging
-h Display this information
Examples:
\t%(cmd)s /path/to/my/image.img
Report bugs to [email protected]""" % {'cmd': cmd}
sys.exit(2)
def unwrap(v):
# Various metadata data types are not primitives and are not autoboxed.
# As such these need to be conveniently unwrapped for display. See:
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/ome/xml/model/primitives/PositiveFloat.html
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/ome/xml/model/primitives/PositiveInteger.html
if v is None:
return None
return v.getValue()
def print_dimensions(image_reader):
series_count = image_reader.getSeriesCount()
print 'Series count: %d' % series_count
for series in range(series_count):
image_reader.setSeries(series)
print 'Series #%d' % (series + 1)
print '-' * 12
print 'SizeX: %d' % image_reader.getSizeX()
print 'SizeY: %d' % image_reader.getSizeY()
print 'SizeZ: %d' % image_reader.getSizeZ()
print 'SizeC: %d' % image_reader.getSizeC()
print 'SizeT: %d' % image_reader.getSizeT()
pixel_type = image_reader.getPixelType()
bpp = FormatTools.getBytesPerPixel(pixel_type)
is_float = FormatTools.isFloatingPoint(pixel_type)
print 'PixelType: %s (%d byte(s); floating point? %s)' % (
FormatTools.getPixelTypeString(pixel_type), bpp, is_float
)
print 'Is little endian? %s' % image_reader.isLittleEndian()
print '=' * 12
print_basic_metadata(image_reader, series)
def print_basic_metadata(image_reader, series):
# Image indexes are analoguous to series indexes
image_index = series
# MetadataRetrieve provides method driven access to the hierarchical OME
# data model. See:
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/loci/formats/meta/MetadataRetrieve.html
metadata_retrieve = image_reader.getMetadataStore()
print 'Image name: %s' % metadata_retrieve.getImageName(image_index)
print 'Physical SizeX: %s' % \
unwrap(metadata_retrieve.getPixelsPhysicalSizeX(image_index))
print 'Physical SizeY: %s' % \
unwrap(metadata_retrieve.getPixelsPhysicalSizeY(image_index))
print 'Physical SizeZ: %s' % \
unwrap(metadata_retrieve.getPixelsPhysicalSizeZ(image_index))
# Channel metadata
channel_count = metadata_retrieve.getChannelCount(image_index)
print 'Channel count: %d' % channel_count
for channel in range(channel_count):
channel_name = metadata_retrieve.getChannelName(image_index, channel)
print 'Channel %d name: %s' % (channel + 1, channel_name)
print
def showinf(filename):
# ImageReader is the core interface for working with data and metadata.
# See:
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/loci/formats/ImageReader.html
image_reader = ImageReader()
# OMEXMLMetadata is the core metadata store for OME-XML metadata. We
# will need to create and use one to handle metadata in a structured
# fashion. See:
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/loci/formats/meta/IMetadata.html
# * http://downloads.openmicroscopy.org/bio-formats/5.0.5/api/loci/formats/ome/OMEXMLMetadata.html
service_factory = ServiceFactory()
ome_xml_service = service_factory.getInstance(OMEXMLService)
image_reader.setMetadataStore(ome_xml_service.createOMEXMLMetadata())
# Initialize the reader and print metadata
image_reader.setId(filename)
try:
print_dimensions(image_reader)
finally:
image_reader.close()
if __name__ == '__main__':
try:
options, args = getopt(sys.argv[1:], "hd")
except GetoptError, (msg, opt):
usage(msg)
level = DebugTools.enableLogging('INFO')
for option, argument in options:
if option == "-h":
usage()
if option == "-d":
level = DebugTools.enableLogging('DEBUG')
filename, = args
showinf(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment