<s> import logging <EOL> import signal <EOL> import time <EOL> from subprocess import Popen , PIPE <EOL> import numpy <EOL> try : <EOL> from bokeh . plotting import ( curdoc , cursession , figure , output_server , <EOL> push , show ) <EOL> BOKEH_AVAILABLE = True <EOL> except ImportError : <EOL> BOKEH_AVAILABLE = False <EOL> from blocks . extensions import SimpleExtension <EOL> logger = logging . getLogger ( __name__ ) <EOL> class PlotManager ( SimpleExtension ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , document , plotters , open_browser = False , <EOL> start_server = False , ** kwargs ) : <EOL> if not BOKEH_AVAILABLE : <EOL> raise ImportError <EOL> self . plotters = plotters <EOL> self . start_server = start_server <EOL> self . document = document <EOL> self . _startserver ( ) <EOL> for plotter in self . plotters : <EOL> plotter . manager = self <EOL> plotter . initialize ( ) <EOL> if open_browser : <EOL> show ( ) <EOL> kwargs . setdefault ( '<STR_LIT>' , True ) <EOL> kwargs . setdefault ( "<STR_LIT>" , True ) <EOL> super ( PlotManager , self ) . __init__ ( ** kwargs ) <EOL> def do ( self , which_callback , * args ) : <EOL> for plotter in self . plotters : <EOL> plotter . call ( ) <EOL> push ( ) <EOL> def _startserver ( self ) : <EOL> if self . start_server : <EOL> def preexec_fn ( ) : <EOL> """<STR_LIT>""" <EOL> signal . signal ( signal . SIGINT , signal . SIG_IGN ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . sub = Popen ( '<STR_LIT>' <EOL> '<STR_LIT>' . split ( ) , <EOL> stdout = PIPE , stderr = PIPE , preexec_fn = preexec_fn ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> logger . info ( '<STR_LIT>' . format ( self . sub . pid ) ) <EOL> else : <EOL> self . sub = None <EOL> output_server ( self . document ) <EOL> def __getstate__ ( self ) : <EOL> state = self . __dict__ . copy ( ) <EOL> state [ '<STR_LIT>' ] = None <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ . update ( state ) <EOL> self . _startserver ( ) <EOL> curdoc ( ) . add ( * self . p ) <EOL> class BasePlotter ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def manager ( self ) : <EOL> return self . _manager <EOL> @ manager . setter <EOL> def manager ( self , manager ) : <EOL> self . _manager = manager <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def call ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_titles ( self , titles , matching_list ) : <EOL> if isinstance ( titles , str ) : <EOL> self . titles = [ titles ] * len ( matching_list ) <EOL> else : <EOL> if not isinstance ( titles , list ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if len ( titles ) == <NUM_LIT:1> : <EOL> self . titles = titles * len ( matching_list ) <EOL> elif len ( titles ) != len ( matching_list ) : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . titles = titles <EOL> class Plotter ( BasePlotter ) : <EOL> """<STR_LIT>""" <EOL> colors = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , channels , titles = '<STR_LIT>' ) : <EOL> self . channels = channels <EOL> self . set_titles ( titles , channels ) <EOL> super ( Plotter , self ) . __init__ ( ) <EOL> def initialize ( self ) : <EOL> channels = self . channels <EOL> titles = self . titles <EOL> self . plots = { } <EOL> self . p = [ ] <EOL> self . p_indices = { } <EOL> self . color_mapping = { } <EOL> j = <NUM_LIT:0> <EOL> for i , ( channel_set , title ) in enumerate ( zip ( channels , titles ) ) : <EOL> self . p . append ( figure ( title = title ) ) <EOL> for channel in channel_set : <EOL> self . p_indices [ channel ] = i <EOL> if channel not in self . color_mapping . keys ( ) : <EOL> self . color_mapping [ channel ] = self . colors [ j ] <EOL> j += <NUM_LIT:1> <EOL> def call ( self ) : <EOL> log = self . manager . main_loop . log <EOL> iteration = log . status [ '<STR_LIT>' ] <EOL> i = <NUM_LIT:0> <EOL> for key , value in log . current_row . items ( ) : <EOL> if key in self . p_indices : <EOL> if key not in self . plots : <EOL> fig = self . p [ self . p_indices [ key ] ] <EOL> fig . line ( [ iteration ] , [ value ] , legend = key , <EOL> x_axis_label = '<STR_LIT>' , <EOL> y_axis_label = '<STR_LIT:value>' , name = key , <EOL> line_color = self . color_mapping [ key ] ) <EOL> i += <NUM_LIT:1> <EOL> renderer = fig . select ( dict ( name = key ) ) <EOL> self . plots [ key ] = renderer [ <NUM_LIT:0> ] . data_source <EOL> else : <EOL> self . plots [ key ] . data [ '<STR_LIT:x>' ] . append ( iteration ) <EOL> self . plots [ key ] . data [ '<STR_LIT:y>' ] . append ( value ) <EOL> cursession ( ) . store_objects ( self . plots [ key ] ) <EOL> class DisplayImage ( BasePlotter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , image_getters , titles = '<STR_LIT>' ) : <EOL> self . image_getters = image_getters <EOL> self . set_titles ( titles , image_getters ) <EOL> super ( DisplayImage , self ) . __init__ ( ) <EOL> def initialize ( self ) : <EOL> self . figs = [ ] <EOL> for image_getter , title in zip ( self . image_getters , self . titles ) : <EOL> self . figs . append ( figure ( title = title , <EOL> x_range = [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> y_range = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) <EOL> def call ( self ) : <EOL> for fig , image_getter in zip ( self . figs , self . image_getters ) : <EOL> rgba_image = format_image_to_rgba ( image_getter ( ) ) . copy ( ) <EOL> img_h = rgba_image . shape [ <NUM_LIT:0> ] <EOL> img_w = rgba_image . shape [ <NUM_LIT:1> ] <EOL> fig . renderers = [ ] <EOL> fig . image_rgba ( image = [ rgba_image ] , <EOL> x = [ <NUM_LIT:0> ] , y = [ <NUM_LIT:0> ] , <EOL> dw = [ min ( img_w * <NUM_LIT:1.> / img_h , <NUM_LIT:1> ) ] , <EOL> dh = [ min ( img_h * <NUM_LIT:1.> / img_w , <NUM_LIT:1> ) ] ) <EOL> def format_image_to_rgba ( image_0255 ) : <EOL> image_0255_clipped = numpy . clip ( image_0255 , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> int_image = numpy . round ( <EOL> image_0255_clipped <EOL> ) . astype ( dtype = numpy . uint8 ) <EOL> rgba_image = numpy . zeros ( int_image . shape [ : <NUM_LIT:2> ] , dtype = numpy . uint32 ) <EOL> image_view = rgba_image . view ( <EOL> dtype = numpy . uint8 <EOL> ) . reshape ( int_image . shape [ : <NUM_LIT:2> ] + ( <NUM_LIT:4> , ) ) <EOL> image_view [ : , : , <NUM_LIT:3> ] = <NUM_LIT:255> <EOL> if int_image . ndim == <NUM_LIT:2> : <EOL> image_view [ : , : , : <NUM_LIT:3> ] = int_image [ : , : , None ] <EOL> elif int_image . ndim == <NUM_LIT:3> : <EOL> image_view [ : , : , : <NUM_LIT:3> ] = int_image <EOL> else : <EOL> image_view = int_image <EOL> return rgba_image <EOL> class Display2DData ( BasePlotter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data_streams , titles = '<STR_LIT>' , alpha = <NUM_LIT> , radius = <NUM_LIT:0.1> ) : <EOL> self . data_streams = data_streams <EOL> self . set_titles ( titles , data_streams ) <EOL> self . alpha = alpha <EOL> self . radius = radius <EOL> for data_stream in data_streams : <EOL> if '<STR_LIT>' not in data_stream . sources : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> super ( Display2DData , self ) . __init__ ( ) <EOL> def initialize ( self ) : <EOL> self . figs = [ ] <EOL> for data_stream , title in zip ( self . data_streams , self . titles ) : <EOL> self . figs . append ( figure ( title = title ) ) <EOL> def call ( self ) : <EOL> for fig , data_stream in zip ( self . figs , self . data_streams ) : <EOL> iterator = data_stream . get_epoch_iterator ( ) <EOL> fig . renderers = fig . renderers [ : <NUM_LIT:4> ] <EOL> for data in iterator : <EOL> coordinates = data [ data_stream . sources . index ( '<STR_LIT>' ) ] <EOL> if '<STR_LIT>' in data_stream . sources : <EOL> color_intensities = data [ <EOL> data_stream . sources . index ( '<STR_LIT>' ) ] <EOL> color_intensities = numpy . round ( <EOL> color_intensities [ : , <NUM_LIT:0> ] * <NUM_LIT:255> <EOL> ) <EOL> colors = [ "<STR_LIT>" % ( <NUM_LIT:255> - color_intensity , <EOL> color_intensity , <NUM_LIT> ) <EOL> for color_intensity in color_intensities ] <EOL> elif '<STR_LIT>' in data_stream . sources : <EOL> color_intensities = data [ <EOL> data_stream . sources . index ( '<STR_LIT>' ) ] <EOL> color_intensities = numpy . round ( <EOL> color_intensities [ : , <NUM_LIT:0> ] * <NUM_LIT:255> <EOL> ) <EOL> colors = [ "<STR_LIT>" % ( <NUM_LIT:255> - color_intensity , <EOL> color_intensity , <NUM_LIT> ) <EOL> for color_intensity in color_intensities ] <EOL> else : <EOL> colors = "<STR_LIT>" <EOL> fig . scatter ( coordinates [ : , <NUM_LIT:0> ] , coordinates [ : , <NUM_LIT:1> ] , <EOL> radius = <NUM_LIT> , fill_color = colors , fill_alpha = <NUM_LIT:1.> , <EOL> line_color = None ) </s>
