<s> import wx <EOL> import numpy as np <EOL> import urllib2 <EOL> import urllib <EOL> import urlparse <EOL> import os . path <EOL> import logging <EOL> import bioformats <EOL> from properties import Properties <EOL> from errors import ClearException <EOL> p = Properties . getInstance ( ) <EOL> class ThrowingURLopener ( urllib . URLopener ) : <EOL> def http_error_default ( * args , ** kwargs ) : <EOL> return urllib . URLopener . http_error_default ( * args , ** kwargs ) <EOL> class ImageReader ( object ) : <EOL> def ReadImages ( self , fds ) : <EOL> '''<STR_LIT>''' <EOL> return self . read_images_via_bioformats ( fds ) <EOL> def _read_image_via_bioformats ( self , filename_or_url ) : <EOL> opener = ThrowingURLopener ( ) <EOL> if p . image_url_prepend : <EOL> parsed = urlparse . urlparse ( p . image_url_prepend + filename_or_url ) <EOL> if parsed . scheme : <EOL> try : <EOL> filename_or_url , ignored_headers = opener . retrieve ( parsed . geturl ( ) ) <EOL> except IOError , e : <EOL> if e . args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> status_code , message = e . args [ <NUM_LIT:1> : <NUM_LIT:3> ] <EOL> raise ClearException ( <EOL> '<STR_LIT>' % parsed . geturl ( ) , <EOL> '<STR_LIT>' % ( status_code , message ) ) <EOL> else : <EOL> raise <EOL> logging . info ( '<STR_LIT>' % filename_or_url ) <EOL> return bioformats . load_image ( filename_or_url ) <EOL> def _extract_channels ( self , filename_or_url , image , image_name , channels_per_image ) : <EOL> if image . ndim == <NUM_LIT:2> and channels_per_image != <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( <NUM_LIT:1> , <EOL> image_name , <EOL> filename_or_url , <EOL> channels_per_image ) ) <EOL> if image . ndim > <NUM_LIT:2> and image . shape [ <NUM_LIT:2> ] < channels_per_image : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( image . shape [ <NUM_LIT:2> ] , <EOL> image_name , <EOL> filename_or_url , <EOL> channels_per_image ) ) <EOL> if image . ndim > <NUM_LIT:2> and image . shape [ <NUM_LIT:2> ] > channels_per_image : <EOL> logging . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( image . shape [ <NUM_LIT:2> ] , <EOL> image_name , <EOL> filename_or_url , <EOL> channels_per_image ) ) <EOL> return [ image [ : , : , j ] <EOL> for j in range ( int ( channels_per_image ) ) ] <EOL> else : <EOL> if image . ndim == <NUM_LIT:2> : <EOL> return [ image ] <EOL> else : <EOL> return [ image [ : , : , j ] <EOL> for j in range ( image . shape [ <NUM_LIT:2> ] ) ] <EOL> def read_images_via_bioformats ( self , filenames_or_urls ) : <EOL> '''<STR_LIT>''' <EOL> channels = [ ] <EOL> for i , filename_or_url in enumerate ( filenames_or_urls ) : <EOL> image = self . _read_image_via_bioformats ( filename_or_url ) <EOL> channels += self . _extract_channels ( filename_or_url , image , <EOL> p . image_names [ i ] , <EOL> int ( p . channels_per_image [ i ] ) ) <EOL> from imagetools import check_image_shape_compatibility <EOL> check_image_shape_compatibility ( channels ) <EOL> if p . image_rescale : <EOL> from imagetools import rescale <EOL> for i in range ( len ( channels ) ) : <EOL> if channels [ i ] . shape != p . image_rescale : <EOL> channels [ i ] = rescale ( channels [ i ] , ( p . image_rescale [ <NUM_LIT:1> ] , p . image_rescale [ <NUM_LIT:0> ] ) ) <EOL> return channels <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import wx <EOL> from datamodel import DataModel <EOL> from dbconnect import DBConnect <EOL> from imageviewer import ImageViewer <EOL> import sys <EOL> app = wx . PySimpleApp ( ) <EOL> p = Properties . getInstance ( ) <EOL> dm = DataModel . getInstance ( ) <EOL> db = DBConnect . getInstance ( ) <EOL> ir = ImageReader ( ) <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> propsFile = sys . argv [ <NUM_LIT:1> ] <EOL> p . LoadFile ( propsFile ) <EOL> else : <EOL> if not p . show_load_dialog ( ) : <EOL> wx . GetApp ( ) . Exit ( ) <EOL> obkey = dm . GetRandomObject ( ) <EOL> fds = db . GetFullChannelPathsForImage ( obkey [ : - <NUM_LIT:1> ] ) <EOL> images = ir . ReadImages ( fds ) <EOL> ImageViewer ( images , img_key = obkey [ : - <NUM_LIT:1> ] ) . Show ( ) <EOL> app . MainLoop ( ) </s>
