<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import os <EOL> import sys <EOL> import shutil <EOL> import numpy <EOL> import mmap <EOL> from numpy . testing . utils import memusage <EOL> from . tiff_data import type2name , name2type , type2bytes , type2dtype , tag_value2name , tag_name2value <EOL> from . tiff_data import LittleEndianNumpyDTypes , BigEndianNumpyDTypes , default_tag_values , sample_format_map <EOL> from . utils import bytes2str , isindisk <EOL> from . tiff_base import TiffBase <EOL> from . tiff_sample_plane import TiffSamplePlane <EOL> from . tiff_array import TiffArray <EOL> import lsm <EOL> import tif_lzw <EOL> IFDEntry_init_hooks = [ ] <EOL> IFDEntry_finalize_hooks = [ ] <EOL> IOError_too_many_open_files_hint = '''<STR_LIT>''' <EOL> OSError_operation_not_permitted_hint = '''<STR_LIT>''' <EOL> class TIFFfile ( TiffBase ) : <EOL> """<STR_LIT>""" <EOL> def close ( self ) : <EOL> if hasattr ( self , '<STR_LIT:data>' ) : <EOL> if self . verbose : <EOL> sys . stdout . write ( '<STR_LIT>' % ( self . filename ) ) ; sys . stdout . flush ( ) <EOL> for ifd in self . IFD : <EOL> ifd . close ( ) <EOL> if self . use_memmap : <EOL> pass <EOL> del self . data <EOL> __del__ = close <EOL> def __init__ ( self , filename , mode = '<STR_LIT:r>' , first_byte = <NUM_LIT:0> , verbose = False , local_cache = None , use_memmap = True ) : <EOL> """<STR_LIT>""" <EOL> self . verbose = verbose <EOL> self . first_byte = first_byte <EOL> self . use_memmap = use_memmap <EOL> try : <EOL> if local_cache is not None : <EOL> cache_filename = local_cache + '<STR_LIT:/>' + filename <EOL> if os . path . exists ( cache_filename ) : <EOL> filename = cache_filename <EOL> elif not isindisk ( filename ) : <EOL> assert isindisk ( local_cache ) , ` local_cache ` <EOL> dirname = os . path . dirname ( cache_filename ) <EOL> if not os . path . isdir ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> shutil . copyfile ( filename , cache_filename ) <EOL> filename = cache_filename <EOL> if verbose : <EOL> sys . stdout . write ( '<STR_LIT>' % ( filename ) ) ; sys . stdout . flush ( ) <EOL> if mode != '<STR_LIT:r>' : <EOL> raise NotImplementedError ( ` mode ` ) <EOL> if not os . path . isfile ( filename ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not os . stat ( filename ) . st_size : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if use_memmap : <EOL> self . data = numpy . memmap ( filename , dtype = numpy . ubyte , mode = mode ) <EOL> else : <EOL> assert mode == '<STR_LIT:r>' , ` mode ` <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> self . data = numpy . frombuffer ( f . read ( ) , dtype = numpy . ubyte ) <EOL> f . close ( ) <EOL> except IOError , msg : <EOL> if '<STR_LIT>' in str ( msg ) : <EOL> raise IOError ( IOError_too_many_open_files_hint % msg ) <EOL> if '<STR_LIT>' in str ( msg ) : <EOL> raise IOError ( OSError_operation_not_permitted_hint % msg ) <EOL> raise <EOL> except OSError , msg : <EOL> if '<STR_LIT>' in str ( msg ) : <EOL> raise OSError ( OSError_operation_not_permitted_hint % msg ) <EOL> raise <EOL> except mmap . error , msg : <EOL> if '<STR_LIT>' in str ( msg ) : <EOL> raise mmap . error ( IOError_too_many_open_files_hint % msg ) <EOL> raise <EOL> self . filename = filename <EOL> self . memory_usage = [ ( self . data . nbytes , self . data . nbytes , '<STR_LIT>' ) ] <EOL> byteorder = self . data [ first_byte : first_byte + <NUM_LIT:2> ] . view ( dtype = numpy . uint16 ) [ <NUM_LIT:0> ] <EOL> if byteorder == <NUM_LIT> : <EOL> self . endian = '<STR_LIT>' <EOL> self . dtypes = LittleEndianNumpyDTypes <EOL> elif byteorder == <NUM_LIT> : <EOL> self . endian = '<STR_LIT>' <EOL> self . dtypes = BigEndianNumpyDTypes <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( hex ( byteorder ) ) ) <EOL> magic = self . get_uint16 ( first_byte + <NUM_LIT:2> ) <EOL> if magic != <NUM_LIT> : <EOL> raise ValueError ( '<STR_LIT>' % ( magic ) ) <EOL> self . IFD0 = IFD0 = first_byte + self . get_uint32 ( first_byte + <NUM_LIT:4> ) <EOL> self . memory_usage . append ( ( first_byte , first_byte + <NUM_LIT:8> , '<STR_LIT>' ) ) <EOL> n = self . get_uint16 ( IFD0 ) <EOL> IFD_list = [ ] <EOL> IFD_offset = IFD0 <EOL> while IFD_offset : <EOL> n = self . get_uint16 ( IFD_offset ) <EOL> ifd = IFD ( self ) <EOL> exif_offset = <NUM_LIT:0> <EOL> for i in range ( n ) : <EOL> entry = IFDEntry ( ifd , self , IFD_offset + <NUM_LIT:2> + i * <NUM_LIT:12> ) <EOL> ifd . append ( entry ) <EOL> if entry . tag == <NUM_LIT> : <EOL> exif_offset = entry . value <EOL> ifd . finalize ( ) <EOL> IFD_list . append ( ifd ) <EOL> self . memory_usage . append ( ( IFD_offset , IFD_offset + <NUM_LIT:2> + n * <NUM_LIT:12> + <NUM_LIT:4> , '<STR_LIT>' % ( len ( IFD_list ) , len ( ifd ) ) ) ) <EOL> IFD_offset = self . get_uint32 ( IFD_offset + <NUM_LIT:2> + n * <NUM_LIT:12> ) <EOL> if IFD_offset == <NUM_LIT:0> and exif_offset != <NUM_LIT:0> : <EOL> IFD_offset = exif_offset <EOL> exif_offset = <NUM_LIT:0> <EOL> if verbose : <EOL> sys . stdout . write ( '<STR_LIT>' % ( len ( IFD_list ) ) ) ; sys . stdout . flush ( ) <EOL> self . IFD = IFD_list <EOL> if verbose : <EOL> sys . stdout . write ( '<STR_LIT>' ) ; sys . stdout . flush ( ) <EOL> self . time = None <EOL> def set_time ( self , time ) : <EOL> self . time = time <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . filename ) <EOL> def get_uint16 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:2> ] . view ( dtype = self . dtypes . uint16 ) [ <NUM_LIT:0> ] <EOL> def get_uint32 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:4> ] . view ( dtype = self . dtypes . uint32 ) [ <NUM_LIT:0> ] <EOL> def get_int16 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:2> ] . view ( dtype = self . dtypes . int16 ) [ <NUM_LIT:0> ] <EOL> def get_int32 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:4> ] . view ( dtype = self . dtypes . int32 ) [ <NUM_LIT:0> ] <EOL> def get_float32 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:4> ] . view ( dtype = self . dtypes . float32 ) [ <NUM_LIT:0> ] <EOL> def get_float64 ( self , offset ) : <EOL> return self . data [ offset : offset + <NUM_LIT:8> ] . view ( dtype = self . dtypes . float64 ) [ <NUM_LIT:0> ] <EOL> get_short = get_uint16 <EOL> get_long = get_uint32 <EOL> get_double = get_float64 <EOL> def get_value ( self , offset , typ ) : <EOL> values = self . get_values ( offset , typ , <NUM_LIT:1> ) <EOL> if values is not None : <EOL> return values [ <NUM_LIT:0> ] <EOL> def get_values ( self , offset , typ , count ) : <EOL> if isinstance ( typ , numpy . dtype ) : <EOL> dtype = typ <EOL> bytes = typ . itemsize <EOL> elif isinstance ( typ , type ) and issubclass ( typ , numpy . generic ) : <EOL> dtype = typ <EOL> bytes = typ ( ) . itemsize <EOL> else : <EOL> if isinstance ( typ , str ) : <EOL> ntyp = typ <EOL> typ = name2type . get ( typ ) <EOL> else : <EOL> ntyp = str ( typ ) <EOL> dtype = self . dtypes . type2dt . get ( typ ) <EOL> bytes = type2bytes . get ( typ ) <EOL> if dtype is None or bytes is None : <EOL> sys . stderr . write ( '<STR_LIT>' % ( typ , ntyp , dtype , bytes ) ) <EOL> return <EOL> return self . data [ offset : offset + bytes * count ] . view ( dtype = dtype ) <EOL> def get_string ( self , offset , length = None ) : <EOL> if length is None : <EOL> i = <NUM_LIT:0> <EOL> while self . data [ offset + i ] : <EOL> i += <NUM_LIT:1> <EOL> length = i <EOL> string = self . get_values ( offset , '<STR_LIT>' , length ) . tostring ( ) <EOL> return string <EOL> def check_memory_usage ( self , verbose = True ) : <EOL> '''<STR_LIT>''' <EOL> l = [ ] <EOL> l . extend ( self . memory_usage ) <EOL> for ifd in self . IFD : <EOL> l . extend ( ifd . memory_usage ) <EOL> l . sort ( ) <EOL> last_end = None <EOL> ok = True <EOL> for start , end , resource in l : <EOL> if last_end : <EOL> if last_end != start : <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start - last_end ) <EOL> ok = False <EOL> if start < last_end and verbose : <EOL> print '<STR_LIT>' <EOL> if verbose : <EOL> print '<STR_LIT>' % ( start , end , end - start , resource ) <EOL> last_end = end <EOL> return ok <EOL> def is_contiguous ( self ) : <EOL> for i , ifd in enumerate ( self . IFD ) : <EOL> strip_offsets = ifd . get ( '<STR_LIT>' ) . value <EOL> strip_nbytes = ifd . get ( '<STR_LIT>' ) . value <EOL> if not ifd . is_contiguous ( ) : <EOL> return False <EOL> if i == <NUM_LIT:0> : <EOL> pass <EOL> else : <EOL> if isinstance ( strip_offsets , numpy . ndarray ) : <EOL> start = strip_offsets [ <NUM_LIT:0> ] <EOL> else : <EOL> start = strip_offsets <EOL> if end != start : <EOL> return False <EOL> if isinstance ( strip_offsets , numpy . ndarray ) : <EOL> end = strip_offsets [ - <NUM_LIT:1> ] + strip_nbytes [ - <NUM_LIT:1> ] <EOL> else : <EOL> end = strip_offsets + strip_nbytes <EOL> return True <EOL> def get_contiguous ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_contiguous ( ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> ifd0 = self . IFD [ <NUM_LIT:0> ] <EOL> ifd1 = self . IFD [ - <NUM_LIT:1> ] <EOL> width = ifd0 . get ( '<STR_LIT>' ) . value <EOL> length = ifd0 . get ( '<STR_LIT>' ) . value <EOL> assert width == ifd1 . get ( '<STR_LIT>' ) . value <EOL> assert length == ifd1 . get ( '<STR_LIT>' ) . value <EOL> depth = len ( self . IFD ) <EOL> compression = ifd . get ( '<STR_LIT>' ) . value <EOL> if compression != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> bits_per_sample = ifd0 . get ( '<STR_LIT>' ) . value <EOL> photo_interp = ifd0 . get ( '<STR_LIT>' ) . value <EOL> planar_config = ifd0 . get ( '<STR_LIT>' ) . value <EOL> strip_offsets0 = ifd0 . get ( '<STR_LIT>' ) . value <EOL> strip_nbytes0 = ifd0 . get ( '<STR_LIT>' ) . value <EOL> strip_offsets1 = ifd1 . get ( '<STR_LIT>' ) . value <EOL> strip_nbytes1 = ifd1 . get ( '<STR_LIT>' ) . value <EOL> samples_per_pixel = ifd1 . get ( '<STR_LIT>' ) . value <EOL> assert samples_per_pixel == <NUM_LIT:1> , ` samples_per_pixel ` <EOL> if isinstance ( bits_per_sample , numpy . ndarray ) : <EOL> dtype = getattr ( self . dtypes , '<STR_LIT>' % ( bits_per_sample [ i ] ) ) <EOL> else : <EOL> dtype = getattr ( self . dtypes , '<STR_LIT>' % ( bits_per_sample ) ) <EOL> if isinstance ( strip_offsets0 , numpy . ndarray ) : <EOL> start = strip_offsets0 [ <NUM_LIT:0> ] <EOL> end = strip_offsets1 [ - <NUM_LIT:1> ] + strip_nbytes1 [ - <NUM_LIT:1> ] <EOL> else : <EOL> start = strip_offsets0 <EOL> end = strip_offsets1 + strip_nbytes1 <EOL> return self . data [ start : end ] . view ( dtype = dtype ) . reshape ( ( depth , width , length ) ) <EOL> def get_subfile_types ( self ) : <EOL> """<STR_LIT>""" <EOL> s = set ( ) <EOL> for ifd in self . IFD : <EOL> s . add ( ifd . get_value ( '<STR_LIT>' ) ) <EOL> return sorted ( s ) <EOL> def get_depth ( self , subfile_type = <NUM_LIT:0> ) : <EOL> depth = <NUM_LIT:0> <EOL> for ifd in self . IFD : <EOL> if ifd . get_value ( '<STR_LIT>' ) == subfile_type : <EOL> depth += <NUM_LIT:1> <EOL> return depth <EOL> def get_first_ifd ( self , subfile_type = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for ifd in self . IFD : <EOL> if ifd . get_value ( '<STR_LIT>' ) == subfile_type : <EOL> return ifd <EOL> def get_tiff_array ( self , sample_index = <NUM_LIT:0> , subfile_type = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> planes = [ ] <EOL> index = <NUM_LIT:0> <EOL> time_lst = self . time <EOL> for ifd in self . IFD : <EOL> if ifd . get_value ( '<STR_LIT>' , subfile_type ) != subfile_type : <EOL> continue <EOL> plane = TiffSamplePlane ( ifd , sample_index = sample_index ) <EOL> if time_lst is not None : <EOL> plane . set_time ( time_lst [ index ] ) <EOL> planes . append ( plane ) <EOL> index += <NUM_LIT:1> <EOL> tiff_array = TiffArray ( planes ) <EOL> return tiff_array <EOL> def get_samples ( self , subfile_type = <NUM_LIT:0> , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> i = <NUM_LIT:0> <EOL> step = <NUM_LIT:0> <EOL> can_return_memmap = True <EOL> ifd_lst = [ ifd for ifd in self . IFD if ifd . get_value ( '<STR_LIT>' ) == subfile_type ] <EOL> depth = len ( ifd_lst ) <EOL> full_l = [ ] <EOL> for ifd in ifd_lst : <EOL> if not ifd . is_contiguous ( ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> strip_offsets = ifd . get_value ( '<STR_LIT>' ) <EOL> strip_nbytes = ifd . get_value ( '<STR_LIT>' ) <EOL> l . append ( ( strip_offsets [ <NUM_LIT:0> ] , strip_offsets [ - <NUM_LIT:1> ] + strip_nbytes [ - <NUM_LIT:1> ] ) ) <EOL> for off , nb in zip ( strip_offsets , strip_nbytes ) : <EOL> full_l . append ( ( off , off + nb ) ) <EOL> if i == <NUM_LIT:0> : <EOL> compression = ifd . get_value ( '<STR_LIT>' ) <EOL> if compression != <NUM_LIT:1> : <EOL> can_return_memmap = False <EOL> width = ifd . get_value ( '<STR_LIT>' ) <EOL> length = ifd . get_value ( '<STR_LIT>' ) <EOL> samples_per_pixel = ifd . get_value ( '<STR_LIT>' ) <EOL> planar_config = ifd . get_value ( '<STR_LIT>' ) <EOL> bits_per_sample = ifd . get_value ( '<STR_LIT>' ) <EOL> sample_format = ifd . get_value ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> photometric_interpretation = ifd . get_value ( '<STR_LIT>' ) <EOL> if self . is_lsm or not isinstance ( strip_offsets , numpy . ndarray ) : <EOL> strips_per_image = <NUM_LIT:1> <EOL> else : <EOL> strips_per_image = len ( strip_offsets ) <EOL> format = sample_format_map . get ( sample_format ) <EOL> if format is None : <EOL> print '<STR_LIT>' % ( sample_format ) <EOL> format = '<STR_LIT>' <EOL> dtype_lst = [ ] <EOL> bits_per_pixel = <NUM_LIT:0> <EOL> for j in range ( samples_per_pixel ) : <EOL> bits = bits_per_sample [ j ] <EOL> bits_per_pixel += bits <EOL> dtype = getattr ( self . dtypes , '<STR_LIT>' % ( format , bits ) ) <EOL> dtype_lst . append ( dtype ) <EOL> bytes_per_pixel = bits_per_pixel // <NUM_LIT:8> <EOL> assert <NUM_LIT:8> * bytes_per_pixel == bits_per_pixel , ` bits_per_pixel ` <EOL> bytes_per_row = width * bytes_per_pixel <EOL> strip_length = l [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> strip_length_str = bytes2str ( strip_length ) <EOL> bytes_per_image = length * bytes_per_row <EOL> rows_per_strip = bytes_per_image // ( bytes_per_row * strips_per_image ) <EOL> if bytes_per_image % ( bytes_per_row * strips_per_image ) : <EOL> rows_per_strip += <NUM_LIT:1> <EOL> assert rows_per_strip == ifd . get_value ( '<STR_LIT>' , rows_per_strip ) , ` rows_per_strip , ifd . get_value ( '<STR_LIT>' ) , bytes_per_image , bytes_per_row , strips_per_image , self . filename ` <EOL> else : <EOL> assert width == ifd . get_value ( '<STR_LIT>' , width ) , ` width , ifd . get_value ( '<STR_LIT>' ) ` <EOL> assert length == ifd . get_value ( '<STR_LIT>' , length ) , ` length , ifd . get_value ( '<STR_LIT>' ) ` <EOL> assert planar_config == ifd . get_value ( '<STR_LIT>' , planar_config ) <EOL> if can_return_memmap : <EOL> assert strip_length == l [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , ` strip_length , l [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] ` <EOL> else : <EOL> strip_length = max ( strip_length , l [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] - l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> strip_length_str = '<STR_LIT>' + bytes2str ( strip_length ) <EOL> assert ( bits_per_sample == ifd . get_value ( '<STR_LIT>' , bits_per_sample ) ) . all ( ) , ` bits_per_sample , ifd . get_value ( '<STR_LIT>' ) ` <EOL> if i > <NUM_LIT:0> : <EOL> if i == <NUM_LIT:1> : <EOL> step = l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] - l [ - <NUM_LIT:2> ] [ <NUM_LIT:1> ] <EOL> assert step >= <NUM_LIT:0> , ` step , l [ - <NUM_LIT:2> ] , l [ - <NUM_LIT:1> ] ` <EOL> else : <EOL> if step != l [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] - l [ - <NUM_LIT:2> ] [ <NUM_LIT:1> ] : <EOL> can_return_memmap = False <EOL> i += <NUM_LIT:1> <EOL> if verbose : <EOL> bytes_per_image_str = bytes2str ( bytes_per_image ) <EOL> print '''<STR_LIT>''' % ( locals ( ) ) <EOL> if photometric_interpretation == <NUM_LIT:2> : <EOL> assert samples_per_pixel == <NUM_LIT:3> , ` samples_per_pixel ` <EOL> sample_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> sample_names = [ '<STR_LIT>' % ( j ) for j in range ( samples_per_pixel ) ] <EOL> depth = i <EOL> if not can_return_memmap : <EOL> if planar_config == <NUM_LIT:1> : <EOL> if samples_per_pixel == <NUM_LIT:1> : <EOL> i = <NUM_LIT:0> <EOL> arr = numpy . empty ( depth * bytes_per_image , dtype = self . dtypes . uint8 ) <EOL> bytes_per_strip = rows_per_strip * bytes_per_row <EOL> for start , end in full_l : <EOL> if compression == <NUM_LIT:1> : <EOL> d = self . data [ start : end ] <EOL> elif compression == <NUM_LIT:5> : <EOL> d = self . data [ start : end ] <EOL> d = tif_lzw . decode ( d , bytes_per_strip ) <EOL> arr [ i : i + d . nbytes ] = d <EOL> i += d . nbytes <EOL> arr = arr . view ( dtype = dtype_lst [ <NUM_LIT:0> ] ) . reshape ( ( depth , length , width ) ) <EOL> return [ arr ] , sample_names <EOL> else : <EOL> i = <NUM_LIT:0> <EOL> arr = numpy . empty ( depth * bytes_per_image , dtype = self . dtypes . uint8 ) <EOL> bytes_per_strip = rows_per_strip * bytes_per_row <EOL> for start , end in full_l : <EOL> sys . stdout . write ( "<STR_LIT>" % ( start , end ) ) ; sys . stdout . flush ( ) <EOL> if compression == <NUM_LIT:1> : <EOL> d = self . data [ start : end ] <EOL> elif compression == <NUM_LIT:5> : <EOL> d = self . data [ start : end ] <EOL> d = tif_lzw . decode ( d , bytes_per_strip ) <EOL> arr [ i : i + d . nbytes ] = d <EOL> i += d . nbytes <EOL> dt = numpy . dtype ( dict ( names = sample_names , formats = dtype_lst ) ) <EOL> arr = arr . view ( dtype = dt ) . reshape ( ( depth , length , width ) ) <EOL> return [ arr [ n ] for n in arr . dtype . names ] , arr . dtype . names <EOL> raise NotImplementedError ( ` depth , bytes_per_image , samples_per_pixel ` ) <EOL> else : <EOL> raise NotImplementedError ( ` planar_config ` ) <EOL> start = l [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> end = l [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> if start > step : <EOL> arr = self . data [ start - step : end ] . reshape ( ( depth , strip_length + step ) ) <EOL> k = step <EOL> elif end <= self . data . size - step : <EOL> arr = self . data [ start : end + step ] . reshape ( ( depth , strip_length + step ) ) <EOL> k = <NUM_LIT:0> <EOL> else : <EOL> raise NotImplementedError ( ` start , end , step ` ) <EOL> sys . stdout . flush ( ) <EOL> if planar_config == <NUM_LIT:2> : <EOL> if self . is_lsm : <EOL> if subfile_type == <NUM_LIT:0> : <EOL> sample_names = self . lsminfo . get ( '<STR_LIT>' ) <EOL> elif subfile_type == <NUM_LIT:1> : <EOL> sample_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert samples_per_pixel == <NUM_LIT:3> , ` samples_per_pixel ` <EOL> else : <EOL> raise NotImplementedError ( ` subfile_type ` ) <EOL> samples = [ ] <EOL> for j in range ( samples_per_pixel ) : <EOL> bytes = bits_per_sample [ j ] // <NUM_LIT:8> * width * length <EOL> tmp = arr [ : , k : k + bytes ] <EOL> tmp = tmp . view ( dtype = dtype_lst [ j ] ) <EOL> tmp = tmp . reshape ( ( depth , length , width ) ) <EOL> samples . append ( tmp ) <EOL> k += bytes <EOL> return samples , sample_names <EOL> raise NotImplementedError ( ` planar_config , self . is_lsm ` ) <EOL> elif planar_config == <NUM_LIT:1> : <EOL> samples = [ ] <EOL> bytes = sum ( bits_per_sample [ : samples_per_pixel ] ) // <NUM_LIT:8> * width * length <EOL> bytes_per_sample = bits_per_sample // <NUM_LIT:8> <EOL> for j in range ( samples_per_pixel ) : <EOL> i0 = k + j * bytes_per_sample [ j ] <EOL> tmp = arr [ : , i0 : i0 + bytes : samples_per_pixel ] <EOL> tmp = numpy . array ( tmp . reshape ( ( tmp . size , ) ) ) <EOL> tmp = tmp . view ( dtype = dtype_lst [ j ] ) <EOL> tmp = tmp . reshape ( ( depth , length , width ) ) <EOL> samples . append ( tmp ) <EOL> return samples , sample_names <EOL> else : <EOL> raise NotImplementedError ( ` planar_config ` ) <EOL> def get_info ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> subfile_types = self . get_subfile_types ( ) <EOL> l . append ( '<STR_LIT>' % ( len ( subfile_types ) ) ) <EOL> for subfile_type in subfile_types : <EOL> ifd = self . get_first_ifd ( subfile_type = subfile_type ) <EOL> l . append ( '<STR_LIT:->' * <NUM_LIT:50> ) <EOL> l . append ( '<STR_LIT>' % ( subfile_type ) ) <EOL> l . append ( '<STR_LIT>' % ( self . get_depth ( subfile_type = subfile_type ) ) ) <EOL> for tag in [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] : <EOL> v = ifd . get_value ( tag , human = True ) <EOL> if v is None : <EOL> continue <EOL> if tag == '<STR_LIT>' and subfile_type == <NUM_LIT:0> : <EOL> if v . startswith ( '<STR_LIT>' ) or v [ : <NUM_LIT:4> ] . lower ( ) == '<STR_LIT>' : <EOL> try : <EOL> import lxml . etree <EOL> tree = lxml . etree . fromstring ( v ) <EOL> v = lxml . etree . tostring ( tree , pretty_print = True ) <EOL> except Exception , msg : <EOL> print '<STR_LIT>' % ( self . __class__ . __name__ , msg ) <EOL> l . append ( '<STR_LIT>' % ( tag , v ) ) <EOL> else : <EOL> l . append ( '<STR_LIT>' % ( tag , v ) ) <EOL> else : <EOL> l . append ( '<STR_LIT>' % ( tag , v ) ) <EOL> if self . is_lsm and subfile_types == <NUM_LIT:0> : <EOL> l . append ( '<STR_LIT>' % ( self . lsminfo ) ) <EOL> return '<STR_LIT:\n>' . join ( l ) <EOL> TiffFile = TIFFfile <EOL> class IFD : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tiff ) : <EOL> self . tiff = tiff <EOL> self . entries = [ ] <EOL> self . entries_dict = { } <EOL> def __len__ ( self ) : <EOL> return len ( self . entries ) <EOL> def append ( self , entry ) : <EOL> self . entries . append ( entry ) <EOL> self . entries_dict [ getattr ( entry , '<STR_LIT>' , id ( entry ) ) ] = entry <EOL> def close ( self ) : <EOL> for entry in self . entries : <EOL> entry . close ( ) <EOL> self . entries [ : ] = [ ] <EOL> self . entries_dict . clear ( ) <EOL> @ property <EOL> def memory_usage ( self ) : <EOL> l = [ ] <EOL> for entry in self . entries : <EOL> l . extend ( entry . memory_usage ) <EOL> return l <EOL> def __str__ ( self ) : <EOL> l = [ ] <EOL> for entry in self . entries : <EOL> l . append ( str ( entry ) ) <EOL> return '<STR_LIT:\n>' . join ( l ) <EOL> def human ( self ) : <EOL> l = [ ] <EOL> for entry in self . entries : <EOL> l . append ( entry . human ( ) ) <EOL> return '<STR_LIT:\n>' . join ( l ) <EOL> def get ( self , tag_name ) : <EOL> """<STR_LIT>""" <EOL> return self . entries_dict . get ( tag_name ) <EOL> def get_value ( self , tag_name , default = None , human = False ) : <EOL> """<STR_LIT>""" <EOL> entry = self . get ( tag_name ) <EOL> if entry is not None : <EOL> value = entry . value <EOL> else : <EOL> if default is None : <EOL> if default_tag_values . has_key ( tag_name ) : <EOL> value = default_tag_values [ tag_name ] <EOL> else : <EOL> value = None <EOL> sys . stdout . write ( '<STR_LIT>' % ( self . __class__ . __name__ , tag_name ) ) <EOL> else : <EOL> value = default <EOL> if tag_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if not isinstance ( value , numpy . ndarray ) : <EOL> value = numpy . array ( [ value ] ) <EOL> if tag_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> samples_per_pixel = self . get_value ( '<STR_LIT>' ) <EOL> if not isinstance ( value , numpy . ndarray ) : <EOL> value = numpy . array ( [ value ] * samples_per_pixel ) <EOL> if tag_name in [ '<STR_LIT>' ] : <EOL> value = value [ : samples_per_pixel ] <EOL> if tag_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if value is not None : <EOL> return '<STR_LIT>' . join ( value . view ( '<STR_LIT>' % ( value . nbytes // value . size ) ) ) <EOL> if human : <EOL> if tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:1> : '<STR_LIT:None>' , <NUM_LIT:2> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:0> : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <EOL> <NUM_LIT:4> : '<STR_LIT>' , <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT:8> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' , <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT:5> : '<STR_LIT>' , <NUM_LIT:6> : '<STR_LIT>' , <NUM_LIT:7> : '<STR_LIT>' , <NUM_LIT:8> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> new_value = [ ] <EOL> for v in value : <EOL> new_value . append ( type2name . get ( v , v ) ) <EOL> value = new_value <EOL> elif tag_name == '<STR_LIT>' : <EOL> value = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' , <NUM_LIT:3> : '<STR_LIT>' } . get ( value , value ) <EOL> elif tag_name == '<STR_LIT>' : <EOL> pass <EOL> return value <EOL> def get_sample_names ( self ) : <EOL> subfile_type = self . get_value ( '<STR_LIT>' ) <EOL> samples_per_pixel = self . get_value ( '<STR_LIT>' ) <EOL> if self . tiff . is_lsm : <EOL> if subfile_type == <NUM_LIT:0> : <EOL> return self . tiff . lsminfo . get ( '<STR_LIT>' ) <EOL> if subfile_type == <NUM_LIT:1> and samples_per_pixel == <NUM_LIT:3> : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return [ '<STR_LIT>' % i for i in range ( samples_per_pixel ) ] <EOL> def get_pixel_name ( self ) : <EOL> subfile_type = self . get_value ( '<STR_LIT>' ) <EOL> samples_per_pixel = self . get_value ( '<STR_LIT>' ) <EOL> if self . tiff . is_lsm : <EOL> if subfile_type == <NUM_LIT:0> : <EOL> return '<STR_LIT:_>' . join ( self . tiff . lsminfo . get ( '<STR_LIT>' ) ) <EOL> if subfile_type == <NUM_LIT:1> and samples_per_pixel == <NUM_LIT:3> : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def get_sample_dtypes ( self ) : <EOL> sample_format = self . get_value ( '<STR_LIT>' ) <EOL> bits_per_sample = self . get_value ( '<STR_LIT>' ) <EOL> return [ self . tiff . dtypes . get_dtype ( f , b ) for f , b in zip ( sample_format , bits_per_sample ) ] <EOL> def get_pixel_dtype ( self ) : <EOL> sample_names = self . get_sample_names ( ) <EOL> sample_dtypes = self . get_sample_dtypes ( ) <EOL> return numpy . dtype ( zip ( sample_names , sample_dtypes ) ) <EOL> def get_pixel_typename ( self ) : <EOL> sample_dtypes = self . get_sample_dtypes ( ) <EOL> return '<STR_LIT:_>' . join ( map ( str , sample_dtypes ) ) <EOL> def finalize ( self ) : <EOL> for entry in self . entries : <EOL> for hook in IFDEntry_finalize_hooks : <EOL> hook ( entry ) <EOL> def is_contiguous ( self ) : <EOL> strip_offsets = self . get ( '<STR_LIT>' ) . value <EOL> strip_nbytes = self . get ( '<STR_LIT>' ) . value <EOL> if isinstance ( strip_offsets , numpy . ndarray ) : <EOL> for i in range ( len ( strip_offsets ) - <NUM_LIT:1> ) : <EOL> if strip_offsets [ i ] + strip_nbytes [ i ] != strip_offsets [ i + <NUM_LIT:1> ] : <EOL> return False <EOL> return True <EOL> def get_contiguous ( self , channel_name = None ) : <EOL> """<STR_LIT>""" <EOL> width = self . get ( '<STR_LIT>' ) . value <EOL> length = self . get ( '<STR_LIT>' ) . value <EOL> strip_offsets = self . get ( '<STR_LIT>' ) . value <EOL> strip_nbytes = self . get ( '<STR_LIT>' ) . value <EOL> bits_per_sample = self . get ( '<STR_LIT>' ) . value <EOL> photo_interp = self . get ( '<STR_LIT>' ) . value <EOL> planar_config = self . get ( '<STR_LIT>' ) . value <EOL> compression = self . get ( '<STR_LIT>' ) . value <EOL> subfile_type = self . get ( '<STR_LIT>' ) . value <EOL> if compression != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not self . is_contiguous ( ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if self . tiff . is_lsm : <EOL> lsminfo = self . tiff . lsminfo <EOL> if subfile_type == <NUM_LIT:0> : <EOL> channel_names = lsminfo . get ( '<STR_LIT>' ) <EOL> elif subfile_type == <NUM_LIT:1> : <EOL> if photo_interp == <NUM_LIT:2> : <EOL> channel_names = '<STR_LIT>' <EOL> else : <EOL> raise NotImplementedError ( ` photo_interp ` ) <EOL> else : <EOL> raise NotImplementedError ( ` subfile_type ` ) <EOL> assert planar_config == <NUM_LIT:2> , ` planar_config ` <EOL> nof_channels = self . tiff . lsmentry [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> scantype = self . tiff . lsmentry [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> assert scantype == <NUM_LIT:0> , ` scantype ` <EOL> r = { } <EOL> for i in range ( nof_channels ) : <EOL> if isinstance ( bits_per_sample , numpy . ndarray ) : <EOL> dtype = getattr ( self . dtypes , '<STR_LIT>' % ( bits_per_sample [ i ] ) ) <EOL> r [ channel_names [ i ] ] = self . tiff . data [ strip_offsets [ i ] : strip_offsets [ i ] + strip_nbytes [ i ] ] . view ( dtype = dtype ) . reshape ( ( width , length ) ) <EOL> else : <EOL> dtype = getattr ( self . dtypes , '<STR_LIT>' % ( bits_per_sample ) ) <EOL> r [ channel_names [ i ] ] = self . tiff . data [ strip_offsets : strip_offsets + strip_nbytes ] . view ( dtype = dtype ) . reshape ( ( width , length ) ) <EOL> return r <EOL> else : <EOL> raise NotImplementedError ( ` self . tiff ` ) <EOL> def get_voxel_sizes ( self ) : <EOL> tiff = self . tiff <EOL> if tiff . is_lsm : <EOL> sample_spacing = tiff . lsminfo . get ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> line_spacing = tiff . lsminfo . get ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> plane_spacing = tiff . lsminfo . get ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> return ( plane_spacing , line_spacing , sample_spacing ) <EOL> descr = self . get_value ( '<STR_LIT>' , human = True ) <EOL> if descr is None : <EOL> return ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if descr . startswith ( '<STR_LIT>' ) or descr [ : <NUM_LIT:4> ] . lower ( ) == '<STR_LIT>' : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> for vx , vy , vz in [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] : <EOL> ix = descr . find ( vx ) <EOL> iy = descr . find ( vy ) <EOL> iz = descr . find ( vz ) <EOL> if ix == - <NUM_LIT:1> : x = <NUM_LIT:1> <EOL> else : x = float ( descr [ ix : ] . split ( None , <NUM_LIT:2> ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> if iy == - <NUM_LIT:1> : y = <NUM_LIT:1> <EOL> else : y = float ( descr [ iy : ] . split ( None , <NUM_LIT:2> ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> if iz == - <NUM_LIT:1> : z = <NUM_LIT:1> <EOL> else : z = float ( descr [ iz : ] . split ( None , <NUM_LIT:2> ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> if - <NUM_LIT:1> not in [ ix , iy ] : <EOL> return ( z , y , x ) <EOL> print '<STR_LIT>' % ( descr ) <EOL> return ( z , y , x ) <EOL> def get_pixel_sizes ( self ) : <EOL> tiff = self . tiff <EOL> if tiff . is_lsm : <EOL> sample_spacing = tiff . lsminfo . get ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> line_spacing = tiff . lsminfo . get ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> return ( line_spacing , sample_spacing ) <EOL> descr = self . get_value ( '<STR_LIT>' , human = True ) <EOL> if descr is None : <EOL> return ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> if descr . startswith ( '<STR_LIT>' ) or descr [ : <NUM_LIT:4> ] . lower ( ) == '<STR_LIT>' : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> for vx , vy , vz in [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] : <EOL> ix = descr . find ( vx ) <EOL> iy = descr . find ( vy ) <EOL> if ix == - <NUM_LIT:1> : x = <NUM_LIT:1> <EOL> else : x = float ( descr [ ix : ] . split ( None , <NUM_LIT:2> ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> if iy == - <NUM_LIT:1> : y = <NUM_LIT:1> <EOL> else : y = float ( descr [ iy : ] . split ( None , <NUM_LIT:2> ) [ <NUM_LIT:1> ] . strip ( ) ) <EOL> if - <NUM_LIT:1> not in [ ix , iy ] : <EOL> return ( y , x ) <EOL> print '<STR_LIT>' % ( descr ) <EOL> return ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> class IFDEntry : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ifd , tiff , offset ) : <EOL> self . ifd = ifd <EOL> self . tiff = tiff <EOL> self . offset = offset <EOL> self . tag = tiff . get_uint16 ( offset ) <EOL> self . type = tiff . get_uint16 ( offset + <NUM_LIT:2> ) <EOL> self . count = tiff . get_uint32 ( offset + <NUM_LIT:4> ) <EOL> for hook in IFDEntry_init_hooks : <EOL> hook ( self ) <EOL> self . bytes = bytes = type2bytes . get ( self . type , <NUM_LIT:0> ) <EOL> if self . count == <NUM_LIT:1> and <NUM_LIT:1> <= bytes <= <NUM_LIT:4> : <EOL> self . offset = None <EOL> value = tiff . get_value ( offset + <NUM_LIT:8> , self . type ) <EOL> else : <EOL> self . offset = tiff . get_int32 ( offset + <NUM_LIT:8> ) <EOL> value = tiff . get_values ( self . offset , self . type , self . count ) <EOL> if value is not None : <EOL> self . value = value <EOL> tag_name = self . tag_name = tag_value2name . get ( self . tag , '<STR_LIT>' % ( hex ( self . tag ) , ) ) <EOL> self . type_name = type2name . get ( self . type , '<STR_LIT>' % ( self . type , ) ) <EOL> self . memory_usage = [ ] <EOL> if self . offset is not None : <EOL> self . memory_usage . append ( ( self . offset , self . offset + self . bytes * self . count , self . tag_name ) ) <EOL> def close ( self ) : <EOL> del self . value <EOL> @ property <EOL> def _value_str ( self ) : <EOL> tag_name = self . tag_name <EOL> value = self . value <EOL> if value is not None : <EOL> if tag_name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return '<STR_LIT>' . join ( value . view ( '<STR_LIT>' % ( value . nbytes // value . size ) ) ) <EOL> return value <EOL> def __str__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> r = self . str_hook ( self ) <EOL> if isinstance ( r , str ) : <EOL> return r <EOL> if hasattr ( self , '<STR_LIT:value>' ) : <EOL> return '<STR_LIT>' % ( self . __dict__ ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . __dict__ ) <EOL> def human ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> r = self . str_hook ( self ) <EOL> if isinstance ( r , str ) : <EOL> return r <EOL> if hasattr ( self , '<STR_LIT:value>' ) : <EOL> self . value_str = self . _value_str <EOL> if self . tag_name == '<STR_LIT>' : <EOL> return '<STR_LIT>' % ( self . __dict__ ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . __dict__ ) <EOL> else : <EOL> return '<STR_LIT>' % ( self . __dict__ ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . tiff , self . offset ) <EOL> def StripOffsets_hook ( ifdentry ) : <EOL> if ifdentry . tag_name == '<STR_LIT>' : <EOL> ifd = ifdentry . ifd <EOL> counts = ifd . get ( '<STR_LIT>' ) <EOL> if ifdentry . offset is not None : <EOL> for i , ( count , offset ) in enumerate ( zip ( counts . value , ifdentry . value ) ) : <EOL> ifdentry . memory_usage . append ( ( offset , offset + count , '<STR_LIT>' % ( i ) ) ) <EOL> else : <EOL> offset = ifdentry . value <EOL> ifdentry . memory_usage . append ( ( offset , offset + counts . value , '<STR_LIT>' ) ) <EOL> IFDEntry_finalize_hooks . append ( StripOffsets_hook ) <EOL> lsm . register ( locals ( ) ) </s>
