<s> import sys , re <EOL> import numpy <EOL> import threading <EOL> import math <EOL> from io import BytesIO <EOL> import matplotlib <EOL> from matplotlib . image import FigureImage <EOL> from matplotlib . figure import Figure <EOL> import matplotlib . lines as lines <EOL> from ginga import ImageView <EOL> from ginga import Mixins , Bindings , colors <EOL> from . import transform <EOL> from ginga . mplw . CanvasRenderMpl import CanvasRenderer <EOL> rc = matplotlib . rcParams <EOL> rc . update ( matplotlib . rcParamsDefault ) <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT:f>' <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT:left>' <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT:right>' <EOL> rc [ '<STR_LIT>' ] = [ ] <EOL> rc [ '<STR_LIT>' ] = [ ] <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rc [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> rc [ '<STR_LIT>' ] = [ ] <EOL> rc [ '<STR_LIT>' ] = [ ] <EOL> rc [ '<STR_LIT>' ] = [ ] <EOL> class ImageViewMplError ( ImageView . ImageViewError ) : <EOL> pass <EOL> class ImageViewMpl ( ImageView . ImageViewBase ) : <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 . ax_img = None <EOL> self . ax_util = None <EOL> self . mpimage = None <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 . in_axes = False <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> ax = self . figure . add_axes ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ) , frame_on = False ) <EOL> self . ax_img = ax <EOL> ax . hold ( False ) <EOL> ax . get_xaxis ( ) . set_visible ( False ) <EOL> ax . get_yaxis ( ) . set_visible ( False ) <EOL> ax . patch . set_visible ( False ) <EOL> ax . autoscale ( enable = False ) <EOL> newax = self . figure . add_axes ( self . ax_img . get_position ( ) , <EOL> sharex = ax , sharey = ax , <EOL> frameon = False ) <EOL> newax . hold ( True ) <EOL> newax . autoscale ( enable = False ) <EOL> newax . get_xaxis ( ) . set_visible ( False ) <EOL> newax . get_yaxis ( ) . set_visible ( False ) <EOL> self . ax_util = newax <EOL> self . _msg_timer = None <EOL> self . _defer_timer = None <EOL> if hasattr ( figure . canvas , '<STR_LIT>' ) : <EOL> self . _msg_timer = figure . canvas . new_timer ( ) <EOL> self . _msg_timer . single_shot = True <EOL> self . _msg_timer . add_callback ( self . onscreen_message_off ) <EOL> self . _defer_timer = figure . canvas . new_timer ( ) <EOL> self . _defer_timer . single_shot = True <EOL> self . _defer_timer . add_callback ( self . delayed_redraw ) <EOL> self . cross1 = lines . Line2D ( ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) , <EOL> transform = newax . transAxes , <EOL> color = '<STR_LIT>' , alpha = <NUM_LIT:1.0> ) <EOL> self . cross2 = lines . Line2D ( ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) , <EOL> transform = newax . transAxes , <EOL> color = '<STR_LIT>' , alpha = <NUM_LIT:1.0> ) <EOL> canvas = figure . canvas <EOL> if hasattr ( canvas , '<STR_LIT>' ) : <EOL> canvas . set_viewer ( self ) <EOL> else : <EOL> canvas . mpl_connect ( "<STR_LIT>" , self . _resize_cb ) <EOL> left , bottom , wd , ht = self . ax_img . bbox . bounds <EOL> self . configure_window ( wd , ht ) <EOL> def get_figure ( self ) : <EOL> return self . figure <EOL> def set_widget ( self , canvas ) : <EOL> if hasattr ( canvas , '<STR_LIT>' ) : <EOL> canvas . set_viewer ( self ) <EOL> def get_widget ( self ) : <EOL> return self . figure . canvas <EOL> def get_rgb_order ( self ) : <EOL> return self . _rgb_order <EOL> def calculate_aspect ( self , shape , extent ) : <EOL> dx = abs ( extent [ <NUM_LIT:1> ] - extent [ <NUM_LIT:0> ] ) / float ( shape [ <NUM_LIT:1> ] ) <EOL> dy = abs ( extent [ <NUM_LIT:3> ] - extent [ <NUM_LIT:2> ] ) / float ( shape [ <NUM_LIT:0> ] ) <EOL> return dx / dy <EOL> def render_image1 ( 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> dst_x = dst_y = <NUM_LIT:0> <EOL> if self . mpimage is None : <EOL> self . mpimage = self . figure . figimage ( data , xo = dst_x , yo = dst_y , <EOL> origin = '<STR_LIT>' ) <EOL> else : <EOL> self . mpimage . ox = dst_x <EOL> self . mpimage . oy = dst_y <EOL> self . mpimage . set_data ( data ) <EOL> def render_image2 ( self , rgbobj , dst_x , dst_y ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> if self . figure is None : <EOL> return <EOL> arr = self . getwin_array ( order = self . _rgb_order ) <EOL> x0 , y0 = <NUM_LIT:0> , <NUM_LIT:0> <EOL> y1 , x1 = arr . shape [ : <NUM_LIT:2> ] <EOL> flipx , flipy , swapxy = self . get_transforms ( ) <EOL> if swapxy : <EOL> x0 , x1 , y0 , y1 = y0 , y1 , x0 , x1 <EOL> extent = ( x0 , x1 , y1 , y0 ) <EOL> self . logger . debug ( "<STR_LIT>" % ( str ( extent ) ) ) <EOL> aspect = self . calculate_aspect ( arr . shape , extent ) <EOL> if self . mpimage is None : <EOL> img = self . ax_img . imshow ( arr , interpolation = '<STR_LIT:none>' , <EOL> origin = "<STR_LIT>" , <EOL> vmin = <NUM_LIT:0> , vmax = <NUM_LIT:255> , <EOL> extent = extent , <EOL> aspect = aspect ) <EOL> self . mpimage = img <EOL> else : <EOL> self . mpimage . set_data ( arr ) <EOL> self . ax_img . set_aspect ( aspect ) <EOL> self . mpimage . set_extent ( extent ) <EOL> def render_image ( self , rgbobj , dst_x , dst_y ) : <EOL> for ax in self . figure . axes : <EOL> if not ( ax in ( self . ax_img , self . ax_util ) ) : <EOL> if hasattr ( ax , "<STR_LIT>" ) : <EOL> for line in ax . lines : <EOL> try : <EOL> line . _transformed_path . invalidate ( ) <EOL> except AttributeError : <EOL> pass <EOL> if self . in_axes : <EOL> self . render_image2 ( rgbobj , dst_x , dst_y ) <EOL> else : <EOL> self . render_image1 ( rgbobj , dst_x , dst_y ) <EOL> self . ax_util . cla ( ) <EOL> if self . t_ [ '<STR_LIT>' ] : <EOL> self . ax_util . add_line ( self . cross1 ) <EOL> self . ax_util . add_line ( self . cross2 ) <EOL> if self . message : <EOL> self . draw_message ( self . message ) <EOL> self . figure . canvas . draw ( ) <EOL> def draw_message ( self , message ) : <EOL> self . ax_util . text ( <NUM_LIT:0.5> , <NUM_LIT> , message , <EOL> fontsize = <NUM_LIT> , horizontalalignment = '<STR_LIT>' , <EOL> color = '<STR_LIT>' , <EOL> verticalalignment = '<STR_LIT>' , <EOL> transform = self . ax_util . transAxes ) <EOL> def configure_window ( self , width , height ) : <EOL> self . configure ( width , height ) <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 add_axes ( self ) : <EOL> ax = self . figure . add_axes ( self . ax_img . get_position ( ) , <EOL> frameon = False , <EOL> viewer = self , <EOL> projection = '<STR_LIT>' ) <EOL> ax . get_xaxis ( ) . set_visible ( False ) <EOL> ax . get_yaxis ( ) . set_visible ( False ) <EOL> return ax <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> try : <EOL> self . _msg_timer . stop ( ) <EOL> except : <EOL> pass <EOL> self . message = text <EOL> self . redraw ( whence = <NUM_LIT:3> ) <EOL> if delay : <EOL> time_ms = int ( delay * <NUM_LIT> ) <EOL> self . _msg_timer . interval = time_ms <EOL> self . _msg_timer . start ( ) <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> try : <EOL> self . _defer_timer . stop ( ) <EOL> except : <EOL> pass <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> class ImageViewEvent ( ImageViewMpl ) : <EOL> def __init__ ( self , logger = None , rgbmap = None , settings = None ) : <EOL> ImageViewMpl . __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> connect = figure . canvas . mpl_connect <EOL> connect ( "<STR_LIT>" , self . enter_notify_event ) <EOL> connect ( "<STR_LIT>" , self . leave_notify_event ) <EOL> connect ( "<STR_LIT>" , self . motion_notify_event ) <EOL> connect ( "<STR_LIT>" , self . button_press_event ) <EOL> connect ( "<STR_LIT>" , self . button_release_event ) <EOL> connect ( "<STR_LIT>" , self . key_press_event ) <EOL> connect ( "<STR_LIT>" , self . key_release_event ) <EOL> connect ( "<STR_LIT>" , self . scroll_event ) <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 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>
