|
| 1 | +''' metadatatools.py - mechanism to wrap some bioformats metadata classes |
| 2 | +
|
| 3 | +CellProfiler is distributed under the GNU General Public License, |
| 4 | +but this file is licensed under the more permissive BSD license. |
| 5 | +See the accompanying file LICENSE for details. |
| 6 | +
|
| 7 | +Copyright (c) 2003-2009 Massachusetts Institute of Technology |
| 8 | +Copyright (c) 2009-2013 Broad Institute |
| 9 | +All rights reserved. |
| 10 | +
|
| 11 | +Please see the AUTHORS file for credits. |
| 12 | +
|
| 13 | +Website: http://www.cellprofiler.org |
| 14 | +''' |
| 15 | + |
| 16 | +__version__ = "$Revision$" |
| 17 | + |
| 18 | +from javabridge import jutil |
| 19 | +import bioformats |
| 20 | + |
| 21 | +def createOMEXMLMetadata(): |
| 22 | + '''Creates an OME-XML metadata object using reflection, to avoid direct |
| 23 | + dependencies on the optional loci.formats.ome package. |
| 24 | + ''' |
| 25 | + return jutil.static_call('loci/formats/MetadataTools', 'createOMEXMLMetadata', '()Lloci/formats/meta/IMetadata;') |
| 26 | + |
| 27 | + |
| 28 | +class MetadataStore(object): |
| 29 | + ''' ''' |
| 30 | + def __init__(self, o): |
| 31 | + self.o = o |
| 32 | + |
| 33 | + createRoot = jutil.make_method('createRoot', '()V', '') |
| 34 | + def setPixelsBigEndian(self, bigEndian, imageIndex, binDataIndex): |
| 35 | + '''Set the endianness for a particular image |
| 36 | + |
| 37 | + bigEndian - True for big-endian, False for little-endian |
| 38 | + imageIndex - index of the image in question from IFormatReader.get_index? |
| 39 | + binDataIndex - ??? |
| 40 | + ''' |
| 41 | + # Post loci_tools 4.2 |
| 42 | + try: |
| 43 | + jutil.call(self.o, 'setPixelsBinDataBigEndian', |
| 44 | + '(Ljava/lang/Boolean;II)V', |
| 45 | + bigEndian, imageIndex, binDataIndex) |
| 46 | + except jutil.JavaException: |
| 47 | + jutil.call(self.o, 'setPixelsBigEndian', '(Ljava/lang/Boolean;II)V', |
| 48 | + bigEndian, imageIndex, binDataIndex) |
| 49 | + |
| 50 | + def setPixelsDimensionOrder(self, dimension_order, imageIndex, binDataIndex): |
| 51 | + '''Set the dimension order for a series''' |
| 52 | + # Post loci_tools 4.2 - use ome.xml.model.DimensionOrder |
| 53 | + try: |
| 54 | + jdimension_order = jutil.static_call( |
| 55 | + 'ome/xml/model/enums/DimensionOrder', 'fromString', |
| 56 | + '(Ljava/lang/String;)Lome/xml/model/enums/DimensionOrder;', |
| 57 | + dimension_order) |
| 58 | + jutil.call(self.o, 'setPixelsDimensionOrder', |
| 59 | + '(Lome/xml/model/enums/DimensionOrder;I)V', |
| 60 | + jdimension_order, imageIndex) |
| 61 | + except jutil.JavaException: |
| 62 | + jutil.call(self.o, 'setPixelsDimensionOrder', |
| 63 | + '(Ljava/lang/String;II)V', |
| 64 | + dimension_order, imageIndex, binDataIndex) |
| 65 | + |
| 66 | + setPixelsPixelType = jutil.make_method( |
| 67 | + 'setPixelsPixelType', '(Ljava/lang/String;II)V', |
| 68 | + '''Sets the pixel storage type |
| 69 | + pixel_type - text representation of the type, e.g. "uint8" |
| 70 | + imageIndex - ? |
| 71 | + binDataIndex - ? |
| 72 | + |
| 73 | + WARNING: only available in BioFormats < 4.2 |
| 74 | + ''') |
| 75 | + setPixelsType = jutil.make_method( |
| 76 | + 'setPixelsType', '(Lome/xml/model/enums/PixelType;I)V', |
| 77 | + '''Set the pixel storage type |
| 78 | + |
| 79 | + pixel_type - one of the enumerated values from PixelType. |
| 80 | + imageIndex - ? |
| 81 | + |
| 82 | + See the ome.xml.model.enums.PixelType and make_pixel_type_class's |
| 83 | + PixelType for possible values. |
| 84 | + ''') |
| 85 | + |
| 86 | + def setPixelsSizeX(self, x, imageIndex, binDataIndex): |
| 87 | + try: |
| 88 | + jutil.call(self.o, 'setPixelsSizeX', |
| 89 | + '(Lome/xml/model/primitives/PositiveInteger;I)V', |
| 90 | + PositiveInteger(x), imageIndex) |
| 91 | + except jutil.JavaException: |
| 92 | + jutil.call(self.o, 'setPixelsSizeX', |
| 93 | + '(Ljava/lang/Integer;II)V', x, imageIndex, binDataIndex) |
| 94 | + |
| 95 | + def setPixelsSizeY(self, y, imageIndex, binDataIndex): |
| 96 | + try: |
| 97 | + jutil.call(self.o, 'setPixelsSizeY', |
| 98 | + '(Lome/xml/model/primitives/PositiveInteger;I)V', |
| 99 | + PositiveInteger(y), imageIndex) |
| 100 | + except jutil.JavaException: |
| 101 | + jutil.call(self.o, 'setPixelsSizeY', |
| 102 | + '(Ljava/lang/Integer;II)V', y, imageIndex, binDataIndex) |
| 103 | + |
| 104 | + def setPixelsSizeZ(self, z, imageIndex, binDataIndex): |
| 105 | + try: |
| 106 | + jutil.call(self.o, 'setPixelsSizeZ', |
| 107 | + '(Lome/xml/model/primitives/PositiveInteger;I)V', |
| 108 | + PositiveInteger(z), imageIndex) |
| 109 | + except jutil.JavaException: |
| 110 | + jutil.call(self.o, 'setPixelsSizeZ', |
| 111 | + '(Ljava/lang/Integer;II)V', z, imageIndex, binDataIndex) |
| 112 | + |
| 113 | + def setPixelsSizeC(self, c, imageIndex, binDataIndex): |
| 114 | + try: |
| 115 | + jutil.call(self.o, 'setPixelsSizeC', |
| 116 | + '(Lome/xml/model/primitives/PositiveInteger;I)V', |
| 117 | + PositiveInteger(c), imageIndex) |
| 118 | + except jutil.JavaException: |
| 119 | + jutil.call(self.o, 'setPixelsSizeC', |
| 120 | + '(Ljava/lang/Integer;II)V', c, imageIndex, binDataIndex) |
| 121 | + |
| 122 | + def setPixelsSizeT(self, t, imageIndex, binDataIndex): |
| 123 | + try: |
| 124 | + jutil.call(self.o, 'setPixelsSizeT', |
| 125 | + '(Lome/xml/model/primitives/PositiveInteger;I)V', |
| 126 | + PositiveInteger(t), imageIndex) |
| 127 | + except jutil.JavaException: |
| 128 | + jutil.call(self.o, 'setPixelsSizeT', |
| 129 | + '(Ljava/lang/Integer;II)V', t, imageIndex, binDataIndex) |
| 130 | + |
| 131 | + def setLogicalChannelSamplesPerPixel(self, samplesPerPixel, imageIndex, channelIndex): |
| 132 | + 'For a particular LogicalChannel, sets number of channel components in the logical channel.' |
| 133 | + try: |
| 134 | + jutil.call(self.o, 'setChannelSamplesPerPixel', |
| 135 | + '(Lome/xml/model/primitives/PositiveInteger;II)V', |
| 136 | + PositiveInteger(samplesPerPixel), |
| 137 | + imageIndex, channelIndex) |
| 138 | + except jutil.JavaException: |
| 139 | + jutil.call(self.o, 'setLogicalChannelSamplesPerPixel', |
| 140 | + '(Ljava/lang/Integer;II)V', samplesPerPixel, |
| 141 | + imageIndex, channelIndex) |
| 142 | + setImageID = jutil.make_method( |
| 143 | + 'setImageID', '(Ljava/lang/String;I)V', |
| 144 | + '''Tag the indexed image with a name |
| 145 | + |
| 146 | + id - the name, for instance Image:0 |
| 147 | + imageIndex - the index of the image (series???) |
| 148 | + ''') |
| 149 | + setPixelsID = jutil.make_method( |
| 150 | + 'setPixelsID', '(Ljava/lang/String;I)V', |
| 151 | + '''Tag the pixels with a name (???) |
| 152 | + |
| 153 | + id - the name, for instance Pixels:0 |
| 154 | + imageIndex - the index of the image (???) |
| 155 | + ''') |
| 156 | + setChannelID = jutil.make_method( |
| 157 | + 'setChannelID', '(Ljava/lang/String;II)V', |
| 158 | + '''Give an ID name to the given channel |
| 159 | + |
| 160 | + id - the name of the channel |
| 161 | + imageIndex - (???) |
| 162 | + channelIndex - index of the channel to be ID'ed''') |
| 163 | + |
| 164 | +class MetadataRetrieve(object): |
| 165 | + ''' ''' |
| 166 | + def __init__(self, o): |
| 167 | + self.o = o |
| 168 | + |
| 169 | + getPixelsBigEndian = jutil.make_method('getPixelsBigEndian', '(II)Ljava/lang/Boolean;', |
| 170 | + 'For a particular Pixels, gets endianness of the pixels set.') |
| 171 | + getPixelsDimensionOrder = jutil.make_method('getPixelsDimensionOrder', '(II)Ljava/lang/String;', |
| 172 | + 'For a particular Pixels, gets the dimension order of the pixels set.') |
| 173 | + getPixelsPixelType = jutil.make_method('getPixelsPixelType', '(II)Ljava/lang/String;', |
| 174 | + 'For a particular Pixels, gets the pixel type.') |
| 175 | + getPixelsSizeX = jutil.make_method('getPixelsSizeX', '(II)Ljava/lang/Integer;', |
| 176 | + 'For a particular Pixels, gets The size of an individual plane or section\'s X axis (width).') |
| 177 | + getPixelsSizeY = jutil.make_method('getPixelsSizeY', '(II)Ljava/lang/Integer;', |
| 178 | + 'For a particular Pixels, gets The size of an individual plane or section\'s Y axis (height).') |
| 179 | + getPixelsSizeZ = jutil.make_method('getPixelsSizeZ', '(II)Ljava/lang/Integer;', |
| 180 | + 'For a particular Pixels, gets number of optical sections per stack.') |
| 181 | + getPixelsSizeC = jutil.make_method('getPixelsSizeC', '(II)Ljava/lang/Integer;', |
| 182 | + 'For a particular Pixels, gets number of channels per timepoint.') |
| 183 | + getPixelsSizeT = jutil.make_method('getPixelsSizeT', '(II)Ljava/lang/Integer;', |
| 184 | + 'For a particular Pixels, gets number of timepoints.') |
| 185 | + getLogicalChannelSamplesPerPixel = jutil.make_method('getLogicalChannelSamplesPerPixel', '(II)Ljava/lang/Integer;', |
| 186 | + 'For a particular LogicalChannel, gets number of channel components in the logical channel.') |
| 187 | + getChannelName = jutil.make_method('getChannelName', |
| 188 | + '(II)Ljava/lang/String;', |
| 189 | + '''Get the name for a particular channel. |
| 190 | + |
| 191 | + imageIndex - image # to query (use C = 0) |
| 192 | + channelIndex - channel # to query''') |
| 193 | + getChannelID = jutil.make_method('getChannelID', |
| 194 | + '(II)Ljava/lang/String;', |
| 195 | + '''Get the OME channel ID for a particular channel. |
| 196 | + |
| 197 | + imageIndex - image # to query (use C = 0) |
| 198 | + channelIndex - channel # to query''') |
| 199 | + |
| 200 | + |
| 201 | +def wrap_imetadata_object(o): |
| 202 | + ''' Returns a python object wrapping the functionality of the given |
| 203 | + IMetaData object (as returned by createOMEXMLMetadata) ''' |
| 204 | + class IMetadata(MetadataStore, MetadataRetrieve): |
| 205 | + ''' ''' |
| 206 | + def __init__(self, o): |
| 207 | + MetadataStore.__init__(self, o) |
| 208 | + MetadataRetrieve.__init__(self, o) |
| 209 | + self.o = o |
| 210 | + |
| 211 | + return IMetadata(o) |
| 212 | + |
| 213 | +__pixel_type_class = None |
| 214 | +def make_pixel_type_class(): |
| 215 | + '''The class, ome.xml.model.enums.PixelType |
| 216 | + |
| 217 | + The Java class has enumerations for the various image data types |
| 218 | + such as UINT8 or DOUBLE |
| 219 | + ''' |
| 220 | + global __pixel_type_class |
| 221 | + if __pixel_type_class is None: |
| 222 | + class PixelType(object): |
| 223 | + '''Provide enums from ome.xml.model.enums.PixelType''' |
| 224 | + klass = jutil.get_env().find_class('ome/xml/model/enums/PixelType') |
| 225 | + INT8 = jutil.get_static_field(klass, 'INT8', 'Lome/xml/model/enums/PixelType;') |
| 226 | + INT16 = jutil.get_static_field(klass, 'INT16', 'Lome/xml/model/enums/PixelType;') |
| 227 | + INT32 = jutil.get_static_field(klass, 'INT32', 'Lome/xml/model/enums/PixelType;') |
| 228 | + UINT8 = jutil.get_static_field(klass, 'UINT8', 'Lome/xml/model/enums/PixelType;') |
| 229 | + UINT16 = jutil.get_static_field(klass, 'UINT16', 'Lome/xml/model/enums/PixelType;') |
| 230 | + UINT32 = jutil.get_static_field(klass, 'UINT32', 'Lome/xml/model/enums/PixelType;') |
| 231 | + FLOAT = jutil.get_static_field(klass, 'FLOAT', 'Lome/xml/model/enums/PixelType;') |
| 232 | + BIT = jutil.get_static_field(klass, 'BIT', 'Lome/xml/model/enums/PixelType;') |
| 233 | + DOUBLE = jutil.get_static_field(klass, 'DOUBLE', 'Lome/xml/model/enums/PixelType;') |
| 234 | + COMPLEX = jutil.get_static_field(klass, 'COMPLEX', 'Lome/xml/model/enums/PixelType;') |
| 235 | + DOUBLECOMPLEX = jutil.get_static_field(klass, 'DOUBLECOMPLEX', 'Lome/xml/model/enums/PixelType;') |
| 236 | + __pixel_type_class = PixelType() |
| 237 | + return __pixel_type_class |
| 238 | + |
| 239 | +MINIMUM = 'MINIMUM' |
| 240 | +NO_OVERLAYS = 'NO_OVERLAYS' |
| 241 | +ALL = 'ALL' |
| 242 | + |
| 243 | +def get_metadata_options(level): |
| 244 | + '''Get an instance of the MetadataOptions interface |
| 245 | + |
| 246 | + level - MINIMUM, NO_OVERLAYS or ALL to set the metadata retrieval level |
| 247 | + |
| 248 | + The object returned can be used in setMetadataOptions in a format reader. |
| 249 | + ''' |
| 250 | + jlevel = jutil.get_static_field('loci/formats/in/MetadataLevel', level, |
| 251 | + 'Lloci/formats/in/MetadataLevel;') |
| 252 | + return jutil.make_instance('loci/formats/in/DefaultMetadataOptions', |
| 253 | + '(Lloci/formats/in/MetadataLevel;)V', |
| 254 | + jlevel) |
| 255 | + |
| 256 | + |
| 257 | +def PositiveInteger(some_number): |
| 258 | + '''Return an instance of ome.xml.model.primitives.PositiveInteger |
| 259 | + |
| 260 | + some_number - the number to be wrapped up in the class |
| 261 | + ''' |
| 262 | + return jutil.make_instance('ome/xml/model/primitives/PositiveInteger', |
| 263 | + '(Ljava/lang/Integer;)V', some_number) |
| 264 | + |
0 commit comments