<s> import re <EOL> import skspec <EOL> import skspec . data <EOL> import matplotlib . pyplot as plt <EOL> from datetime import datetime <EOL> from collections import OrderedDict <EOL> from skspec import Spectra <EOL> from IPython . html . widgets import ( <EOL> FlexBox , VBox , HBox , HTML , Box , RadioButtons , <EOL> FloatText , Dropdown , Checkbox , Image , <EOL> IntSlider , Button , Text , FloatSlider , IntText , ContainerWidget <EOL> ) <EOL> from IPython . utils . traitlets import ( <EOL> link , Unicode , Float , Int , Enum , Bool , Instance , Any <EOL> ) <EOL> from IPython import get_ipython <EOL> from specgui import Box , HTML <EOL> from nbtools import mpl2html , log_message <EOL> from skspec . core . spectra import _normdic as NUdic <EOL> import skspec . config as pvconf <EOL> from skspec . data import aunps_glass <EOL> from skspec . plotting . advanced_plots import PLOTPARSER <EOL> from skspec . correlation import Corr2d <EOL> class SpectraModel ( HTML , Box ) : <EOL> """<STR_LIT>""" <EOL> classname = Unicode ( "<STR_LIT>" , sync = True ) <EOL> title = Unicode ( "<STR_LIT>" , sync = True ) <EOL> CONTENT = Unicode ( """<STR_LIT>""" , sync = True ) <EOL> html = Bool ( sync = True ) <EOL> DONT_DRAW = re . compile ( r'<STR_LIT>' ) <EOL> SPECUNITS = aunps_glass ( ) . specunits ( ) <EOL> VARUNITS = aunps_glass ( ) . varunits ( ) <EOL> NORMUNITS = NUdic <EOL> SPECUNITS_REV = OrderedDict ( ( v , k ) for k , v in SPECUNITS . items ( ) ) <EOL> VARUNITS_REV = OrderedDict ( ( v , k ) for k , v in VARUNITS . items ( ) ) <EOL> NORMUNITS_REV = OrderedDict ( ( v , k ) for k , v in NORMUNITS . items ( ) ) <EOL> COLORS = [ "<STR_LIT:b>" , "<STR_LIT:g>" , "<STR_LIT:r>" , "<STR_LIT:y>" , "<STR_LIT:k>" ] <EOL> COLORMAPS = sorted ( m for m in plt . cm . datad if not m . endswith ( "<STR_LIT>" ) ) <EOL> SLIDER_STEPS = Float ( <NUM_LIT> ) <EOL> load_spec = Bool ( False , sync = True ) <EOL> load_file = Bool ( True , sync = True ) <EOL> file_name = Unicode ( "<STR_LIT>" , sync = True ) <EOL> save_spec = Bool ( False , sync = True ) <EOL> save_spec_as = Unicode ( "<STR_LIT>" , sync = True ) <EOL> inbox = Bool ( False , sync = True ) <EOL> outbox = Bool ( False , sync = True ) <EOL> spec = Instance ( Spectra ) <EOL> testdataset = Unicode ( '<STR_LIT>' , sync = True ) <EOL> spec_modified = Instance ( Spectra ) <EOL> figwidth = Float ( <NUM_LIT> ) <EOL> figheight = Float ( <NUM_LIT> ) <EOL> interactive = Bool ( False , sync = True ) <EOL> colorbar = Bool ( False , sync = True ) <EOL> autoupdate = Bool ( True , sync = True ) <EOL> colormap = Enum ( COLORMAPS , sync = True ) <EOL> color = Enum ( COLORS , default_value = '<STR_LIT:k>' , sync = True ) <EOL> advancedbox = Bool ( False , sync = True ) <EOL> cmapbox = Bool ( False , sync = True ) <EOL> colorbox = Bool ( False , sync = True ) <EOL> kind = Enum ( PLOTPARSER . keys ( ) , default_value = '<STR_LIT>' , sync = True ) <EOL> selectlines = Bool ( False , sync = True ) <EOL> spec_unit = Enum ( SPECUNITS . values ( ) , sync = True ) <EOL> var_unit = Enum ( VARUNITS . values ( ) , sync = True ) <EOL> iunit = Unicode <EOL> norm_unit = Enum ( NORMUNITS . values ( ) , sync = True ) <EOL> message = Unicode <EOL> specslice_position_start = Float ( sync = True ) <EOL> specslice_position_end = Float ( sync = True ) <EOL> specslider_start = Float ( sync = True ) <EOL> specslider_end = Float ( sync = True ) <EOL> specstep = Float ( sync = True ) <EOL> specspacing = Int ( <NUM_LIT:1> , sync = True ) <EOL> timeslice_position_start = Float ( sync = True ) <EOL> timeslice_position_end = Float ( sync = True ) <EOL> timeslider_start = Float ( sync = True ) <EOL> timeslider_end = Float ( sync = True ) <EOL> timestep = Float ( sync = True ) <EOL> timespacing = Int ( <NUM_LIT:1> , sync = True ) <EOL> specbox = Bool ( False , sync = True ) <EOL> timebox = Bool ( False , sync = True ) <EOL> scalebox = Bool ( False , sync = True ) <EOL> user_f = Unicode ( sync = True ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SpectraModel , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _dom_classes += ( "<STR_LIT>" , ) <EOL> def _spec_default ( self ) : <EOL> return getattr ( skspec . data , '<STR_LIT>' ) ( ) <EOL> def _colormap_default ( self ) : <EOL> return pvconf . CMAP_1DSPECPLOT <EOL> def _spec_changed ( self , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> self . spec_modified = self . spec <EOL> self . _FREEZE = True <EOL> self . spec_unit = self . spec . full_specunit <EOL> self . var_unit = self . spec . full_varunit <EOL> self . norm_unit = self . spec . full_norm <EOL> self . iunit = self . spec . full_iunit <EOL> self . specslice_position_start = self . spec . index [ <NUM_LIT:0> ] <EOL> self . specslice_position_end = self . spec . index [ - <NUM_LIT:1> ] <EOL> self . specslider_start = self . spec . index [ <NUM_LIT:0> ] <EOL> self . specslider_end = self . spec . index [ - <NUM_LIT:1> ] <EOL> self . specstep = ( self . spec . index . max ( ) - self . spec . index . min ( ) ) / self . SLIDER_STEPS <EOL> self . specspacing = <NUM_LIT:1> <EOL> self . timeslice_position_start = self . spec . columns [ <NUM_LIT:0> ] <EOL> self . timeslice_position_end = self . spec . columns [ - <NUM_LIT:1> ] <EOL> self . timeslider_start = self . spec . columns [ <NUM_LIT:0> ] <EOL> self . timeslider_end = self . spec . columns [ - <NUM_LIT:1> ] <EOL> self . timestep = <NUM_LIT:10> <EOL> self . timespacing = <NUM_LIT:1> <EOL> self . _color_state = False <EOL> self . _FREEZE = False <EOL> self . draw ( name , old , new ) <EOL> def _autoupdate_changed ( self , name , old , new ) : <EOL> if self . autoupdate == True : <EOL> self . draw ( name , old , new ) <EOL> def _norm_unit_changed ( self , name , old , new ) : <EOL> self . spec_modified = self . spec_modified . as_norm ( self . NORMUNITS_REV [ new ] ) <EOL> self . draw ( name , old , new ) <EOL> def _spec_unit_changed ( self , name , old , new ) : <EOL> self . spec_modified . specunit = self . SPECUNITS_REV [ new ] <EOL> self . draw ( name , old , new ) <EOL> def _var_unit_changed ( self , name , old , new ) : <EOL> self . spec_modified . varunit = self . VARUNITS_REV [ new ] <EOL> self . draw ( name , old , new ) <EOL> def _iunit_changed ( self , name , old , new ) : <EOL> self . spec_modified . iunit = new <EOL> self . draw ( name , old , new ) <EOL> def _figwidth_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _figheight_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _colormap_changed ( self , name , old , new ) : <EOL> self . _color_state = False <EOL> self . draw ( name , old , new ) <EOL> def _color_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _FREEZE = True <EOL> self . colorbar = False <EOL> self . _color_state = True <EOL> self . _FREEZE = False <EOL> self . draw ( ) <EOL> def _colorbar_changed ( self , name , old , new ) : <EOL> self . _color_state = False <EOL> self . draw ( name , old , new ) <EOL> def _interactive_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _kind_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _selectlines_changed ( self , name , old , new ) : <EOL> if self . interactive : <EOL> self . draw ( name , old , new ) <EOL> def _file_name_changed ( self ) : <EOL> try : <EOL> self . spec = getattr ( skspec . data , self . file_name ) ( ) <EOL> except AttributeError : <EOL> pass <EOL> @ log_message <EOL> def save_plot ( self ) : <EOL> self . fig_old . savefig ( self . save_spec_as + '<STR_LIT>' ) <EOL> @ log_message <EOL> def save_to_ns ( self ) : <EOL> get_ipython ( ) . user_ns [ self . save_spec_as ] = self . spec_modified <EOL> @ log_message <EOL> def load_from_ns ( self ) : <EOL> self . spec = get_ipython ( ) . user_ns [ self . file_name ] . as_varunit ( "<STR_LIT:s>" ) <EOL> def _specslice_position_start_changed ( self , name , old , new ) : <EOL> if not self . _FREEZE : <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> def _specslice_position_end_changed ( self , name , old , new ) : <EOL> if not self . _FREEZE : <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> def _timeslice_position_start_changed ( self , name , old , new ) : <EOL> if not self . _FREEZE : <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> def _timeslice_position_end_changed ( self , name , old , new ) : <EOL> if not self . _FREEZE : <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> def _timespacing_changed ( self , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> if self . timespacing < <NUM_LIT:1> : <EOL> self . timespacing = <NUM_LIT:1> <EOL> elif self . timespacing > self . spec_modified . shape [ <NUM_LIT:1> ] : <EOL> self . timespacing = self . spec_modified . shape [ <NUM_LIT:1> ] <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> def _specspacing_changed ( self , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> if self . specspacing < <NUM_LIT:0> : <EOL> self . specspacing = <NUM_LIT:0> <EOL> elif self . specspacing > self . spec_modified . shape [ <NUM_LIT:0> ] : <EOL> self . specspacing = self . spec_modified . shape [ <NUM_LIT:0> ] <EOL> self . slice_spectrum ( name ) <EOL> self . draw ( name , old , new ) <EOL> @ log_message <EOL> def slice_spectrum ( self , name = None ) : <EOL> """<STR_LIT>""" <EOL> self . spec_modified = self . spec . nearby [ self . specslice_position_start : self . specslice_position_end : self . specspacing , <EOL> self . timeslice_position_start : self . timeslice_position_end : self . timespacing ] <EOL> @ log_message <EOL> def apply_userf ( self , name = None ) : <EOL> import numpy as np <EOL> self . spec_modified = self . spec_modified . apply ( eval ( self . user_f ) ) <EOL> self . draw ( name ) <EOL> @ log_message <EOL> def draw ( self , name = None , old = None , new = None ) : <EOL> if name is not None and self . DONT_DRAW . match ( name ) : <EOL> return <EOL> if self . _FREEZE : <EOL> return <EOL> plot_and_message = '<STR_LIT>' <EOL> if self . autoupdate : <EOL> f = plt . figure ( figsize = ( self . figwidth , self . figheight ) ) <EOL> if PLOTPARSER . is_3d ( self . kind ) : <EOL> projection = '<STR_LIT>' <EOL> else : <EOL> projection = None <EOL> ax = f . add_subplot ( <NUM_LIT> , projection = projection ) <EOL> if self . _color_state or self . kind not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> colorkwags = dict ( color = self . color ) <EOL> else : <EOL> colorkwags = dict ( cmap = self . colormap , cbar = self . colorbar ) <EOL> self . spec_modified . plot ( ax = ax , <EOL> fig = f , <EOL> kind = self . kind , <EOL> norm = self . NORMUNITS_REV [ self . norm_unit ] , <EOL> ** colorkwags <EOL> ) <EOL> f . tight_layout ( ) <EOL> lines = ax . get_lines ( ) <EOL> plt . close ( f ) <EOL> if self . interactive : <EOL> import mpld3 <EOL> if self . selectlines : <EOL> from line_plugin import HighlightLines <EOL> for idx , col in enumerate ( self . spec_modified . columns ) : <EOL> name = '<STR_LIT>' % ( idx , col ) <EOL> tooltip = mpld3 . plugins . LineLabelTooltip ( lines [ idx ] , name ) <EOL> mpld3 . plugins . connect ( f , tooltip ) <EOL> mpld3 . plugins . connect ( f , HighlightLines ( lines ) ) <EOL> plot_and_message += mpld3 . fig_to_html ( f ) <EOL> else : <EOL> plot_and_message += mpl2html ( f ) <EOL> self . fig_old = f <EOL> else : <EOL> plot_and_message += html_figure ( self . fig_old ) <EOL> self . value = plot_and_message <EOL> class CorrModel ( SpectraModel ) : <EOL> """<STR_LIT:U+0020>""" <EOL> corr2d = Any <EOL> corr3d = Any <EOL> contour3d = Any <EOL> plottype = Enum ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , sync = True ) <EOL> plot3d = Enum ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ] ) <EOL> scalea = Float ( sync = True ) <EOL> scaleb = Float ( <NUM_LIT:1> , sync = True ) <EOL> fill = Bool ( False , sync = True ) <EOL> specscale_position_start = Float ( sync = True ) <EOL> specscale_position_end = Float ( sync = True ) <EOL> specscale_start = Float ( sync = True ) <EOL> specscale_end = Float ( sync = True ) <EOL> specstep = Float ( sync = True ) <EOL> specspacing = Int ( <NUM_LIT:1> , sync = True ) <EOL> def _spec_modified_changed ( self ) : <EOL> self . corr2d = Corr2d ( self . spec_modified ) <EOL> def _spec_corrmodified_changed ( self ) : <EOL> self . corr3d = Corr2d ( self . spec_modified ) <EOL> def _spec_contourmodified_changed ( self ) : <EOL> self . contour3d = Corr2d ( self . spec_modified ) <EOL> def _plottype_default ( self ) : <EOL> return '<STR_LIT>' <EOL> def _plottype_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _plot3d_default ( self ) : <EOL> return '<STR_LIT>' <EOL> def _plot3d_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> def _scalea_changed ( self , name , old , new ) : <EOL> self . corr2d . scale ( alpha = self . scalea , beta = self . scaleb ) <EOL> self . draw ( name , old , new ) <EOL> def _scaleb_changed ( self , name , old , new ) : <EOL> self . corr2d . scale ( alpha = self . scalea , beta = self . scaleb ) <EOL> self . draw ( name , old , new ) <EOL> def _fill_changed ( self , name , old , new ) : <EOL> self . draw ( name , old , new ) <EOL> print self . corr2d . sync <EOL> def draw ( self , name = None , old = None , new = None ) : <EOL> if name is not None and self . DONT_DRAW . match ( name ) : <EOL> return <EOL> if self . _FREEZE : <EOL> return <EOL> plot_and_message = '<STR_LIT>' <EOL> if self . autoupdate : <EOL> f = plt . figure ( figsize = ( self . figwidth , self . figheight ) ) <EOL> if self . plot3d in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> projection = '<STR_LIT>' <EOL> else : <EOL> projection = None <EOL> ax = f . add_subplot ( <NUM_LIT> , projection = projection ) <EOL> if self . _color_state or self . kind not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> colorkwags = dict ( color = self . color ) <EOL> else : <EOL> colorkwags = dict ( cmap = self . colormap , cbar = self . colorbar ) <EOL> f . tight_layout ( ) <EOL> lines = ax . get_lines ( ) <EOL> pltkwds = dict ( kind = self . plot3d , <EOL> fill = self . fill , <EOL> fig = f ) <EOL> pltkwds . update ( colorkwags ) <EOL> if projection == '<STR_LIT>' : <EOL> pltkwds [ '<STR_LIT>' ] = ax <EOL> pltkwds [ '<STR_LIT>' ] = dict ( fill = self . fill ) <EOL> getattr ( self . corr2d , self . plottype ) . plot ( ** pltkwds ) <EOL> plt . close ( f ) <EOL> if self . interactive : <EOL> import mpld3 <EOL> if self . selectlines : <EOL> from line_plugin import HighlightLines <EOL> for idx , col in enumerate ( self . spec_modified . columns ) : <EOL> name = '<STR_LIT>' % ( idx , col ) <EOL> tooltip = mpld3 . plugins . LineLabelTooltip ( lines [ idx ] , name ) <EOL> mpld3 . plugins . connect ( f , tooltip ) <EOL> mpld3 . plugins . connect ( f , HighlightLines ( lines ) ) <EOL> plot_and_message += mpld3 . fig_to_html ( f ) <EOL> else : <EOL> plot_and_message += mpl2html ( f ) <EOL> self . fig_old = f <EOL> else : <EOL> plot_and_message += html_figure ( self . fig_old ) <EOL> self . value = plot_and_message </s>
