Skip to content

Commit e673169

Browse files
committed
Extracted bioformats code from CellProfiler
(http:://github.com/CellProfiler/CellProfiler) rev. 5fc781e8d2a1b8abe508e56c60ec50295cd12aef and started documentation
0 parents  commit e673169

20 files changed

Lines changed: 2172 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.pyc
2+
/build
3+
/docs/_build
4+
/python_bioformats.egg-info

bioformats/__init__.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''Bioformats package - wrapper for loci.bioformats java code
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+
__version__ = "$Revision$"
16+
17+
import os.path
18+
import javabridge
19+
import formatreader as _formatreader
20+
import formatwriter as _formatwriter
21+
22+
_jars_dir = os.path.join(os.path.dirname(__file__), 'jars')
23+
JARS = javabridge.JARS + [os.path.realpath(os.path.join(_jars_dir, name + '.jar'))
24+
for name in ['loci_tools']]
25+
26+
# See http://www.loci.wisc.edu/software/bio-formats
27+
READABLE_FORMATS = ('al3d', 'am', 'amiramesh', 'apl', 'arf', 'avi', 'bmp',
28+
'c01', 'cfg', 'cxd', 'dat', 'dcm', 'dicom', 'dm3', 'dv',
29+
'eps', 'epsi', 'fits', 'flex', 'fli', 'gel', 'gif', 'grey',
30+
'hdr', 'html', 'hx', 'ics', 'ids', 'img', 'ims', 'ipl',
31+
'ipm', 'ipw', 'jp2', 'jpeg', 'jpg', 'l2d', 'labels', 'lei',
32+
'lif', 'liff', 'lim', 'lsm', 'mdb', 'mnc', 'mng', 'mov',
33+
'mrc', 'mrw', 'mtb', 'naf', 'nd', 'nd2', 'nef', 'nhdr',
34+
'nrrd', 'obsep', 'oib', 'oif', 'ome', 'ome.tiff', 'pcx',
35+
'pgm', 'pic', 'pict', 'png', 'ps', 'psd', 'r3d', 'raw',
36+
'scn', 'sdt', 'seq', 'sld', 'stk', 'svs', 'tif', 'tiff',
37+
'tnb', 'txt', 'vws', 'xdce', 'xml', 'xv', 'xys', 'zvi')
38+
WRITABLE_FORMATS = ('avi', 'eps', 'epsi', 'ics', 'ids', 'jp2', 'jpeg', 'jpg',
39+
'mov', 'ome', 'ome.tiff', 'png', 'ps', 'tif', 'tiff')
40+
41+
ImageReader = _formatreader.make_image_reader_class()
42+
FormatTools = _formatreader.make_format_tools_class()
43+
OMETiffWriter = _formatwriter.make_ome_tiff_writer_class()
44+
ChannelSeparator = _formatreader.make_reader_wrapper_class(
45+
"loci/formats/ChannelSeparator")
46+
47+
from .metadatatools import createOMEXMLMetadata as create_ome_xml_metadata
48+
from .metadatatools import wrap_imetadata_object
49+
import metadatatools as _metadatatools
50+
PixelType = _metadatatools.make_pixel_type_class()
51+
52+
load_image = _formatreader.load_using_bioformats
53+
54+
def init_logger():
55+
javabridge.static_call("org/apache/log4j/BasicConfigurator",
56+
"configure", "()V")
57+
log4j_logger = javabridge.static_call("org/apache/log4j/Logger",
58+
"getRootLogger",
59+
"()Lorg/apache/log4j/Logger;")
60+
warn_level = javabridge.get_static_field("org/apache/log4j/Level", "WARN",
61+
"Lorg/apache/log4j/Level;")
62+
javabridge.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V",
63+
warn_level)
64+
65+
66+
if __name__ == "__main__":
67+
# Handy-dandy PyShell for exploring BioFormats / Rhino / ImageJ
68+
import wx.py.PyCrust
69+
70+
wx.py.PyCrust.main()
71+
J.kill_vm()

bioformats/formatreader.py

Lines changed: 469 additions & 0 deletions
Large diffs are not rendered by default.

bioformats/formatwriter.py

Lines changed: 416 additions & 0 deletions
Large diffs are not rendered by default.

bioformats/jars/loci_tools.jar

9.24 MB
Binary file not shown.

bioformats/log4j.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
import javabridge
3+
4+
def basic_config():
5+
javabridge.static_call("org/apache/log4j/BasicConfigurator",
6+
"configure", "()V")
7+
log4j_logger = javabridge.static_call("org/apache/log4j/Logger",
8+
"getRootLogger",
9+
"()Lorg/apache/log4j/Logger;")
10+
warn_level = javabridge.get_static_field("org/apache/log4j/Level","WARN",
11+
"Lorg/apache/log4j/Level;")
12+
javabridge.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V",
13+
warn_level)

bioformats/metadatatools.py

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
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+

bioformats/noseplugin.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# CellProfiler is distributed under the GNU General Public License.
2+
# See the accompanying file LICENSE for details.
3+
#
4+
# Copyright (c) 2003-2009 Massachusetts Institute of Technology
5+
# Copyright (c) 2009-2013 Broad Institute
6+
#
7+
# Please see the AUTHORS file for credits.
8+
#
9+
# Website: http://www.cellprofiler.org
10+
11+
import logging
12+
from nose.plugins import Plugin
13+
14+
import javabridge
15+
16+
17+
log = logging.getLogger(__name__)
18+
19+
20+
class Log4JPlugin(Plugin):
21+
'''
22+
Plugin that initializes Log4J in order to avoid Bioformats error
23+
messages.
24+
25+
'''
26+
enabled = False
27+
name = "log4j"
28+
score = 90 # Less than the score of javabridge.nosetests.JavaBridgePlugin
29+
30+
def begin(self):
31+
javabridge.static_call("org/apache/log4j/BasicConfigurator",
32+
"configure", "()V")
33+
log4j_logger = javabridge.static_call("org/apache/log4j/Logger",
34+
"getRootLogger",
35+
"()Lorg/apache/log4j/Logger;")
36+
warn_level = javabridge.get_static_field("org/apache/log4j/Level","WARN",
37+
"Lorg/apache/log4j/Level;")
38+
javabridge.call(log4j_logger, "setLevel", "(Lorg/apache/log4j/Level;)V",
39+
warn_level)
433 KB
Binary file not shown.

0 commit comments

Comments
 (0)