Skip to content

Commit 5eb9c45

Browse files
committed
Added documentation.
1 parent 2c4d440 commit 5eb9c45

10 files changed

Lines changed: 248 additions & 107 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/build
33
/docs/_build
44
/python_bioformats.egg-info
5+
/dist

bioformats/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,43 @@
4242
ChannelSeparator = _formatreader.make_reader_wrapper_class(
4343
"loci/formats/ChannelSeparator")
4444

45+
4546
from .metadatatools import createOMEXMLMetadata as create_ome_xml_metadata
4647
from .metadatatools import wrap_imetadata_object
4748
import metadatatools as _metadatatools
4849
PixelType = _metadatatools.make_pixel_type_class()
4950
get_metadata_options = _metadatatools.get_metadata_options
5051

52+
# Reading images
53+
54+
ImageReader = _formatreader.ImageReader
5155
load_image = _formatreader.load_using_bioformats
5256
load_image_url = _formatreader.load_using_bioformats_url
57+
58+
# Cached image readers
59+
5360
get_image_reader = _formatreader.get_image_reader
5461
release_image_reader = _formatreader.release_image_reader
62+
clear_image_reader_cache = _formatreader.clear_image_reader_cache
63+
64+
# Metadata
65+
66+
from .omexml import OMEXML
5567
get_omexml_metadata = _formatreader.get_omexml_metadata
5668

69+
# Writing images
70+
71+
write_image = _formatwriter.write_image
72+
73+
from .omexml import PT_UINT16, PT_UINT8, PT_BIT
74+
75+
# Omero
76+
77+
from .formatreader import use_omero_credentials, set_omero_credentials, get_omero_credentials
78+
from .formatreader import set_omero_login_hook, omero_logout
79+
from .formatreader import K_OMERO_SERVER, K_OMERO_PORT, K_OMERO_USER, K_OMERO_SESSION_ID,\
80+
K_OMERO_PASSWORD, K_OMERO_CONFIG_FILE
81+
5782
from . import omexml
5883

5984
def init_logger():

bioformats/formatreader.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,17 @@ def __init__(self, rdr):
357357
def set_omero_credentials(omero_server, omero_port, omero_username, omero_password):
358358
'''Set the credentials to be used to connect to the Omero server
359359
360-
omero_server - DNS name of the server
360+
:param omero_server: DNS name of the server
361361
362-
omero_port - use this port to connect to the server
362+
:param omero_port: use this port to connect to the server
363363
364-
omero_username - log on as this user
364+
:param omero_username: log on as this user
365365
366-
omero_password - log on using this password
366+
:param omero_password: log on using this password
367367
368-
The session ID is valid after this is called. An exception is thrown
369-
if the login fails. omero_logout() can be called to log out.
368+
The session ID is valid after this function is called. An exception is thrown
369+
if the login fails. :func:`bioformats.omero_logout()` can be called to log out.
370+
370371
'''
371372
global __omero_server
372373
global __omero_username
@@ -388,9 +389,10 @@ def set_omero_credentials(omero_server, omero_port, omero_username, omero_passwo
388389
return __omero_session_id
389390

390391
def get_omero_credentials():
391-
'''Return a pickleable dictionary representing the Omero credentials
392+
'''Return a pickleable dictionary representing the Omero credentials.
392393
393-
Call use_omero_credentials in some other process to use this.
394+
Call :func:`bioformats.use_omero_credentials` in some other process to use this.
395+
394396
'''
395397
if __omero_session_id is None:
396398
omero_login()
@@ -427,14 +429,17 @@ def omero_login():
427429
return __omero_session_id
428430

429431
def omero_logout():
430-
'''Abandon any current Omero session'''
432+
'''Abandon any current Omero session.
433+
434+
'''
431435
global __omero_session_id
432436
__omero_session_id = None
433437

434438
def use_omero_credentials(credentials):
435-
'''Use the session ID from an extant login as credentials
439+
'''Use the session ID from an existing login as credentials.
436440
437-
credentials - credentials from get_omero_credentials
441+
:param credentials: credentials from get_omero_credentials.
442+
438443
'''
439444
global __omero_server
440445
global __omero_username
@@ -451,12 +456,16 @@ def use_omero_credentials(credentials):
451456

452457
__omero_login_fn = None
453458
def set_omero_login_hook(fn):
454-
'''Set the function to be called when a login to Omero is needed'''
459+
'''Set the function to be called when a login to Omero is needed.
460+
461+
'''
455462
global __omero_login_fn
456463
__omero_login_fn = fn
457464

458465
def get_omero_reader():
459-
'''Return an loci.ome.io.OMEROReader instance, wrapped as a FormatReader'''
466+
'''Return an ``loci.ome.io.OMEROReader`` instance, wrapped as a FormatReader.
467+
468+
'''
460469
script = """
461470
var rdr = new Packages.loci.ome.io.OmeroReader();
462471
rdr.setServer(server);
@@ -492,18 +501,19 @@ def load_using_bioformats_url(url, c=None, z=0, t=0, series=None, index=None,
492501

493502

494503
class ImageReader(object):
495-
'''Find the appropriate reader for a file
504+
'''Find the appropriate reader for a file.
496505
497506
This class is meant to be harnessed to a scope like this:
498507
499-
with GetImageReader(path) as rdr:
500-
....
508+
>>> with GetImageReader(path) as reader:
509+
>>> ....
501510
502-
It uses __enter__ and __exit__ to manage the random access stream
511+
It uses `__enter__` and `__exit__` to manage the random access stream
503512
that can be used to cache the file contents in memory.
513+
504514
'''
505-
506-
def __init__(self, path = None, url= None, perform_init=True):
515+
516+
def __init__(self, path=None, url=None, perform_init=True):
507517
self.stream = None
508518
file_scheme = "file:"
509519
self.url = url
@@ -689,17 +699,17 @@ def read(self, c = None, z = 0, t = 0, series = None, index = None,
689699
rescale = True, wants_max_intensity = False, channel_names = None):
690700
'''Read a single plane from the image reader file.
691701
692-
c - read from this channel. None = read color image if multichannel
693-
or interleaved RGB
694-
z - z-stack index
695-
t - time index
696-
series - series for .flex and similar multi-stack formats
697-
index - if None, fall back to zct, otherwise load the indexed frame
698-
rescale - True to rescale the intensity scale to 0 and 1, False to
699-
return the raw values native to the file
700-
wants_max_intensity - if False, only return the image, if True
702+
:param c: read from this channel. `None` = read color image if multichannel
703+
or interleaved RGB.
704+
:param z: z-stack index
705+
:param t: time index
706+
:param series: series for ``.flex`` and similar multi-stack formats
707+
:param index: if `None`, fall back to ``zct``, otherwise load the indexed frame
708+
:param rescale: `True` to rescale the intensity scale to 0 and 1; `False` to
709+
return the raw values native to the file.
710+
:param wants_max_intensity: if `False`, only return the image; if `True`,
701711
return a tuple of image and max intensity
702-
channel_names - provide the channel names for the OME metadata
712+
:param channel_names: provide the channel names for the OME metadata
703713
'''
704714
FormatTools = make_format_tools_class()
705715
ChannelSeparator = make_reader_wrapper_class(
@@ -888,14 +898,15 @@ def load_using_bioformats(path, c=None, z=0, t=0, series=None, index=None,
888898
rescale = True,
889899
wants_max_intensity = False,
890900
channel_names = None):
891-
'''Load the given image file using the Bioformats library
901+
'''Load the given image file using the Bioformats library.
892902
893-
path: path to the file
894-
z: the frame index in the z (depth) dimension.
895-
t: the frame index in the time dimension.
896-
channel_names: None if you don't want them, a list which will be filled if you do
903+
:param path: path to the file
904+
:param z: the frame index in the `z` (depth) dimension.
905+
:param t: the frame index in the time dimension.
906+
:param channel_names: `None` if you don't want them, a list which will be filled if you do.
897907
898-
Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image
908+
:returns: either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image.
909+
899910
'''
900911

901912
with ImageReader(path=path) as rdr:
@@ -905,13 +916,13 @@ def load_using_bioformats(path, c=None, z=0, t=0, series=None, index=None,
905916
def get_omexml_metadata(path=None, url=None):
906917
'''Read the OME metadata from a file using Bio-formats
907918
908-
path - path to the file
919+
:param path: path to the file
909920
910-
allowopenfiles - allow the image reader to open files while looking for
911-
the proper reader class.
912-
913-
groupfiles - utilize the groupfiles option to take the directory structure
921+
:param groupfiles: utilize the groupfiles option to take the directory structure
914922
into account.
923+
924+
:returns: the metdata as XML.
925+
915926
'''
916927
with ImageReader(path=path, url=url, perform_init=False) as rdr:
917928
#

bioformats/formatwriter.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,29 @@ def write_image(pathname, pixels, pixel_type,
4949
c = 0, z = 0, t = 0,
5050
size_c = 1, size_z = 1, size_t = 1,
5151
channel_names = None):
52-
"""Write the image using bioformats
52+
"""Write the image using bioformats.
5353
54-
filename - save to this filename
54+
:param filename: save to this filename
5555
56-
pixels - the image to save
56+
:param pixels: the image to save
5757
58-
pixel_type - save using this pixel type
58+
:param pixel_type: save using this pixel type
5959
60-
c - the image's channel index
60+
:param c: the image's channel index
6161
62-
z - the image's z index
62+
:param z: the image's `z` index
6363
64-
t - the image's t index
64+
:param t: the image's `t` index
6565
66-
sizeC - # of channels in the stack
66+
:param sizeC: # of channels in the stack
6767
68-
sizeZ - # of z stacks
68+
:param sizeZ: # of z stacks
6969
70-
sizeT - # of timepoints in the stack
70+
:param sizeT: # of timepoints in the stack
7171
72-
channel_names - names of the channels (make up names if not present
73-
"""
72+
:param channel_names: names of the channels (make up names if not present).
73+
74+
"""
7475
omexml = ome.OMEXML()
7576
omexml.image(0).Name = os.path.split(pathname)[1]
7677
p = omexml.image(0).Pixels

bioformats/omexml.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ def make_text_node(parent, namespace, tag_name, text):
253253
set_text(node, text)
254254

255255
class OMEXML(object):
256-
'''The OMEXML class reads and writes OME-XML with methods to get and set it
256+
'''Reads and writes OME-XML with methods to get and set it.
257257
258-
The OMEXML class has four main purposes - to parse OME-XML, to output
258+
The OMEXML class has four main purposes: to parse OME-XML, to output
259259
OME-XML, to provide a structured mechanism for inspecting OME-XML and to
260260
let the caller create and modify OME-XML.
261261
@@ -271,36 +271,33 @@ class OMEXML(object):
271271
where it's supported is to use properties on OMEXML and on some of its
272272
derived objects. For instance:
273273
274-
o = OMEXML()
275-
print o.image().AcquiredDate
274+
>>> o = OMEXML()
275+
>>> print o.image().AcquiredDate
276276
277277
will get you the date that image # 0 was acquired.
278278
279-
o = OMEXML()
279+
>>> o = OMEXML()
280+
>>> o.image().Name = "MyImage"
280281
281-
o.image().Name = "MyImage"
282-
283-
will set the image name to "MyImage"
282+
will set the image name to "MyImage".
284283
285284
You can add and remove objects using the "count" properties. Each of these
286285
handles hooking up and removing orphaned elements for you and should be
287286
less error prone than creating orphaned elements and attaching them. For
288287
instance, to create a three-color image:
289288
290-
o = OMEXML()
291-
292-
o.image().Pixels.channel_count = 3
293-
294-
o.image().Pixels.Channel(0).Name = "Red"
295-
296-
o.image().Pixels.Channel(1).Name = "Green"
297-
298-
o.image().Pixels.Channel(2).Name = "Blue"
289+
>>> o = OMEXML()
290+
>>> o.image().Pixels.channel_count = 3
291+
>>> o.image().Pixels.Channel(0).Name = "Red"
292+
>>> o.image().Pixels.Channel(1).Name = "Green"
293+
>>> o.image().Pixels.Channel(2).Name = "Blue"
299294
300-
You can view the OME-XML schema documentation online at:
301-
http://git.openmicroscopy.org/src/develop/components/specification/Documentation/Generated/OME-2011-06/ome.html
295+
See the `OME-XML schema documentation <http://git.openmicroscopy.org/src/develop/components/specification/Documentation/Generated/OME-2011-06/ome.html>`_.
296+
302297
'''
303-
def __init__(self, xml=default_xml):
298+
def __init__(self, xml=None):
299+
if xml is None:
300+
xml = default_xml
304301
if isinstance(xml, unicode):
305302
xml = xml.encode("utf-8")
306303
self.dom = ElementTree.ElementTree(ElementTree.fromstring(xml))
@@ -416,7 +413,16 @@ def set_AcquiredDate(self, date):
416413

417414
@property
418415
def Pixels(self):
419-
'''The OME/Image/Pixels element'''
416+
'''The OME/Image/Pixels element.
417+
418+
Example:
419+
>>> md = bioformats.omexml.OMEXML(xml)
420+
>>> pixels = omemetadata.image(i).Pixels
421+
>>> channel_count = pixels.SizeC
422+
>>> stack_count = pixels.SizeZ
423+
>>> timepoint_count = pixels.SizeT
424+
425+
'''
420426
return OMEXML.Pixels(self.node.find(qnome("Pixels")))
421427

422428
def image(self, index=0):
@@ -1208,4 +1214,4 @@ def set_ImageRef(self, value):
12081214
ref = ElementTree.SubElement(self.node, qn(NS_SPW, "ImageRef"))
12091215
ref.set("ID", value)
12101216
ImageRef = property(get_ImageRef, set_ImageRef)
1211-
1217+

0 commit comments

Comments
 (0)