In [2]:
import pest_tools as pt
import matplotlib
%matplotlib inline
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.utils.traitlets import Any, Bool, Dict, List, Unicode
from threading import Lock

MultiSelect Widet


In [4]:
class MultipleSelectWidget(widgets.DOMWidget):
    _view_name = Unicode('MultipleSelectView', sync=True)
    
    value = List(sync=True)
    values = Dict(sync=True)
    values_order = List(sync=True)
    description = Unicode(sync=True)

    def __init__(self, *args, **kwargs):
        self.value_lock = Lock()

        self.values = kwargs.get('values', [])
        self.value = kwargs.get('value', [])
        self.values_order = kwargs.get('values_order', [])
            
        widgets.DOMWidget.__init__(self, *args, **kwargs)

In [5]:
%%javascript
require(["widgets/js/widget"], function(WidgetManager){
    var MultipleSelectView = IPython.DOMWidgetView.extend({
        initialize: function(parameters) {
            this.model.on('change',this.update,this);
            this.options = parameters.options;
            this.child_views = [];
            // I had to override DOMWidgetView's initialize to set model.views otherwise
            // multiple views would get attached to the model
            this.model.views = [this];
        },
        
        render : function(){
            this.$el
                .addClass('widget-hbox');
            this.$label = $('<div />')
                .appendTo(this.$el)
                .addClass('widget-hlabel')
                .hide();
            this.$listbox = $('<select/>')
                .addClass('widget-listbox')
                .attr('multiple', '')
                .attr('size', 6)
                .appendTo(this.$el);
            this.$el_to_style = this.$listbox;
            this.update();
        },
        
        update : function(options){
            if (typeof(options) === 'undefined' || options.updated_view != this) {
                var values = this.model.get('values');
                var values_order = this.model.get('values_order');
                
                var that = this;
                _.each(values_order, function(key, index) {
                    if (that.$listbox.find('option[key="' + key + '"]').length === 0) {
                        $('<option />')
                            .text(values[key])
                            .attr('key', key)
                            .appendTo(that.$listbox)
                            .on('click', $.proxy(that.handle_click, that));
                    }                    
                });
                
                var value = this.model.get('value') || [];
                
                this.$listbox.find('option').each(function(index, element) {
                    var key = $(element).attr('key');
                    
                    if (key in values) {
                        if (value.indexOf(key) != -1) {
                            $(element).prop('selected', true);
                        }
                    } else {
                        $(element).remove();
                    }
                });

                var description = this.model.get('description');
                if (description.length === 0) {
                    this.$label.hide();
                } else {
                    this.$label.text(description);
                    this.$label.show();
                }
            }
            return MultipleSelectView.__super__.update.apply(this);
        },

        handle_click: function (event) {
            var value = $(event.target).parent().children('option:selected').map(function() { return $(this).attr('key') }).get()
            
            this.model.set('value', value, {updated_view: this});
            this.touch();
        },    
    });
    WidgetManager.register_widget_view('MultipleSelectView', MultipleSelectView);
});


Adjust some matplotlib defaults.
Change figsize so plots are larger in the notebook and change the defualt cmap. Use ggplot style


In [6]:
matplotlib.rcParams.update({'figure.figsize': [14,8], 'image.cmap' : 'cubehelix_r'})
matplotlib.style.use('ggplot')

Load some example data. Assumes this notebook is in the same directory as the example data included on GitHub


In [7]:
res = pt.Res(r'example.res')
jco_df, par_names, obs_names = pt.load_jco(r'example.jco')
pars_dict = pt.load_pars(r'example.pst')
obs_dict = pt.load_obs(r'example.pst')
par_sen = pt.ParSen(jco_df, obs_dict, pars_dict)
ob_sen = pt.ObSen(jco_df, obs_dict)
par_groups = []
for i in pars_dict:
    group = pars_dict[i][5]
    if group not in par_groups:
        par_groups.append(group)


0.0 Percent; Record 0 of 1036222 
96.5 Percent; Record 1000000 of 1036222 
C:\Anaconda\lib\site-packages\pandas\io\parsers.py:615: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators; you can avoid this warning by specifying engine='python'.
  ParserWarning)

Interactive example of measured vs model plot. When first loaded all groups will be selected. Use Ctrl click to select multiple groups


In [9]:
interactive(res.plot_measure_vs_model, groups = MultipleSelectWidget(value = res.groups, values = dict(zip(res.groups, res.groups)), values_order=res.groups), plot_type = ('scatter', 'hexbin'))



In [10]:
interactive(par_sen.plot, group = par_groups, n = (1,50))



In [11]:
interactive(par_sen.plot, group = fixed(None), n = (1,50))



In [12]:
res.plot_objective_contrib()


Load differnt res and jco that is better for correlation plotting


In [14]:
res2 = pt.Res('cor_testing.res')
jco_df2, par_names2, obs_names2 = pt.load_jco(r'cor_testing.jco')
cor = pt.Cor(jco_df2, res2.df)

Correlation plotting needs some work.


In [16]:
interactive(cor.plot_img_with_dendrograms, use_abs_cor = True)



In [17]:
interactive(cor.plot_dendrogram, method = ('complete','single','average','weighted'), metric = fixed('euclidean'))



In [14]:


In [14]: