hyperspy.external package¶
Submodules¶
hyperspy.external.progressbar module¶
Text progressbar library for python.
This library provides a text mode progressbar. This is tipically used to display the progress of a long running operation, providing a visual clue that processing is underway.
The ProgressBar class manages the progress, and the format of the line is given by a number of widgets. A widget is an object that may display diferently depending on the state of the progress. There are three types of widget: - a string, which always shows itself; - a ProgressBarWidget, which may return a diferent value every time it’s update method is called; and - a ProgressBarWidgetHFill, which is like ProgressBarWidget, except it expands to fill the remaining width of the line.
The progressbar module is very easy to use, yet very powerful. And automatically supports features like auto-resizing when available.
-
class
hyperspy.external.progressbar.
Bar
(marker='#', left='|', right='|')¶ Bases:
hyperspy.external.progressbar.ProgressBarWidgetHFill
The bar of progress. It will strech to fill the line.
-
update
(pbar, width)¶
-
-
class
hyperspy.external.progressbar.
DummyProgressBar
¶ Bases:
object
-
static
finish
()¶
-
static
start
()¶
-
static
update
(*args, **kwargs)¶
-
static
-
class
hyperspy.external.progressbar.
ETA
¶ Bases:
hyperspy.external.progressbar.ProgressBarWidget
Widget for the Estimated Time of Arrival
-
update
(pbar)¶
-
-
class
hyperspy.external.progressbar.
FileTransferSpeed
¶ Bases:
hyperspy.external.progressbar.ProgressBarWidget
Widget for showing the transfer speed (useful for file transfer).
-
update
(pbar)¶
-
-
class
hyperspy.external.progressbar.
MyBar
(text)¶ Bases:
object
Encapsulation of a nice progress bar
-
init
(max)¶
-
update
(i)¶
-
-
class
hyperspy.external.progressbar.
Percentage
¶ Bases:
hyperspy.external.progressbar.ProgressBarWidget
Just the percentage done.
-
update
(pbar)¶
-
-
class
hyperspy.external.progressbar.
ProgressBar
(maxval=100, widgets=[<hyperspy.external.progressbar.Percentage object at 0x7fae1e039128>, ' ', <hyperspy.external.progressbar.Bar object at 0x7fae1e039a90>], term_width=None)¶ Bases:
object
This is the ProgressBar class, it updates and prints the bar.
The term_width parameter may be an integer. Or None, in which case it will try to guess it, if it fails it will default to 80 columns.
The simple use is like this: >>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish()
But anything you want to do is possible (well, almost anything). You can supply different widgets of any type in any order. And you can even write your own widgets! There are many widgets already shipped and you should experiment with them.
When implementing a widget update method you may access any attribute or function of the ProgressBar object calling the widget’s update method. The most important attributes you would like to access are: - currval: current value of the progress, 0 <= currval <= maxval - maxval: maximum (and final) value of the progress - finished: True if the bar is have finished (reached 100%), False o/w - start_time: first time update() method of ProgressBar was called - seconds_elapsed: seconds elapsed since start_time - percentage(): percentage of the progress (this is a method)
-
finish
()¶ Used to tell the progress is finished.
-
handle_resize
(signum, frame)¶
-
percentage
()¶ Returns the percentage of the progress.
-
start
()¶ Start measuring time, and prints the bar at 0%.
It returns self so you can use it like this: >>> pbar = ProgressBar().start() >>> for i in range(100): ... # do something ... pbar.update(i+1) ... >>> pbar.finish()
-
update
(value)¶ Updates the progress bar to a new value.
-
-
class
hyperspy.external.progressbar.
ProgressBarWidget
¶ Bases:
object
This is an element of ProgressBar formatting.
The ProgressBar object will call it’s update value when an update is needed. It’s size may change between call, but the results will not be good if the size changes drastically and repeatedly.
-
update
(pbar)¶ Returns the string representing the widget.
The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made.
At least this function must be overriden.
-
-
class
hyperspy.external.progressbar.
ProgressBarWidgetHFill
¶ Bases:
object
This is a variable width element of ProgressBar formatting.
The ProgressBar object will call it’s update value, informing the width this object must the made. This is like TeX hfill, it will expand to fill the line. You can use more than one in the same line, and they will all have the same width, and together will fill the line.
-
update
(pbar, width)¶ Returns the string representing the widget.
The parameter pbar is a reference to the calling ProgressBar, where one can access attributes of the class for knowing how the update must be made. The parameter width is the total horizontal width the widget must have.
At least this function must be overriden.
-
-
class
hyperspy.external.progressbar.
ReverseBar
(marker='#', left='|', right='|')¶ Bases:
hyperspy.external.progressbar.Bar
The reverse bar of progress, or bar of regress. :)
-
update
(pbar, width)¶
-
-
class
hyperspy.external.progressbar.
RotatingMarker
(markers='|/-\')¶ Bases:
hyperspy.external.progressbar.ProgressBarWidget
A rotating marker for filling the bar of progress.
-
update
(pbar)¶
-
-
hyperspy.external.progressbar.
format_time
(seconds)¶
-
hyperspy.external.progressbar.
progressbar
(text='calculating', maxval=100, disabled=False)¶ Returns a useful default progressbar.
Examples
>>> pbar=progressbar(maxval=10000) >>> for i in range(10000): pbar.update(i) #do some heavy calculation in each step >>> pbar.finish()
-
hyperspy.external.progressbar.
running_from_terminal
()¶
hyperspy.external.tifffile module¶
Read and write image data from and to TIFF files.
Image and metadata can be read from TIFF, BigTIFF, OME-TIFF, STK, LSM, NIH, SGI, ImageJ, MicroManager, FluoView, SEQ and GEL files. Only a subset of the TIFF specification is supported, mainly uncompressed and losslessly compressed 2**(0 to 6) bit integer, 16, 32 and 64-bit float, grayscale and RGB(A) images, which are commonly used in bio-scientific imaging. Specifically, reading JPEG and CCITT compressed image data or EXIF, IPTC, GPS, and XMP metadata is not implemented. Only primary info records are read for STK, FluoView, MicroManager, and NIH image formats.
TIFF, the Tagged Image File Format, is under the control of Adobe Systems. BigTIFF allows for files greater than 4 GB. STK, LSM, FluoView, SGI, SEQ, GEL, and OME-TIFF, are custom extensions defined by Molecular Devices (Universal Imaging Corporation), Carl Zeiss MicroImaging, Olympus, Silicon Graphics International, Media Cybernetics, Molecular Dynamics, and the Open Microscopy Environment consortium respectively.
For command line usage run python tifffile.py --help
Author: | Christoph Gohlke |
---|---|
Organization: | Laboratory for Fluorescence Dynamics, University of California, Irvine |
Version: | 2014.08.24 |
Requirements¶
- CPython 2.7 or 3.4
- Numpy 1.8.2
- Matplotlib 1.4 (optional for plotting)
- Tifffile.c 2013.11.05 (recommended for faster decoding of PackBits and LZW encoded strings)
Notes
The API is not stable yet and might change between revisions.
Tested on little-endian platforms only.
Other Python packages and modules for reading bio-scientific TIFF files:
- Imread
- PyLibTiff
- SimpleITK
- PyLSM
- PyMca.TiffIO.py (same as fabio.TiffIO)
- BioImageXD.Readers
- Cellcognition.io
- CellProfiler.bioformats
Acknowledgements¶
- Egor Zindy, University of Manchester, for cz_lsm_scan_info specifics.
- Wim Lewis for a bug fix and some read_cz_lsm functions.
- Hadrien Mary for help on reading MicroManager files.
References
- TIFF 6.0 Specification and Supplements. Adobe Systems Incorporated. http://partners.adobe.com/public/developer/tiff/
- TIFF File Format FAQ. http://www.awaresystems.be/imaging/tiff/faq.html
- MetaMorph Stack (STK) Image File Format. http://support.meta.moleculardevices.com/docs/t10243.pdf
- Image File Format Description LSM 5/7 Release 6.0 (ZEN 2010). Carl Zeiss MicroImaging GmbH. BioSciences. May 10, 2011
- File Format Description - LSM 5xx Release 2.0. http://ibb.gsf.de/homepage/karsten.rodenacker/IDL/Lsmfile.doc
- The OME-TIFF format. http://www.openmicroscopy.org/site/support/file-formats/ome-tiff
- UltraQuant(r) Version 6.0 for Windows Start-Up Guide. http://www.ultralum.com/images%20ultralum/pdf/UQStart%20Up%20Guide.pdf
- Micro-Manager File Formats. http://www.micro-manager.org/wiki/Micro-Manager_File_Formats
- Tags for TIFF and Related Specifications. Digital Preservation. http://www.digitalpreservation.gov/formats/content/tiff_tags.shtml
Examples
>>> data = numpy.random.rand(5, 301, 219)
>>> imsave('temp.tif', data)
>>> image = imread('temp.tif')
>>> numpy.testing.assert_array_equal(image, data)
>>> with TiffFile('temp.tif') as tif:
... images = tif.asarray()
... for page in tif:
... for tag in page.tags.values():
... t = tag.name, tag.value
... image = page.asarray()
-
hyperspy.external.tifffile.
imsave
(filename, data, **kwargs)¶ Write image data to TIFF file.
Refer to the TiffWriter class and member functions for documentation.
Parameters: - filename (str) – Name of file to write.
- data (array_like) – Input image. The last dimensions are assumed to be image depth, height, width, and samples.
- kwargs (dict) – Parameters ‘byteorder’, ‘bigtiff’, and ‘software’ are passed to the TiffWriter class. Parameters ‘photometric’, ‘planarconfig’, ‘resolution’, ‘description’, ‘compress’, ‘volume’, and ‘extratags’ are passed to the TiffWriter.save function.
Examples
>>> data = numpy.random.rand(2, 5, 3, 301, 219) >>> description = u'{"shape": %s}' % str(list(data.shape)) >>> imsave('temp.tif', data, compress=6, ... extratags=[(270, 's', 0, description, True)])
-
hyperspy.external.tifffile.
imread
(files, **kwargs)¶ Return image data from TIFF file(s) as numpy array.
The first image series is returned if no arguments are provided.
Parameters: - files (str or list) – File name, glob pattern, or list of file names.
- key (int, slice, or sequence of page indices) – Defines which pages to return as array.
- series (int) – Defines which series of pages in file to return as array.
- multifile (bool) – If True (default), OME-TIFF data may include pages from multiple files.
- pattern (str) – Regular expression pattern that matches axes names and indices in file names.
- kwargs (dict) – Additional parameters passed to the TiffFile or TiffSequence asarray function.
Examples
>>> im = imread('test.tif', key=0) >>> im.shape (256, 256, 4) >>> ims = imread(['test.tif', 'test.tif']) >>> ims.shape (2, 256, 256, 4)
-
hyperspy.external.tifffile.
imshow
(data, title=None, vmin=0, vmax=None, cmap=None, bitspersample=None, photometric='rgb', interpolation='nearest', dpi=96, figure=None, subplot=111, maxdim=8192, **kwargs)¶ Plot n-dimensional images using matplotlib.pyplot.
Return figure, subplot and plot axis. Requires pyplot already imported
from matplotlib import pyplot
.Parameters: - bitspersample (int or None) – Number of bits per channel in integer RGB images.
- photometric ({'miniswhite', 'minisblack', 'rgb', or 'palette'}) – The color space of the image data.
- title (str) – Window and subplot title.
- figure (matplotlib.figure.Figure (optional).) – Matplotlib to use for plotting.
- subplot (int) – A matplotlib.pyplot.subplot axis.
- maxdim (int) – maximum image size in any dimension.
- kwargs (optional) – Arguments for matplotlib.pyplot.imshow.
-
class
hyperspy.external.tifffile.
TiffFile
(arg, name=None, offset=None, size=None, multifile=True, multifile_close=True)¶ Bases:
object
Read image and metadata from TIFF, STK, LSM, and FluoView files.
TiffFile instances must be closed using the close method, which is automatically called when using the ‘with’ statement.
-
pages
¶ list – All TIFF pages in file.
-
series
¶ list of Records(shape, dtype, axes, TiffPages) – TIFF pages with compatible shapes and types.
-
micromanager_metadata
¶ dict – Extra MicroManager non-TIFF metadata in the file, if exists.
-
All attributes are read-only.
Examples
>>> with TiffFile('test.tif') as tif: ... data = tif.asarray() ... data.shape (256, 256, 4)
-
asarray
(key=None, series=None, memmap=False)¶ Return image data from multiple TIFF pages as numpy array.
By default the first image series is returned.
Parameters: - key (int, slice, or sequence of page indices) – Defines which pages to return as array.
- series (int) – Defines which series of pages to return as array.
- memmap (bool) – If True, return an array stored in a binary file on disk if possible.
-
close
()¶ Close open file handle(s).
-
filehandle
¶ Return file handle.
-
filename
¶ Return name of file handle.
-
fstat
¶ Lazy object attribute whose value is computed on first access.
-
is_bigtiff
¶ Lazy object attribute whose value is computed on first access.
-
is_fluoview
¶ Lazy object attribute whose value is computed on first access.
-
is_imagej
¶ Lazy object attribute whose value is computed on first access.
-
is_lsm
¶ Lazy object attribute whose value is computed on first access.
-
is_mdgel
¶ Lazy object attribute whose value is computed on first access.
-
is_mediacy
¶ Lazy object attribute whose value is computed on first access.
-
is_micromanager
¶ Lazy object attribute whose value is computed on first access.
-
is_nih
¶ Lazy object attribute whose value is computed on first access.
-
is_ome
¶ Lazy object attribute whose value is computed on first access.
-
is_palette
¶ Lazy object attribute whose value is computed on first access.
-
is_rgb
¶ Lazy object attribute whose value is computed on first access.
-
is_stk
¶ Lazy object attribute whose value is computed on first access.
-
series
Lazy object attribute whose value is computed on first access.
-
-
class
hyperspy.external.tifffile.
TiffWriter
(filename, bigtiff=False, byteorder=None, software='tifffile.py')¶ Bases:
object
Write image data to TIFF file.
TiffWriter instances must be closed using the close method, which is automatically called when using the ‘with’ statement.
Examples
>>> data = numpy.random.rand(2, 5, 3, 301, 219) >>> with TiffWriter('temp.tif', bigtiff=True) as tif: ... for i in range(data.shape[0]): ... tif.save(data[i], compress=6)
-
TAGS
= {'y_resolution': 283, 'datetime': 306, 'orientation': 274, 'predictor': 317, 'subfile_type': 255, 'extra_samples': 338, 'resolution_unit': 296, 'page_name': 285, 'image_description': 270, 'sample_format': 339, 'tile_byte_counts': 325, 'tile_offsets': 324, 'bits_per_sample': 258, 'samples_per_pixel': 277, 'tile_width': 322, 'color_map': 320, 'new_subfile_type': 254, 'image_length': 257, 'image_width': 256, 'compression': 259, 'tile_length': 323, 'document_name': 269, 'software': 305, 'rows_per_strip': 278, 'strip_byte_counts': 279, 'photometric': 262, 'x_resolution': 282, 'planar_configuration': 284, 'tile_depth': 32998, 'image_depth': 32997, 'fill_order': 266, 'strip_offsets': 273}¶
-
TYPES
= {'H': 3, 'h': 8, 'b': 6, 'B': 1, 'q': 17, '2I': 5, 's': 2, 'Q': 16, 'f': 11, 'I': 4, 'd': 12, 'i': 9}¶
-
close
()¶
-
save
(data, photometric=None, planarconfig=None, resolution=None, description=None, volume=False, writeshape=False, compress=0, extratags=())¶ Write image data to TIFF file.
Image data are written in one stripe per plane. Dimensions larger than 2 to 4 (depending on photometric mode, planar configuration, and SGI mode) are flattened and saved as separate pages. The ‘sample_format’ and ‘bits_per_sample’ TIFF tags are derived from the data type.
Parameters: - data (array_like) – Input image. The last dimensions are assumed to be image depth, height, width, and samples.
- photometric ({'minisblack', 'miniswhite', 'rgb'}) – The color space of the image data. By default this setting is inferred from the data shape.
- planarconfig ({'contig', 'planar'}) – Specifies if samples are stored contiguous or in separate planes. By default this setting is inferred from the data shape. ‘contig’: last dimension contains samples. ‘planar’: third last dimension contains samples.
- resolution ((float, float) or ((int, int), (int, int))) – X and Y resolution in dots per inch as float or rational numbers.
- description (str) – The subject of the image. Saved with the first page only.
- compress (int) – Values from 0 to 9 controlling the level of zlib compression. If 0, data are written uncompressed (default).
- volume (bool) – If True, volume data are stored in one tile (if applicable) using the SGI image_depth and tile_depth tags. Image width and depth must be multiple of 16. Few software can read this format, e.g. MeVisLab.
- writeshape (bool) – If True, write the data shape to the image_description tag if necessary and no other description is given.
- extratags (sequence of tuples) –
Additional tags as [(code, dtype, count, value, writeonce)].
- code : int
- The TIFF tag Id.
- dtype : str
- Data type of items in ‘value’ in Python struct format. One of B, s, H, I, 2I, b, h, i, f, d, Q, or q.
- count : int
- Number of data values. Not used for string values.
- value : sequence
- ‘Count’ values compatible with ‘dtype’.
- writeonce : bool
- If True, the tag is written to the first page only.
-
-
class
hyperspy.external.tifffile.
TiffSequence
(files, imread=<class 'hyperspy.external.tifffile.TiffFile'>, pattern='axes', *args, **kwargs)¶ Bases:
object
Sequence of image files.
The data shape and dtype of all files must match.
- files : list
- List of file names.
- shape : tuple
- Shape of image sequence.
- axes : str
- Labels of axes in shape.
Examples
>>> tifs = TiffSequence("test.oif.files/*.tif") >>> tifs.shape, tifs.axes ((2, 100), 'CT') >>> data = tifs.asarray() >>> data.shape (2, 100, 256, 256)
-
exception
ParseError
¶ Bases:
Exception
-
TiffSequence.
asarray
(memmap=False, *args, **kwargs)¶ Read image data from all files and return as single numpy array.
If memmap is True, return an array stored in a binary file on disk. The args and kwargs parameters are passed to the imread function.
Raise IndexError or ValueError if image shapes don’t match.
-
TiffSequence.
close
()¶