<s> from dbconnect import DBConnect <EOL> import tilecollection <EOL> from imagetile import ImageTile <EOL> from imagetilesizer import ImageTileSizer <EOL> from imagecontrolpanel import ImageControlPanel <EOL> from properties import Properties <EOL> import imagetools <EOL> import cPickle <EOL> import wx <EOL> import logging <EOL> p = Properties . getInstance ( ) <EOL> db = DBConnect . getInstance ( ) <EOL> EVT_QUANTITY_CHANGED = wx . PyEventBinder ( wx . NewEventType ( ) , <NUM_LIT:1> ) <EOL> class CellMontageFrame ( wx . Frame ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , ** kwargs ) : <EOL> wx . Frame . __init__ ( self , parent , ** kwargs ) <EOL> self . sb = SortBin ( self ) <EOL> self . cp = wx . CollapsiblePane ( self , label = '<STR_LIT>' , style = wx . CP_DEFAULT_STYLE | wx . CP_NO_TLW_RESIZE ) <EOL> self . icp = ImageControlPanel ( self . cp . GetPane ( ) , self ) <EOL> self . Sizer = wx . BoxSizer ( wx . VERTICAL ) <EOL> self . Sizer . Add ( self . sb , <NUM_LIT:1> , wx . EXPAND ) <EOL> self . Sizer . Add ( self . cp , <NUM_LIT:0> , wx . EXPAND ) <EOL> self . cp . Bind ( wx . EVT_COLLAPSIBLEPANE_CHANGED , self . _on_control_pane_change ) <EOL> def _on_control_pane_change ( 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 add_objects ( self , obkeys ) : <EOL> self . sb . AddObjects ( obkeys ) <EOL> def SetBrightness ( self , brightness ) : <EOL> [ t . SetBrightness ( brightness ) for t in self . sb . tiles ] <EOL> def SetScale ( self , scale ) : <EOL> [ t . SetScale ( scale ) for t in self . sb . tiles ] <EOL> self . sb . UpdateSizer ( ) <EOL> def SetContrastMode ( self , mode ) : <EOL> [ t . SetContrastMode ( mode ) for t in self . sb . tiles ] <EOL> class SortBinDropTarget ( wx . DropTarget ) : <EOL> def __init__ ( self , bin ) : <EOL> wx . DropTarget . __init__ ( self ) <EOL> self . data = wx . CustomDataObject ( "<STR_LIT>" ) <EOL> self . SetDataObject ( self . data ) <EOL> self . bin = bin <EOL> def OnData ( self , x , y , dragres ) : <EOL> if not self . GetData ( ) : <EOL> return wx . DragNone <EOL> draginfo = self . data . GetData ( ) <EOL> srcID , obKeys = cPickle . loads ( draginfo ) <EOL> if not obKeys : <EOL> return wx . DragNone <EOL> return self . bin . ReceiveDrop ( srcID , obKeys ) <EOL> class SortBin ( wx . ScrolledWindow ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , chMap = None , label = '<STR_LIT>' , classifier = None , parentSizer = None ) : <EOL> wx . ScrolledWindow . __init__ ( self , parent ) <EOL> self . SetDropTarget ( SortBinDropTarget ( self ) ) <EOL> self . label = label <EOL> self . parentSizer = parentSizer <EOL> self . tiles = [ ] <EOL> self . classifier = classifier <EOL> self . trained = False <EOL> self . empty = True <EOL> self . tile_collection = None <EOL> if chMap : <EOL> self . chMap = chMap <EOL> else : <EOL> self . chMap = p . image_channel_colors <EOL> self . SetBackgroundColour ( '<STR_LIT>' ) <EOL> self . sizer = ImageTileSizer ( ) <EOL> self . SetSizer ( self . sizer ) <EOL> self . SetMinSize ( ( <NUM_LIT:50> , <NUM_LIT:50> ) ) <EOL> ( w , h ) = self . sizer . GetSize ( ) <EOL> self . SetScrollbars ( <NUM_LIT:20> , <NUM_LIT:20> , w / <NUM_LIT:20> , h / <NUM_LIT:20> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . EnableScrolling ( x_scrolling = False , y_scrolling = True ) <EOL> self . Bind ( wx . EVT_LEFT_DOWN , self . OnLeftDown ) <EOL> self . Bind ( wx . EVT_RIGHT_DOWN , self . OnRightDown ) <EOL> self . Bind ( wx . EVT_KEY_DOWN , self . OnKey ) <EOL> self . Bind ( wx . EVT_SET_FOCUS , ( lambda ( evt ) : None ) ) <EOL> tilecollection . EVT_TILE_UPDATED ( self , self . OnTileUpdated ) <EOL> self . CreatePopupMenu ( ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . label , len ( self . sizer . GetChildren ( ) ) ) <EOL> def CreatePopupMenu ( self ) : <EOL> popupMenuItems = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> if self . label != '<STR_LIT>' and self . classifier is not None : <EOL> popupMenuItems += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . popupItemIndexById = { } <EOL> self . popupMenu = wx . Menu ( ) <EOL> for i , item in enumerate ( popupMenuItems ) : <EOL> id = wx . NewId ( ) <EOL> self . popupItemIndexById [ id ] = i <EOL> self . popupMenu . Append ( id , item ) <EOL> self . popupMenu . Bind ( wx . EVT_MENU , self . OnSelectFromPopupMenu ) <EOL> def OnKey ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> if evt . GetKeyCode ( ) in [ wx . WXK_DELETE , wx . WXK_BACK ] : <EOL> self . RemoveSelectedTiles ( ) <EOL> elif evt . ControlDown ( ) or evt . CmdDown ( ) : <EOL> if evt . GetKeyCode ( ) == ord ( '<STR_LIT:A>' ) : <EOL> self . SelectAll ( ) <EOL> elif evt . GetKeyCode ( ) == ord ( '<STR_LIT:D>' ) : <EOL> self . DeselectAll ( ) <EOL> elif evt . GetKeyCode ( ) == ord ( '<STR_LIT:I>' ) : <EOL> self . InvertSelection ( ) <EOL> evt . Skip ( ) <EOL> def OnRightDown ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> self . PopupMenu ( self . popupMenu , evt . GetPosition ( ) ) <EOL> def OnSelectFromPopupMenu ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> choice = self . popupItemIndexById [ evt . GetId ( ) ] <EOL> if choice == <NUM_LIT:0> : <EOL> for key in self . SelectedKeys ( ) : <EOL> if self . classifier : <EOL> imViewer = imagetools . ShowImage ( key [ : - <NUM_LIT:1> ] , self . chMap [ : ] , <EOL> parent = self . classifier , <EOL> brightness = self . classifier . brightness , <EOL> scale = self . classifier . scale , <EOL> contrast = self . classifier . contrast ) <EOL> else : <EOL> imViewer = imagetools . ShowImage ( key [ : - <NUM_LIT:1> ] , self . chMap [ : ] , parent = self ) <EOL> imViewer . imagePanel . SelectPoint ( db . GetObjectCoords ( key ) ) <EOL> elif choice == <NUM_LIT:1> : <EOL> self . SelectAll ( ) <EOL> elif choice == <NUM_LIT:2> : <EOL> self . DeselectAll ( ) <EOL> elif choice == <NUM_LIT:3> : <EOL> self . InvertSelection ( ) <EOL> elif choice == <NUM_LIT:4> : <EOL> self . RemoveSelectedTiles ( ) <EOL> elif choice == <NUM_LIT:5> : <EOL> self . classifier . RenameClass ( self . label ) <EOL> elif choice == <NUM_LIT:6> : <EOL> self . classifier . RemoveSortClass ( self . label ) <EOL> def AddObject ( self , obKey , chMap = None , priority = <NUM_LIT:1> , pos = '<STR_LIT>' ) : <EOL> self . AddObjects ( [ obKey ] , chMap , priority , pos ) <EOL> def AddObjects ( self , obKeys , chMap = None , priority = <NUM_LIT:1> , pos = '<STR_LIT>' , display_whole_image = False ) : <EOL> if chMap is None : <EOL> chMap = p . image_channel_colors <EOL> if self . tile_collection == None : <EOL> self . tile_collection = tilecollection . TileCollection . getInstance ( ) <EOL> imgSet = self . tile_collection . GetTiles ( obKeys , ( self . classifier or self ) , priority , display_whole_image = display_whole_image ) <EOL> for i , obKey , imgs in zip ( range ( len ( obKeys ) ) , obKeys , imgSet ) : <EOL> if self . classifier and self . label == '<STR_LIT>' : <EOL> newTile = ImageTile ( self , obKey , imgs , chMap , False , <EOL> scale = self . classifier . scale , <EOL> brightness = self . classifier . brightness , <EOL> contrast = self . classifier . contrast , <EOL> display_whole_image = True ) <EOL> elif self . classifier : <EOL> newTile = ImageTile ( self , obKey , imgs , chMap , False , <EOL> scale = self . classifier . scale , <EOL> brightness = self . classifier . brightness , <EOL> contrast = self . classifier . contrast ) <EOL> else : <EOL> newTile = ImageTile ( self , obKey , imgs , chMap , False ) <EOL> if pos == '<STR_LIT>' : <EOL> self . tiles . insert ( i , newTile ) <EOL> self . sizer . Insert ( i , newTile , <NUM_LIT:0> , wx . ALL | wx . EXPAND , <NUM_LIT:1> ) <EOL> else : <EOL> self . tiles . append ( newTile ) <EOL> self . sizer . Add ( newTile , <NUM_LIT:0> , wx . ALL | wx . EXPAND , <NUM_LIT:1> ) <EOL> self . UpdateSizer ( ) <EOL> self . UpdateQuantity ( ) <EOL> def RemoveKey ( self , obKey ) : <EOL> '''<STR_LIT>''' <EOL> self . RemoveKeys ( [ obKey ] ) <EOL> def RemoveKeys ( self , obKeys ) : <EOL> '''<STR_LIT>''' <EOL> for t in self . tiles : <EOL> if t . obKey in obKeys : <EOL> self . tiles . remove ( t ) <EOL> self . sizer . Remove ( t ) <EOL> wx . CallAfter ( t . Destroy ) <EOL> wx . CallAfter ( self . UpdateSizer ) <EOL> self . UpdateQuantity ( ) <EOL> def RemoveSelectedTiles ( self ) : <EOL> for tile in self . Selection ( ) : <EOL> self . tiles . remove ( tile ) <EOL> self . sizer . Remove ( tile ) <EOL> wx . CallAfter ( tile . Destroy ) <EOL> wx . CallAfter ( self . UpdateSizer ) <EOL> self . UpdateQuantity ( ) <EOL> def Clear ( self ) : <EOL> self . RemoveKeys ( self . GetObjectKeys ( ) ) <EOL> def find_selected_tile_for_key ( self , obkey ) : <EOL> for t in self . tiles : <EOL> if t . obKey == obkey and t . selected : <EOL> return t <EOL> def ReceiveDrop ( self , srcID , obKeys ) : <EOL> def hack ( obKeys ) : <EOL> def closure ( ) : <EOL> if self . classifier : <EOL> self . AddObjects ( obKeys , self . classifier . chMap ) <EOL> else : <EOL> self . AddObjects ( obKeys ) <EOL> return closure <EOL> closure = hack ( obKeys ) <EOL> if srcID == self . GetId ( ) : <EOL> wx . CallAfter ( closure ) <EOL> return <EOL> self . DeselectAll ( ) <EOL> closure ( ) <EOL> [ tile . Select ( ) for tile in self . tiles if tile . obKey in obKeys ] <EOL> self . SetFocusIgnoringChildren ( ) <EOL> return wx . DragMove <EOL> def MapChannels ( self , chMap ) : <EOL> '''<STR_LIT>''' <EOL> self . chMap = chMap <EOL> for tile in self . tiles : <EOL> tile . MapChannels ( self . chMap ) <EOL> def SelectedKeys ( self ) : <EOL> '''<STR_LIT>''' <EOL> return [ tile . obKey for tile in self . tiles if tile . selected ] <EOL> def Selection ( self ) : <EOL> '''<STR_LIT>''' <EOL> tiles = [ ] <EOL> for tile in self . tiles : <EOL> if tile . selected : <EOL> tiles . append ( tile ) <EOL> return tiles <EOL> def GetObjectKeys ( self ) : <EOL> return [ tile . obKey for tile in self . tiles ] <EOL> def GetNumberOfTiles ( self ) : <EOL> return len ( self . tiles ) <EOL> def GetNumberOfSelectedTiles ( self ) : <EOL> return len ( self . SelectedKeys ( ) ) <EOL> def SelectAll ( self ) : <EOL> '''<STR_LIT>''' <EOL> for tile in self . tiles : <EOL> tile . Select ( ) <EOL> def DeselectAll ( self ) : <EOL> '''<STR_LIT>''' <EOL> for tile in self . tiles : <EOL> tile . Deselect ( ) <EOL> def InvertSelection ( self ) : <EOL> '''<STR_LIT>''' <EOL> for t in self . tiles : <EOL> t . ToggleSelect ( ) <EOL> def OnLeftDown ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> self . SetFocusIgnoringChildren ( ) <EOL> if not evt . ShiftDown ( ) : <EOL> self . DeselectAll ( ) <EOL> def OnTileUpdated ( self , evt ) : <EOL> '''<STR_LIT>''' <EOL> self . UpdateTile ( evt . data ) <EOL> def UpdateTile ( self , obKey ) : <EOL> '''<STR_LIT>''' <EOL> for t in self . tiles : <EOL> if t . obKey == obKey : <EOL> t . UpdateBitmap ( ) <EOL> def UpdateSizer ( self ) : <EOL> return self . SetVirtualSize ( self . sizer . CalcMin ( ) ) <EOL> def UpdateQuantity ( self ) : <EOL> '''<STR_LIT>''' <EOL> empty = len ( self . tiles ) == <NUM_LIT:0> <EOL> if ( empty and not self . empty ) or ( not empty and self . empty ) : <EOL> self . empty = empty <EOL> event = wx . PyCommandEvent ( EVT_QUANTITY_CHANGED . typeId , self . GetId ( ) ) <EOL> self . GetEventHandler ( ) . ProcessEvent ( event ) <EOL> try : <EOL> self . parentSizer . GetStaticBox ( ) . SetLabel ( '<STR_LIT>' % ( self . label , len ( self . tiles ) ) ) <EOL> except : <EOL> logging . info ( "<STR_LIT>" ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app = wx . PySimpleApp ( ) <EOL> p . show_load_dialog ( ) <EOL> import datamodel <EOL> dm = datamodel . DataModel . getInstance ( ) <EOL> f = wx . Frame ( None ) <EOL> sb = SortBin ( f ) <EOL> f . Show ( ) <EOL> sb . AddObjects ( [ dm . GetRandomObject ( ) for i in range ( <NUM_LIT:50> ) ] ) <EOL> app . MainLoop ( ) </s>
