<s> import sys , re <EOL> import numpy <EOL> import threading <EOL> import math <EOL> from io import BytesIO <EOL> from bokeh . plotting import figure , show , curdoc <EOL> from bokeh . models import BoxSelectTool , TapTool , PanTool <EOL> from ginga import ImageView <EOL> from ginga import Mixins , Bindings , colors <EOL> from ginga . web . bokehw . CanvasRenderBokeh import CanvasRenderer <EOL> from ginga . util . heaptimer import TimerHeap <EOL> try : <EOL> from ginga . aggw . ImageViewAgg import ImageViewAgg as ImageViewSS , ImageViewAggError as ImageViewError <EOL> except ImportError : <EOL> try : <EOL> from ginga . cvw . ImageViewCv import ImageViewCv as ImageViewSS , ImageViewCvError as ImageViewError <EOL> except ImportError : <EOL> try : <EOL> from ginga . pilw . ImageViewPil import ImageViewPil as ImageViewSS , ImageViewPilError as ImageViewError <EOL> except ImportError : <EOL> from ginga . mockw . ImageViewMock import ImageViewMock as ImageViewSS , ImageViewMockError as ImageViewError <EOL> class ImageViewBokehError ( ImageViewError ) : <EOL> pass <EOL> class ImageViewBokeh2 ( ImageViewSS ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None ) : <EOL> ImageViewSS . __init__ ( self , logger = logger , <EOL> rgbmap = rgbmap , <EOL> settings = settings ) <EOL> self . figure = None <EOL> self . bkimage = None <EOL> self . _push_server = False <EOL> self . _originUpper = False <EOL> self . msgtask = None <EOL> self . _defer_task = None <EOL> def set_figure ( self , figure ) : <EOL> """<STR_LIT>""" <EOL> self . figure = figure <EOL> self . bkimage = None <EOL> wd = figure . plot_width <EOL> ht = figure . plot_height <EOL> self . configure_window ( wd , ht ) <EOL> def get_figure ( self ) : <EOL> return self . figure <EOL> def update_image ( self ) : <EOL> if self . figure is None : <EOL> return <EOL> wd , ht = self . get_window_size ( ) <EOL> surface = self . get_surface ( ) <EOL> if isinstance ( surface , numpy . ndarray ) : <EOL> arr8 = surface <EOL> else : <EOL> arr8 = numpy . fromstring ( surface . tostring ( ) , dtype = numpy . uint8 ) <EOL> arr8 = arr8 . reshape ( ( ht , wd , <NUM_LIT:4> ) ) <EOL> view = arr8 . view ( dtype = numpy . uint32 ) . reshape ( ( ht , wd ) ) <EOL> dst_x = dst_y = <NUM_LIT:0> <EOL> if self . bkimage is None : <EOL> self . bkimage = self . figure . image_rgba ( image = [ view ] , <EOL> x = [ dst_x ] , y = [ dst_y ] , <EOL> dw = [ wd ] , dh = [ ht ] ) <EOL> d_src = self . bkimage . data_source <EOL> self . _setup_handlers ( d_src ) <EOL> else : <EOL> d_src = self . bkimage . data_source <EOL> d_src . data [ "<STR_LIT:image>" ] = [ view ] <EOL> d_src . data [ "<STR_LIT:x>" ] = [ dst_x ] <EOL> d_src . data [ "<STR_LIT:y>" ] = [ dst_y ] <EOL> d_src . data [ "<STR_LIT>" ] = [ wd ] <EOL> d_src . data [ "<STR_LIT>" ] = [ ht ] <EOL> if self . _push_server : <EOL> try : <EOL> cursession ( ) . store_objects ( d_src ) <EOL> except Exception as e : <EOL> self . logger . warning ( "<STR_LIT>" % ( str ( e ) ) ) <EOL> def reschedule_redraw ( self , time_sec ) : <EOL> if self . figure is not None : <EOL> time_ms = int ( time_sec * <NUM_LIT:1000> ) <EOL> def configure_window ( self , width , height ) : <EOL> self . configure_surface ( width , height ) <EOL> def _setup_handlers ( self , source ) : <EOL> pass <EOL> def set_cursor ( self , cursor ) : <EOL> if self . figure is None : <EOL> return <EOL> def onscreen_message ( self , text , delay = None ) : <EOL> if self . figure is None : <EOL> return <EOL> self . message = text <EOL> self . redraw ( whence = <NUM_LIT:3> ) <EOL> if delay : <EOL> ms = int ( delay * <NUM_LIT> ) <EOL> class ImageViewBokeh ( ImageView . ImageViewBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None ) : <EOL> ImageView . ImageViewBase . __init__ ( self , logger = logger , <EOL> rgbmap = rgbmap , <EOL> settings = settings ) <EOL> self . figure = None <EOL> self . bkimage = None <EOL> self . _push_server = False <EOL> self . defer_redraw = False <EOL> self . _originUpper = False <EOL> self . img_fg = ( <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ) <EOL> self . img_bg = ( <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT:0.5> ) <EOL> self . message = None <EOL> self . _rgb_order = '<STR_LIT>' <EOL> self . renderer = CanvasRenderer ( self ) <EOL> self . cursor = { } <EOL> self . _msg_timer = None <EOL> self . _defer_timer = None <EOL> self . t_ . setDefaults ( show_pan_position = False , <EOL> onscreen_ff = '<STR_LIT>' ) <EOL> def set_figure ( self , figure ) : <EOL> """<STR_LIT>""" <EOL> self . figure = figure <EOL> self . bkimage = None <EOL> wd = figure . plot_width <EOL> ht = figure . plot_height <EOL> self . configure_window ( wd , ht ) <EOL> def get_figure ( self ) : <EOL> return self . figure <EOL> def get_rgb_order ( self ) : <EOL> return self . _rgb_order <EOL> def render_image ( self , rgbobj , dst_x , dst_y ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> if self . figure is None : <EOL> return <EOL> data = self . getwin_array ( order = self . _rgb_order ) <EOL> ht , wd = data . shape [ : <NUM_LIT:2> ] <EOL> view = data . view ( dtype = numpy . uint32 ) . reshape ( ( ht , wd ) ) <EOL> dst_x = dst_y = <NUM_LIT:0> <EOL> if self . bkimage is None : <EOL> self . bkimage = self . figure . image_rgba ( image = [ view ] , <EOL> x = [ dst_x ] , y = [ dst_y ] , <EOL> dw = [ wd ] , dh = [ ht ] ) <EOL> d_src = self . bkimage . data_source <EOL> self . _setup_handlers ( d_src ) <EOL> else : <EOL> d_src = self . bkimage . data_source <EOL> d_src . data [ "<STR_LIT:image>" ] = [ view ] <EOL> d_src . data [ "<STR_LIT:x>" ] = [ dst_x ] <EOL> d_src . data [ "<STR_LIT:y>" ] = [ dst_y ] <EOL> d_src . data [ "<STR_LIT>" ] = [ wd ] <EOL> d_src . data [ "<STR_LIT>" ] = [ ht ] <EOL> if self . t_ [ '<STR_LIT>' ] : <EOL> size = int ( max ( ht , wd ) * <NUM_LIT> ) <EOL> ctr_x , ctr_y = self . get_center ( ) <EOL> self . figure . cross ( x = [ ctr_x ] , y = [ ctr_y ] , <EOL> size = size , color = '<STR_LIT>' , line_width = <NUM_LIT:1> ) <EOL> if self . message : <EOL> y = ( ht // <NUM_LIT:3> ) * <NUM_LIT:2> <EOL> x = ( wd // <NUM_LIT:2> ) <EOL> self . figure . text ( x = [ x ] , y = [ y ] , text = [ self . message ] , <EOL> text_font_size = '<STR_LIT>' , text_baseline = '<STR_LIT>' , <EOL> text_color = '<STR_LIT>' , <EOL> text_align = '<STR_LIT>' ) <EOL> if self . _push_server : <EOL> try : <EOL> cursession ( ) . store_objects ( d_src ) <EOL> except Exception as e : <EOL> self . logger . warning ( "<STR_LIT>" % ( str ( e ) ) ) <EOL> def configure_window ( self , width , height ) : <EOL> self . configure ( width , height ) <EOL> def _setup_handlers ( self , source ) : <EOL> pass <EOL> def _resize_cb ( self , event ) : <EOL> wd , ht = event . width , event . height <EOL> self . logger . debug ( "<STR_LIT>" % ( wd , ht ) ) <EOL> self . configure_window ( wd , ht ) <EOL> def get_png_image_as_buffer ( self , output = None ) : <EOL> ibuf = output <EOL> if ibuf is None : <EOL> ibuf = BytesIO ( ) <EOL> qimg = self . figure . write_to_png ( ibuf ) <EOL> return ibuf <EOL> def update_image ( self ) : <EOL> pass <EOL> def set_cursor ( self , cursor ) : <EOL> pass <EOL> def define_cursor ( self , ctype , cursor ) : <EOL> self . cursor [ ctype ] = cursor <EOL> def get_cursor ( self , ctype ) : <EOL> return self . cursor [ ctype ] <EOL> def switch_cursor ( self , ctype ) : <EOL> self . set_cursor ( self . cursor [ ctype ] ) <EOL> def set_fg ( self , r , g , b ) : <EOL> self . img_fg = ( r , g , b ) <EOL> self . redraw ( whence = <NUM_LIT:3> ) <EOL> def onscreen_message ( self , text , delay = None ) : <EOL> self . message = text <EOL> self . redraw ( whence = <NUM_LIT:3> ) <EOL> if delay : <EOL> time_ms = int ( delay * <NUM_LIT> ) <EOL> def onscreen_message_off ( self ) : <EOL> return self . onscreen_message ( None ) <EOL> def reschedule_redraw ( self , time_sec ) : <EOL> if self . _defer_timer is None : <EOL> self . delayed_redraw ( ) <EOL> return <EOL> time_ms = int ( time_sec * <NUM_LIT:1000> ) <EOL> try : <EOL> self . _defer_timer . interval = time_ms <EOL> self . _defer_timer . start ( ) <EOL> except Exception as e : <EOL> self . logger . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( str ( e ) ) ) <EOL> self . delayed_redraw ( ) <EOL> def show_pan_mark ( self , tf ) : <EOL> self . t_ . set ( show_pan_position = tf ) <EOL> self . redraw ( whence = <NUM_LIT:3> ) <EOL> def timer_cb ( self , * args ) : <EOL> self . logger . info ( "<STR_LIT>" ) <EOL> class ImageViewEvent ( ImageViewBokeh ) : <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None ) : <EOL> ImageViewBokeh . __init__ ( self , logger = logger , rgbmap = rgbmap , <EOL> settings = settings ) <EOL> self . last_win_x = <NUM_LIT:0> <EOL> self . last_win_y = <NUM_LIT:0> <EOL> self . last_data_x = <NUM_LIT:0> <EOL> self . last_data_y = <NUM_LIT:0> <EOL> self . enter_focus = self . t_ . get ( '<STR_LIT>' , True ) <EOL> self . _keytbl = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:">' : '<STR_LIT>' , <EOL> "<STR_LIT:'>" : '<STR_LIT>' , <EOL> '<STR_LIT:\\>' : '<STR_LIT>' , <EOL> '<STR_LIT:U+0020>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> hand = <NUM_LIT:0> <EOL> self . define_cursor ( '<STR_LIT>' , hand ) <EOL> cross = <NUM_LIT:1> <EOL> self . define_cursor ( '<STR_LIT>' , cross ) <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) : <EOL> self . enable_callback ( name ) <EOL> def set_figure ( self , figure ) : <EOL> super ( ImageViewEvent , self ) . set_figure ( figure ) <EOL> def _setup_handlers ( self , source ) : <EOL> source . on_change ( '<STR_LIT>' , self . select_event_cb ) <EOL> self . _pan_tool = self . figure . select ( dict ( type = PanTool ) ) <EOL> pass <EOL> def transkey ( self , keyname ) : <EOL> self . logger . debug ( "<STR_LIT>" % ( keyname ) ) <EOL> if keyname is None : <EOL> return keyname <EOL> try : <EOL> return self . _keytbl [ keyname . lower ( ) ] <EOL> except KeyError : <EOL> return keyname <EOL> def get_keyTable ( self ) : <EOL> return self . _keytbl <EOL> def set_enter_focus ( self , tf ) : <EOL> self . enter_focus = tf <EOL> def focus_event ( self , event , hasFocus ) : <EOL> return self . make_callback ( '<STR_LIT>' , hasFocus ) <EOL> def enter_notify_event ( self , event ) : <EOL> if self . enter_focus : <EOL> self . focus_event ( event , True ) <EOL> return self . make_callback ( '<STR_LIT>' ) <EOL> def leave_notify_event ( self , event ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> if self . enter_focus : <EOL> self . focus_event ( event , False ) <EOL> return self . make_callback ( '<STR_LIT>' ) <EOL> def key_press_event ( self , event ) : <EOL> keyname = event . key <EOL> keyname = self . transkey ( keyname ) <EOL> if keyname is not None : <EOL> self . logger . debug ( "<STR_LIT>" % ( keyname ) ) <EOL> return self . make_ui_callback ( '<STR_LIT>' , keyname ) <EOL> def key_release_event ( self , event ) : <EOL> keyname = event . key <EOL> keyname = self . transkey ( keyname ) <EOL> if keyname is not None : <EOL> self . logger . debug ( "<STR_LIT>" % ( keyname ) ) <EOL> return self . make_ui_callback ( '<STR_LIT>' , keyname ) <EOL> def button_press_event ( self , event ) : <EOL> x , y = event . x , event . y <EOL> button = <NUM_LIT:0> <EOL> if event . button in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> button |= <NUM_LIT> << ( event . button - <NUM_LIT:1> ) <EOL> self . logger . debug ( "<STR_LIT>" % ( x , y , button ) ) <EOL> data_x , data_y = self . get_data_xy ( x , y ) <EOL> return self . make_ui_callback ( '<STR_LIT>' , button , data_x , data_y ) <EOL> def button_release_event ( self , event ) : <EOL> x , y = event . x , event . y <EOL> button = <NUM_LIT:0> <EOL> if event . button in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> button |= <NUM_LIT> << ( event . button - <NUM_LIT:1> ) <EOL> self . logger . debug ( "<STR_LIT>" % ( x , y , button ) ) <EOL> data_x , data_y = self . get_data_xy ( x , y ) <EOL> return self . make_ui_callback ( '<STR_LIT>' , button , data_x , data_y ) <EOL> def select_event_cb ( self , attrname , old_val , new_val ) : <EOL> print ( "<STR_LIT>" % ( attrname , str ( new_val ) ) ) <EOL> self . logger . info ( "<STR_LIT>" % ( attrname , str ( new_val ) ) ) <EOL> def get_last_win_xy ( self ) : <EOL> return ( self . last_win_x , self . last_win_y ) <EOL> def get_last_data_xy ( self ) : <EOL> return ( self . last_data_x , self . last_data_y ) <EOL> def motion_notify_event ( self , event ) : <EOL> button = <NUM_LIT:0> <EOL> x , y = event . x , event . y <EOL> self . last_win_x , self . last_win_y = x , y <EOL> if event . button in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> button |= <NUM_LIT> << ( event . button - <NUM_LIT:1> ) <EOL> self . logger . debug ( "<STR_LIT>" % ( x , y , button ) ) <EOL> data_x , data_y = self . get_data_xy ( x , y ) <EOL> self . last_data_x , self . last_data_y = data_x , data_y <EOL> self . logger . debug ( "<STR_LIT>" % ( data_x , data_y ) ) <EOL> return self . make_ui_callback ( '<STR_LIT>' , button , data_x , data_y ) <EOL> def scroll_event ( self , event ) : <EOL> x , y = event . x , event . y <EOL> direction = None <EOL> if event . step > <NUM_LIT:0> : <EOL> direction = <NUM_LIT:0.0> <EOL> elif event . step < <NUM_LIT:0> : <EOL> direction = <NUM_LIT> <EOL> amount = abs ( event . step ) * <NUM_LIT> <EOL> self . logger . debug ( "<STR_LIT>" % ( <EOL> amount , direction ) ) <EOL> data_x , data_y = self . get_data_xy ( x , y ) <EOL> self . last_data_x , self . last_data_y = data_x , data_y <EOL> return self . make_ui_callback ( '<STR_LIT>' , direction , amount , <EOL> data_x , data_y ) <EOL> class ImageViewZoom ( Mixins . UIMixin , ImageViewEvent ) : <EOL> bindmapClass = Bindings . BindingMapper <EOL> bindingsClass = Bindings . ImageViewBindings <EOL> @ classmethod <EOL> def set_bindingsClass ( cls , klass ) : <EOL> cls . bindingsClass = klass <EOL> @ classmethod <EOL> def set_bindmapClass ( cls , klass ) : <EOL> cls . bindmapClass = klass <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None , <EOL> bindmap = None , bindings = None ) : <EOL> ImageViewEvent . __init__ ( self , logger = logger , rgbmap = rgbmap , <EOL> settings = settings ) <EOL> Mixins . UIMixin . __init__ ( self ) <EOL> self . ui_setActive ( True ) <EOL> if bindmap is None : <EOL> bindmap = ImageViewZoom . bindmapClass ( self . logger ) <EOL> self . bindmap = bindmap <EOL> bindmap . register_for_events ( self ) <EOL> if bindings is None : <EOL> bindings = ImageViewZoom . bindingsClass ( self . logger ) <EOL> self . set_bindings ( bindings ) <EOL> def get_bindmap ( self ) : <EOL> return self . bindmap <EOL> def get_bindings ( self ) : <EOL> return self . bindings <EOL> def set_bindings ( self , bindings ) : <EOL> self . bindings = bindings <EOL> bindings . set_bindings ( self ) <EOL> class CanvasView ( ImageViewZoom ) : <EOL> def __init__ ( self , logger = None , settings = None , rgbmap = None , <EOL> bindmap = None , bindings = None ) : <EOL> ImageViewZoom . __init__ ( self , logger = logger , settings = settings , <EOL> rgbmap = rgbmap , <EOL> bindmap = bindmap , bindings = bindings ) <EOL> self . objects = [ self . private_canvas ] <EOL> def set_canvas ( self , canvas , private_canvas = None ) : <EOL> super ( CanvasView , self ) . set_canvas ( canvas , <EOL> private_canvas = private_canvas ) <EOL> self . objects [ <NUM_LIT:0> ] = self . private_canvas </s>
