<s> import cpa . helpmenu <EOL> from dbconnect import * <EOL> from datamodel import DataModel <EOL> from imagecontrolpanel import * <EOL> from imagepanel import ImagePanel <EOL> from properties import Properties <EOL> import imagetools <EOL> import cPickle <EOL> import logging <EOL> import numpy as np <EOL> import wx <EOL> p = Properties . getInstance ( ) <EOL> db = DBConnect . getInstance ( ) <EOL> REQUIRED_PROPERTIES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:object_name>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> CL_NUMBERED = '<STR_LIT>' <EOL> CL_COLORED = '<STR_LIT>' <EOL> ID_SELECT_ALL = wx . NewId ( ) <EOL> ID_DESELECT_ALL = wx . NewId ( ) <EOL> def get_classifier_window ( ) : <EOL> from classifier import ID_CLASSIFIER <EOL> win = wx . FindWindowById ( ID_CLASSIFIER ) <EOL> if win : <EOL> return win <EOL> wins = [ x for x in wx . GetTopLevelWindows ( ) if x . Name == '<STR_LIT>' ] <EOL> if wins : <EOL> return wins [ <NUM_LIT:0> ] <EOL> else : <EOL> return None <EOL> def rescale_image_coord_to_display ( x , y ) : <EOL> '''<STR_LIT>''' <EOL> if not p . rescale_object_coords : <EOL> return x , y <EOL> x = x * p . image_rescale [ <NUM_LIT:0> ] / p . image_rescale_from [ <NUM_LIT:0> ] <EOL> y = y * p . image_rescale [ <NUM_LIT:1> ] / p . image_rescale_from [ <NUM_LIT:1> ] <EOL> return x , y <EOL> def rescale_display_coord_to_image ( x , y ) : <EOL> '''<STR_LIT>''' <EOL> if not p . rescale_object_coords : <EOL> return x , y <EOL> x = x * p . image_rescale_from [ <NUM_LIT:0> ] / p . image_rescale [ <NUM_LIT:0> ] <EOL> y = y * p . image_rescale_from [ <NUM_LIT:1> ] / p . image_rescale [ <NUM_LIT:1> ] <EOL> return x , y <EOL> class ImageViewerPanel ( ImagePanel ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imgs , chMap , img_key , parent , scale = <NUM_LIT:1.0> , brightness = <NUM_LIT:1.0> , contrast = None ) : <EOL> super ( ImageViewerPanel , self ) . __init__ ( imgs , chMap , parent , scale , brightness , contrast = contrast ) <EOL> self . selectedPoints = [ ] <EOL> self . classes = { } <EOL> self . classVisible = { } <EOL> self . class_rep = CL_COLORED <EOL> self . img_key = img_key <EOL> self . show_object_numbers = False <EOL> def OnPaint ( self , evt ) : <EOL> dc = super ( ImageViewerPanel , self ) . OnPaint ( evt ) <EOL> font = self . GetFont ( ) <EOL> font . SetPixelSize ( ( <NUM_LIT:6> , <NUM_LIT:12> ) ) <EOL> dc . SetFont ( font ) <EOL> dc . SetTextForeground ( '<STR_LIT>' ) <EOL> if self . show_object_numbers and p . object_table : <EOL> dc . SetLogicalFunction ( wx . XOR ) <EOL> dc . BeginDrawing ( ) <EOL> for i , ( x , y ) in enumerate ( self . ob_coords ) : <EOL> x = x * self . scale - <NUM_LIT:6> * ( len ( '<STR_LIT:%s>' % i ) - <NUM_LIT:1> ) <EOL> y = y * self . scale - <NUM_LIT:6> <EOL> dc . DrawText ( '<STR_LIT:%s>' % ( i + <NUM_LIT:1> ) , x , y ) <EOL> dc . EndDrawing ( ) <EOL> if self . classes : <EOL> for ( name , cl ) , clnum , color in zip ( self . classes . items ( ) , self . class_nums , self . colors ) : <EOL> if self . classVisible [ name ] : <EOL> dc . BeginDrawing ( ) <EOL> for ( x , y ) in cl : <EOL> if self . class_rep == CL_NUMBERED : <EOL> dc . SetLogicalFunction ( wx . XOR ) <EOL> x = x * self . scale - <NUM_LIT:3> <EOL> y = y * self . scale - <NUM_LIT:6> <EOL> dc . DrawText ( clnum , x , y ) <EOL> else : <EOL> w = h = <NUM_LIT:4> <EOL> x = x * self . scale - w / <NUM_LIT:2> <EOL> y = y * self . scale - h / <NUM_LIT:2> <EOL> dc . SetPen ( wx . Pen ( color , <NUM_LIT:1> ) ) <EOL> dc . SetBrush ( wx . Brush ( color , style = wx . TRANSPARENT ) ) <EOL> dc . DrawRectangle ( x , y , w , h ) <EOL> dc . DrawRectangle ( x - <NUM_LIT:1> , y - <NUM_LIT:1> , <NUM_LIT:6> , <NUM_LIT:6> ) <EOL> dc . EndDrawing ( ) <EOL> dc . SetLogicalFunction ( wx . XOR ) <EOL> dc . SetPen ( wx . Pen ( "<STR_LIT>" , <NUM_LIT:1> ) ) <EOL> dc . SetBrush ( wx . Brush ( "<STR_LIT>" , style = wx . TRANSPARENT ) ) <EOL> for ( x , y ) in self . selectedPoints : <EOL> w = h = <NUM_LIT:6> <EOL> x = x * self . scale - w / <NUM_LIT:2> <EOL> y = y * self . scale - h / <NUM_LIT:2> <EOL> dc . BeginDrawing ( ) <EOL> dc . DrawRectangle ( x , y , w , h ) <EOL> dc . EndDrawing ( ) <EOL> return dc <EOL> def SetSelectedPoints ( self , posns ) : <EOL> self . selectedPoints = posns <EOL> self . Refresh ( ) <EOL> def SelectPoint ( self , pos ) : <EOL> self . selectedPoints += [ pos ] <EOL> self . Refresh ( ) <EOL> def DeselectPoint ( self , pos ) : <EOL> self . selectedPoints . remove ( pos ) <EOL> self . Refresh ( ) <EOL> def TogglePointSelection ( self , pos ) : <EOL> if pos in self . selectedPoints : <EOL> self . DeselectPoint ( pos ) <EOL> else : <EOL> self . SelectPoint ( pos ) <EOL> def DeselectAll ( self ) : <EOL> self . selectedPoints = [ ] <EOL> self . Refresh ( ) <EOL> def SetClassPoints ( self , classes ) : <EOL> if len ( classes ) == <NUM_LIT:0> : <EOL> logging . warn ( '<STR_LIT>' ) <EOL> return <EOL> from matplotlib . pyplot import cm <EOL> self . classes = classes <EOL> vals = np . arange ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1.> / len ( classes ) ) <EOL> vals += ( <NUM_LIT:1.0> - vals [ - <NUM_LIT:1> ] ) / <NUM_LIT:2> <EOL> self . colors = [ np . array ( cm . jet ( val ) ) * <NUM_LIT:255> for val in vals ] <EOL> self . class_nums = [ str ( i + <NUM_LIT:1> ) for i , _ in enumerate ( classes ) ] <EOL> self . classVisible = { } <EOL> for className in classes . keys ( ) : <EOL> self . classVisible [ className ] = True <EOL> self . Refresh ( ) <EOL> def ToggleObjectNumbers ( self ) : <EOL> self . show_object_numbers = not self . show_object_numbers <EOL> if self . show_object_numbers : <EOL> self . ob_coords = db . GetAllObjectCoordsFromImage ( self . img_key ) <EOL> self . Refresh ( ) <EOL> def ToggleClassRepresentation ( self ) : <EOL> if self . class_rep == CL_NUMBERED : <EOL> self . class_rep = CL_COLORED <EOL> else : <EOL> self . class_rep = CL_NUMBERED <EOL> self . Refresh ( ) <EOL> def ToggleClass ( self , className , show ) : <EOL> self . classVisible [ className ] = show <EOL> self . Refresh ( ) <EOL> class ImageViewer ( wx . Frame ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , imgs = None , chMap = None , img_key = None , parent = None , title = '<STR_LIT>' , <EOL> classifier = None , brightness = <NUM_LIT:1.0> , scale = <NUM_LIT:1.0> , contrast = None , <EOL> classCoords = None ) : <EOL> '''<STR_LIT>''' <EOL> wx . Frame . __init__ ( self , parent , - <NUM_LIT:1> , title ) <EOL> self . SetName ( '<STR_LIT>' ) <EOL> self . SetBackgroundColour ( "<STR_LIT>" ) <EOL> self . img_key = img_key <EOL> self . classifier = parent <EOL> self . sw = wx . ScrolledWindow ( self ) <EOL> self . selection = [ ] <EOL> self . maxSize = tuple ( [ xy - <NUM_LIT:50> for xy in wx . DisplaySize ( ) ] ) <EOL> self . defaultFile = '<STR_LIT>' <EOL> self . defaultPath = '<STR_LIT>' <EOL> self . imagePanel = None <EOL> self . cp = None <EOL> self . controls = None <EOL> self . first_layout = True <EOL> self . inspect_release = True <EOL> if chMap is None : <EOL> try : <EOL> chMap = p . image_channel_colors <EOL> except : <EOL> pass <EOL> self . SetSizer ( wx . BoxSizer ( wx . VERTICAL ) ) <EOL> self . CreateMenus ( ) <EOL> self . CreatePopupMenu ( ) <EOL> if imgs and chMap : <EOL> self . SetImage ( imgs , chMap , brightness , scale , contrast ) <EOL> else : <EOL> self . OnOpenImage ( ) <EOL> self . DoLayout ( ) <EOL> self . Center ( ) <EOL> if classCoords is not None : <EOL> self . SetClasses ( classCoords ) <EOL> def AutoTitle ( self ) : <EOL> if p . plate_id and p . well_id : <EOL> plate , well = db . execute ( '<STR_LIT>' % ( p . plate_id , p . well_id , p . image_table , GetWhereClauseForImages ( [ self . img_key ] ) ) ) [ <NUM_LIT:0> ] <EOL> title = '<STR_LIT>' % ( p . plate_id , plate , p . well_id , well , str ( self . img_key ) ) <EOL> else : <EOL> title = '<STR_LIT>' % ( str ( self . img_key ) ) <EOL> self . SetTitle ( title ) <EOL> def CreatePopupMenu ( self ) : <EOL> self . popupMenu = wx . Menu ( ) <EOL> self . sel_all = wx . MenuItem ( self . popupMenu , ID_SELECT_ALL , '<STR_LIT>' ) <EOL> self . deselect = wx . MenuItem ( self . popupMenu , ID_DESELECT_ALL , '<STR_LIT>' ) <EOL> self . popupMenu . AppendItem ( self . sel_all ) <EOL> self . popupMenu . AppendItem ( self . deselect ) <EOL> accelerator_table = wx . AcceleratorTable ( [ ( wx . ACCEL_CMD , ord ( '<STR_LIT:A>' ) , ID_SELECT_ALL ) , <EOL> ( wx . ACCEL_CMD , ord ( '<STR_LIT:D>' ) , ID_DESELECT_ALL ) , ] ) <EOL> self . SetAcceleratorTable ( accelerator_table ) <EOL> def SetImage ( self , imgs , chMap = None , brightness = <NUM_LIT:1> , scale = <NUM_LIT:1> , contrast = None ) : <EOL> self . AutoTitle ( ) <EOL> self . chMap = chMap or p . image_channel_colors <EOL> self . toggleChMap = self . chMap [ : ] <EOL> if self . imagePanel : <EOL> self . imagePanel . Destroy ( ) <EOL> self . imagePanel = ImageViewerPanel ( imgs , self . chMap , self . img_key , <EOL> self . sw , brightness = brightness , <EOL> scale = scale , contrast = contrast ) <EOL> self . imagePanel . Bind ( wx . EVT_LEFT_DOWN , self . OnLeftDown ) <EOL> self . imagePanel . Bind ( wx . EVT_SIZE , self . OnResizeImagePanel ) <EOL> self . imagePanel . Bind ( wx . EVT_RIGHT_DOWN , self . OnRightDown ) <EOL> def CreateMenus ( self ) : <EOL> self . SetMenuBar ( wx . MenuBar ( ) ) <EOL> self . fileMenu = wx . Menu ( ) <EOL> self . openImageMenuItem = self . fileMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' ) <EOL> self . saveImageMenuItem = self . fileMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' ) <EOL> self . fileMenu . AppendSeparator ( ) <EOL> self . exitMenuItem = self . fileMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' ) <EOL> self . GetMenuBar ( ) . Append ( self . fileMenu , '<STR_LIT>' ) <EOL> self . classifyMenu = wx . Menu ( ) <EOL> self . classifyMenuItem = self . classifyMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' ) <EOL> self . GetMenuBar ( ) . Append ( self . classifyMenu , '<STR_LIT>' ) <EOL> self . viewMenu = wx . Menu ( ) <EOL> self . objectNumberMenuItem = self . viewMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' % p . object_name [ <NUM_LIT:0> ] ) <EOL> self . objectNumberMenuItem . Enable ( p . object_table is not None ) <EOL> self . classViewMenuItem = self . viewMenu . Append ( - <NUM_LIT:1> , text = '<STR_LIT>' % p . object_name [ <NUM_LIT:0> ] ) <EOL> self . classViewMenuItem . Enable ( p . object_table is not None ) <EOL> self . GetMenuBar ( ) . Append ( self . viewMenu , '<STR_LIT>' ) <EOL> self . GetMenuBar ( ) . Append ( cpa . helpmenu . make_help_menu ( self ) , '<STR_LIT>' ) <EOL> def CreateChannelMenus ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> menus = set ( [ items [ <NUM_LIT:2> ] . Menu for items in self . chMapById . values ( ) ] ) <EOL> for menu in menus : <EOL> for i , mbmenu in enumerate ( self . MenuBar . Menus ) : <EOL> if mbmenu [ <NUM_LIT:0> ] == menu : <EOL> self . MenuBar . Remove ( i ) <EOL> for menu in menus : <EOL> menu . Destroy ( ) <EOL> if '<STR_LIT>' in self . __dict__ : <EOL> self . MenuBar . Remove ( self . MenuBar . FindMenu ( '<STR_LIT>' ) ) <EOL> self . imagesMenu . Destroy ( ) <EOL> except : <EOL> pass <EOL> self . imagesMenu = wx . Menu ( ) <EOL> chIndex = <NUM_LIT:0> <EOL> imIndex = <NUM_LIT:0> <EOL> self . chMapById = { } <EOL> self . imMapById = { } <EOL> channel_names = [ ] <EOL> startIndex = <NUM_LIT:0> <EOL> for i , chans in enumerate ( p . channels_per_image ) : <EOL> chans = int ( chans ) <EOL> name = p . image_names [ i ] <EOL> if chans == <NUM_LIT:1> : <EOL> channel_names += [ name ] <EOL> elif chans == <NUM_LIT:3> : <EOL> channel_names += [ '<STR_LIT>' % ( name , x ) for x in '<STR_LIT>' ] <EOL> elif chans == <NUM_LIT:4> : <EOL> channel_names += [ '<STR_LIT>' % ( name , x ) for x in '<STR_LIT>' ] <EOL> else : <EOL> channel_names += [ '<STR_LIT>' % ( name , x + <NUM_LIT:1> ) for x in range ( chans ) ] <EOL> zippedChNamesChMap = zip ( channel_names , self . chMap ) <EOL> for i , chans in enumerate ( p . image_names ) : <EOL> channelIds = [ ] <EOL> for j in range ( <NUM_LIT:0> , int ( p . channels_per_image [ i ] ) ) : <EOL> ( channel , setColor ) = zippedChNamesChMap [ chIndex ] <EOL> channel_menu = wx . Menu ( ) <EOL> for color in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:None>' ] : <EOL> id = wx . NewId ( ) <EOL> item = channel_menu . AppendRadioItem ( id , color ) <EOL> self . chMapById [ id ] = ( chIndex , color , item , channel_menu ) <EOL> if color . lower ( ) == setColor . lower ( ) : <EOL> item . Check ( ) <EOL> self . Bind ( wx . EVT_MENU , self . OnMapChannels , item ) <EOL> if ( ( int ( p . channels_per_image [ i ] ) == <NUM_LIT:1> and color == '<STR_LIT>' ) or <EOL> ( int ( p . channels_per_image [ i ] ) > <NUM_LIT:1> and j == <NUM_LIT:0> and color == '<STR_LIT>' ) or <EOL> ( int ( p . channels_per_image [ i ] ) > <NUM_LIT:1> and j == <NUM_LIT:2> and color == '<STR_LIT>' ) or <EOL> ( int ( p . channels_per_image [ i ] ) > <NUM_LIT:1> and j == <NUM_LIT:1> and color == '<STR_LIT>' ) ) : <EOL> channelIds = channelIds + [ id ] <EOL> self . GetMenuBar ( ) . Append ( channel_menu , channel ) <EOL> chIndex += <NUM_LIT:1> <EOL> id = wx . NewId ( ) <EOL> item = self . imagesMenu . AppendRadioItem ( id , p . image_names [ i ] ) <EOL> self . imMapById [ id ] = ( int ( p . channels_per_image [ i ] ) , item , startIndex , channelIds ) <EOL> self . Bind ( wx . EVT_MENU , self . OnFetchImage , item ) <EOL> startIndex += int ( p . channels_per_image [ i ] ) <EOL> id = wx . NewId ( ) <EOL> item = self . imagesMenu . AppendRadioItem ( id , '<STR_LIT:None>' ) <EOL> self . Bind ( wx . EVT_MENU , self . OnFetchImage , item ) <EOL> item . Check ( ) <EOL> self . GetMenuBar ( ) . Append ( self . imagesMenu , '<STR_LIT>' ) <EOL> def OnFetchImage ( self , evt = None ) : <EOL> for ids in self . chMapById . keys ( ) : <EOL> ( chIndex , color , item , channel_menu ) = self . chMapById [ ids ] <EOL> if ( color . lower ( ) == '<STR_LIT:none>' ) : <EOL> item . Check ( ) <EOL> for ids in self . imMapById . keys ( ) : <EOL> ( cpi , itm , si , channelIds ) = self . imMapById [ ids ] <EOL> if cpi == <NUM_LIT:3> : <EOL> self . chMap [ si ] = '<STR_LIT:none>' <EOL> self . chMap [ si + <NUM_LIT:1> ] = '<STR_LIT:none>' <EOL> self . chMap [ si + <NUM_LIT:2> ] = '<STR_LIT:none>' <EOL> self . toggleChMap [ si ] = '<STR_LIT:none>' <EOL> self . toggleChMap [ si + <NUM_LIT:1> ] = '<STR_LIT:none>' <EOL> self . toggleChMap [ si + <NUM_LIT:2> ] = '<STR_LIT:none>' <EOL> else : <EOL> self . chMap [ si ] = '<STR_LIT:none>' <EOL> self . toggleChMap [ si ] = '<STR_LIT:none>' <EOL> if evt . GetId ( ) in self . imMapById . keys ( ) : <EOL> ( chanPerIm , item , startIndex , channelIds ) = self . imMapById [ evt . GetId ( ) ] <EOL> if chanPerIm == <NUM_LIT:1> : <EOL> self . chMap [ startIndex ] = '<STR_LIT>' <EOL> self . toggleChMap [ startIndex ] = '<STR_LIT>' <EOL> ( chIndex , color , item , channel_menu ) = self . chMapById [ channelIds [ <NUM_LIT:0> ] ] <EOL> item . Check ( ) <EOL> else : <EOL> RGB = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] + [ '<STR_LIT:none>' ] * chanPerIm <EOL> for i in range ( chanPerIm ) : <EOL> self . chMap [ startIndex + i ] = RGB [ i ] <EOL> self . toggleChMap [ startIndex + i ] = RGB [ i ] <EOL> ( chIndex , color , item , channel_menu ) = self . chMapById [ channelIds [ i ] ] <EOL> item . Check ( ) <EOL> self . MapChannels ( self . chMap ) <EOL> def DoLayout ( self ) : <EOL> if self . imagePanel : <EOL> if not self . cp : <EOL> self . cp = wx . CollapsiblePane ( self , label = '<STR_LIT>' , style = wx . CP_DEFAULT_STYLE | wx . CP_NO_TLW_RESIZE ) <EOL> self . SetBackgroundColour ( '<STR_LIT>' ) <EOL> self . controls = ImageControlPanel ( self . cp . GetPane ( ) , self . imagePanel , <EOL> brightness = self . imagePanel . brightness , <EOL> scale = self . imagePanel . scale , <EOL> contrast = self . imagePanel . contrast ) <EOL> self . Bind ( wx . EVT_COLLAPSIBLEPANE_CHANGED , self . OnPaneChanged , self . cp ) <EOL> else : <EOL> self . controls . SetListener ( self . imagePanel ) <EOL> self . Sizer . Clear ( ) <EOL> self . Sizer . Add ( self . sw , proportion = <NUM_LIT:1> , flag = wx . EXPAND ) <EOL> self . Sizer . Add ( self . cp , <NUM_LIT:0> , wx . RIGHT | wx . LEFT | wx . EXPAND , <NUM_LIT> ) <EOL> w , h = self . imagePanel . GetSize ( ) <EOL> if self . first_layout : <EOL> self . SetClientSize ( ( min ( self . maxSize [ <NUM_LIT:0> ] , w * self . imagePanel . scale ) , <EOL> min ( self . maxSize [ <NUM_LIT:1> ] , h * self . imagePanel . scale + <NUM_LIT> ) ) ) <EOL> self . Center ( ) <EOL> self . first_layout = False <EOL> self . sw . SetScrollbars ( <NUM_LIT:1> , <NUM_LIT:1> , w * self . imagePanel . scale , h * self . imagePanel . scale ) <EOL> self . CreateChannelMenus ( ) <EOL> self . Bind ( wx . EVT_KEY_DOWN , self . HoldKey ) <EOL> self . sw . Bind ( wx . EVT_KEY_DOWN , self . HoldKey ) <EOL> self . cp . Bind ( wx . EVT_KEY_DOWN , self . HoldKey ) <EOL> self . imagePanel . Bind ( wx . EVT_KEY_DOWN , self . HoldKey ) <EOL> self . Bind ( wx . EVT_KEY_UP , self . OnKey ) <EOL> self . sw . Bind ( wx . EVT_KEY_UP , self . OnKey ) <EOL> self . cp . Bind ( wx . EVT_KEY_UP , self . OnKey ) <EOL> self . imagePanel . Bind ( wx . EVT_KEY_UP , self . OnKey ) <EOL> self . Bind ( wx . EVT_MENU , lambda ( e ) : self . SelectAll ( ) , self . sel_all ) <EOL> self . Bind ( wx . EVT_MENU , lambda ( e ) : self . DeselectAll ( ) , self . deselect ) <EOL> self . fileMenu . Bind ( wx . EVT_MENU_OPEN , self . OnOpenFileMenu ) <EOL> self . classifyMenu . Bind ( wx . EVT_MENU_OPEN , self . OnOpenClassifyMenu ) <EOL> self . viewMenu . Bind ( wx . EVT_MENU_OPEN , self . OnOpenViewMenu ) <EOL> self . Bind ( wx . EVT_MENU , self . OnOpenImage , self . openImageMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnSaveImage , self . saveImageMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnSaveImage , self . saveImageMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnOpenImage , self . openImageMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnChangeClassRepresentation , self . classViewMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnShowObjectNumbers , self . objectNumberMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , self . OnClassifyImage , self . classifyMenuItem ) <EOL> self . Bind ( wx . EVT_MENU , lambda evt : self . Close ( ) , self . exitMenuItem ) <EOL> def OnClassifyImage ( self , evt = None ) : <EOL> logging . info ( '<STR_LIT>' % str ( self . img_key ) ) <EOL> classifier = get_classifier_window ( ) <EOL> if classifier is None : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return <EOL> classHits = classifier . ScoreImage ( self . img_key ) <EOL> classCoords = { } <EOL> for className , obKeys in classHits . items ( ) : <EOL> classCoords [ className ] = [ db . GetObjectCoords ( key ) for key in obKeys ] <EOL> self . SetClasses ( classCoords ) <EOL> def OnPaneChanged ( self , evt = None ) : <EOL> self . Layout ( ) <EOL> if self . cp . IsExpanded ( ) : <EOL> self . cp . SetLabel ( '<STR_LIT>' ) <EOL> else : <EOL> self . cp . SetLabel ( '<STR_LIT>' ) <EOL> def OnMapChannels ( self , evt ) : <EOL> if evt . GetId ( ) in self . chMapById . keys ( ) : <EOL> ( chIdx , color , _ , _ ) = self . chMapById [ evt . GetId ( ) ] <EOL> self . chMap [ chIdx ] = color <EOL> if color . lower ( ) != '<STR_LIT:none>' : <EOL> self . toggleChMap [ chIdx ] = color <EOL> self . MapChannels ( self . chMap ) <EOL> def MapChannels ( self , chMap ) : <EOL> self . chMap = chMap <EOL> self . imagePanel . MapChannels ( chMap ) <EOL> def HoldKey ( self , evt ) : <EOL> keycode = evt . GetKeyCode ( ) <EOL> chIdx = keycode - <NUM_LIT> <EOL> if evt . CmdDown ( ) or evt . ControlDown ( ) : <EOL> if keycode == ord ( '<STR_LIT:F>' ) : <EOL> if ( self . inspect_release ) : <EOL> self . imagePanel . tmp_brightness = self . imagePanel . brightness <EOL> self . inspect_release = False <EOL> self . imagePanel . SetBrightness ( <NUM_LIT:0> ) <EOL> else : <EOL> evt . Skip ( ) <EOL> def OnKey ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> keycode = evt . GetKeyCode ( ) <EOL> chIdx = keycode - <NUM_LIT> <EOL> if evt . CmdDown ( ) or evt . ControlDown ( ) : <EOL> if keycode == ord ( '<STR_LIT:A>' ) : <EOL> self . SelectAll ( ) <EOL> elif keycode == ord ( '<STR_LIT:D>' ) : <EOL> self . DeselectAll ( ) <EOL> elif keycode == ord ( '<STR_LIT>' ) : <EOL> self . imagePanel . SetContrastMode ( '<STR_LIT:None>' ) <EOL> self . controls . SetContrastMode ( '<STR_LIT:None>' ) <EOL> elif keycode == ord ( '<STR_LIT>' ) : <EOL> self . imagePanel . SetContrastMode ( '<STR_LIT>' ) <EOL> self . controls . SetContrastMode ( '<STR_LIT>' ) <EOL> elif keycode == ord ( '<STR_LIT:L>' ) : <EOL> self . imagePanel . SetContrastMode ( '<STR_LIT>' ) <EOL> self . controls . SetContrastMode ( '<STR_LIT>' ) <EOL> elif keycode == ord ( '<STR_LIT:F>' ) : <EOL> brightness = self . imagePanel . tmp_brightness <EOL> self . imagePanel . SetBrightness ( brightness ) <EOL> self . inspect_release = True <EOL> elif len ( self . chMap ) > chIdx >= <NUM_LIT:0> : <EOL> self . ToggleChannel ( chIdx ) <EOL> else : <EOL> evt . Skip ( ) <EOL> else : <EOL> if keycode == ord ( '<STR_LIT:U+0020>' ) : <EOL> self . cp . Collapse ( not self . cp . IsCollapsed ( ) ) <EOL> self . OnPaneChanged ( ) <EOL> else : <EOL> evt . Skip ( ) <EOL> def OnResizeImagePanel ( self , evt ) : <EOL> self . sw . SetVirtualSize ( evt . GetSize ( ) ) <EOL> def ToggleChannel ( self , chIdx ) : <EOL> if self . chMap [ chIdx ] == '<STR_LIT:None>' : <EOL> for ( idx , color , item , menu ) in self . chMapById . values ( ) : <EOL> if idx == chIdx and color . lower ( ) == self . toggleChMap [ chIdx ] . lower ( ) : <EOL> item . Check ( ) <EOL> self . chMap [ chIdx ] = self . toggleChMap [ chIdx ] <EOL> self . MapChannels ( self . chMap ) <EOL> else : <EOL> for ( idx , color , item , menu ) in self . chMapById . values ( ) : <EOL> if idx == chIdx and color . lower ( ) == '<STR_LIT:none>' : <EOL> item . Check ( ) <EOL> self . chMap [ chIdx ] = '<STR_LIT:None>' <EOL> self . MapChannels ( self . chMap ) <EOL> def SelectAll ( self ) : <EOL> if p . object_table : <EOL> coords = db . GetAllObjectCoordsFromImage ( self . img_key ) <EOL> self . selection = db . GetObjectsFromImage ( self . img_key ) <EOL> if p . rescale_object_coords : <EOL> coords = [ rescale_image_coord_to_display ( x , y ) for ( x , y ) in coords ] <EOL> self . imagePanel . SetSelectedPoints ( coords ) <EOL> def DeselectAll ( self ) : <EOL> self . selection = [ ] <EOL> self . imagePanel . DeselectAll ( ) <EOL> def SelectObject ( self , obkey ) : <EOL> coord = db . GetObjectCoords ( obkey ) <EOL> if p . rescale_object_coords : <EOL> coord = rescale_image_coord_to_display ( coord [ <NUM_LIT:0> ] , coord [ <NUM_LIT:1> ] ) <EOL> self . selection += [ coord ] <EOL> self . imagePanel . SetSelectedPoints ( [ coord ] ) <EOL> def SetClasses ( self , classCoords ) : <EOL> self . classViewMenuItem . Enable ( ) <EOL> self . imagePanel . SetClassPoints ( classCoords ) <EOL> self . controls . SetClassPoints ( classCoords ) <EOL> self . Refresh ( ) <EOL> self . Layout ( ) <EOL> def OnLeftDown ( self , evt ) : <EOL> if self . img_key and p . object_table : <EOL> x = evt . GetPosition ( ) . x / self . imagePanel . scale <EOL> y = evt . GetPosition ( ) . y / self . imagePanel . scale <EOL> if p . rescale_object_coords : <EOL> x , y = rescale_display_coord_to_image ( x , y ) <EOL> obKey = db . GetObjectNear ( self . img_key , x , y ) <EOL> if not obKey : return <EOL> if not evt . ShiftDown ( ) : <EOL> self . selection = [ obKey ] <EOL> self . imagePanel . DeselectAll ( ) <EOL> else : <EOL> if obKey not in self . selection : <EOL> self . selection += [ obKey ] <EOL> else : <EOL> self . selection . remove ( obKey ) <EOL> ( x , y ) = db . GetObjectCoords ( obKey ) <EOL> if p . rescale_object_coords : <EOL> x , y = rescale_image_coord_to_display ( x , y ) <EOL> self . imagePanel . TogglePointSelection ( ( x , y ) ) <EOL> if self . selection : <EOL> source = wx . DropSource ( self ) <EOL> data_object = wx . CustomDataObject ( "<STR_LIT>" ) <EOL> data_object . SetData ( cPickle . dumps ( ( self . GetId ( ) , self . selection ) ) ) <EOL> source . SetData ( data_object ) <EOL> result = source . DoDragDrop ( flags = wx . Drag_DefaultMove ) <EOL> if result is <NUM_LIT:0> : <EOL> pass <EOL> def OnRightDown ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> if p . object_table : <EOL> self . PopupMenu ( self . popupMenu , evt . GetPosition ( ) ) <EOL> def OnOpenImage ( self , evt = None ) : <EOL> tblNum = None <EOL> if p . table_id : <EOL> dlg = wx . TextEntryDialog ( self , p . table_id + '<STR_LIT::>' , '<STR_LIT>' + p . table_id ) <EOL> dlg . SetValue ( '<STR_LIT:0>' ) <EOL> if dlg . ShowModal ( ) == wx . ID_OK : <EOL> try : <EOL> tblNum = int ( dlg . GetValue ( ) ) <EOL> except ValueError : <EOL> errdlg = wx . MessageDialog ( self , '<STR_LIT>' % ( p . table_id ) , "<STR_LIT>" , wx . OK | wx . ICON_EXCLAMATION ) <EOL> errdlg . ShowModal ( ) <EOL> return <EOL> dlg . Destroy ( ) <EOL> else : <EOL> dlg . Destroy ( ) <EOL> return <EOL> dlg = wx . TextEntryDialog ( self , p . image_id + '<STR_LIT::>' , '<STR_LIT>' + p . image_id ) <EOL> dlg . SetValue ( '<STR_LIT>' ) <EOL> if dlg . ShowModal ( ) == wx . ID_OK : <EOL> try : <EOL> imgNum = int ( dlg . GetValue ( ) ) <EOL> except ValueError : <EOL> errdlg = wx . MessageDialog ( self , '<STR_LIT>' % ( p . image_id ) , "<STR_LIT>" , wx . OK | wx . ICON_EXCLAMATION ) <EOL> errdlg . ShowModal ( ) <EOL> return <EOL> dlg . Destroy ( ) <EOL> else : <EOL> dlg . Destroy ( ) <EOL> return <EOL> if p . table_id : <EOL> imkey = ( tblNum , imgNum ) <EOL> else : <EOL> imkey = ( imgNum , ) <EOL> dm = DataModel . getInstance ( ) <EOL> if imkey not in dm . GetAllImageKeys ( ) : <EOL> errdlg = wx . MessageDialog ( self , '<STR_LIT>' , "<STR_LIT>" , wx . OK | wx . ICON_EXCLAMATION ) <EOL> errdlg . ShowModal ( ) <EOL> self . Destroy ( ) <EOL> else : <EOL> self . img_key = imkey <EOL> self . SetImage ( imagetools . FetchImage ( imkey ) , p . image_channel_colors ) <EOL> self . DoLayout ( ) <EOL> def OnSaveImage ( self , evt ) : <EOL> import os <EOL> saveDialog = wx . FileDialog ( self , message = "<STR_LIT>" , <EOL> defaultDir = self . defaultPath , defaultFile = self . defaultFile , <EOL> wildcard = '<STR_LIT>' , <EOL> style = wx . SAVE | wx . FD_OVERWRITE_PROMPT ) <EOL> if saveDialog . ShowModal ( ) == wx . ID_OK : <EOL> filename = str ( saveDialog . GetPath ( ) ) <EOL> self . defaultPath , self . defaultFile = os . path . split ( filename ) <EOL> format = os . path . splitext ( filename ) [ - <NUM_LIT:1> ] <EOL> saveDialog . Destroy ( ) <EOL> if not format . upper ( ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> errdlg = wx . MessageDialog ( self , '<STR_LIT>' % ( format ) , <EOL> "<STR_LIT>" , wx . OK | wx . ICON_EXCLAMATION ) <EOL> if errdlg . ShowModal ( ) == wx . ID_OK : <EOL> return self . OnSaveImage ( evt ) <EOL> if format . upper ( ) == '<STR_LIT>' : <EOL> format = '<STR_LIT>' <EOL> imagetools . SaveBitmap ( self . imagePanel . bitmap , filename , format . upper ( ) [ <NUM_LIT:1> : ] ) <EOL> def OnChangeClassRepresentation ( self , evt ) : <EOL> if self . classViewMenuItem . Text . endswith ( '<STR_LIT>' ) : <EOL> self . classViewMenuItem . Text = '<STR_LIT>' % p . object_name [ <NUM_LIT:0> ] <EOL> else : <EOL> self . classViewMenuItem . Text = '<STR_LIT>' % p . object_name [ <NUM_LIT:0> ] <EOL> self . imagePanel . ToggleClassRepresentation ( ) <EOL> def OnShowObjectNumbers ( self , evt ) : <EOL> if self . objectNumberMenuItem . Text . startswith ( '<STR_LIT>' ) : <EOL> self . objectNumberMenuItem . Text = '<STR_LIT>' % ( p . object_name [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . objectNumberMenuItem . Text = '<STR_LIT>' % ( p . object_name [ <NUM_LIT:0> ] ) <EOL> self . imagePanel . ToggleObjectNumbers ( ) <EOL> def OnOpenFileMenu ( self , evt = None ) : <EOL> if self . imagePanel : <EOL> self . saveImageMenuItem . Enable ( ) <EOL> else : <EOL> self . saveImageMenuItem . Enable ( False ) <EOL> def OnOpenViewMenu ( self , evt = None ) : <EOL> if self . imagePanel and self . imagePanel . classes : <EOL> self . classViewMenuItem . Enable ( ) <EOL> else : <EOL> self . classViewMenuItem . Enable ( False ) <EOL> def OnOpenClassifyMenu ( self , evt = None ) : <EOL> classifier = get_classifier_window ( ) <EOL> if classifier and classifier . IsTrained ( ) : <EOL> self . classifyMenuItem . Enable ( ) <EOL> else : <EOL> self . classifyMenuItem . Enable ( False ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> logging . basicConfig ( ) <EOL> app = wx . PySimpleApp ( ) <EOL> from datamodel import DataModel <EOL> import imagetools <EOL> from imagereader import ImageReader <EOL> p = Properties . getInstance ( ) <EOL> p . image_channel_colors = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> p . object_name = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> p . image_names = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] <EOL> p . image_id = '<STR_LIT>' <EOL> p . channels_per_image = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> images = [ np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> np . ones ( ( <NUM_LIT:200> , <NUM_LIT:200> ) ) / <NUM_LIT> , <EOL> ] <EOL> pixels = [ ] <EOL> for channel in p . image_channel_colors : <EOL> pixels += [ imagetools . tile_images ( images ) ] <EOL> f = ImageViewer ( pixels ) <EOL> f . Show ( ) <EOL> app . MainLoop ( ) </s>
