In [79]:
%matplotlib inline
import pandas as pd
import numpy as np
from collections import OrderedDict
from bokeh.plotting import *
from bokeh.models import HoverTool

In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import clear_output, display, HTML

In [3]:
output_notebook()


BokehJS successfully loaded.

In [4]:
%connect_info


{
  "stdin_port": 61728, 
  "ip": "127.0.0.1", 
  "control_port": 61729, 
  "hb_port": 61730, 
  "signature_scheme": "hmac-sha256", 
  "key": "d32bf9cd-c0a6-496a-b829-f0b6d6fefbfc", 
  "shell_port": 61726, 
  "transport": "tcp", 
  "iopub_port": 61727
}

Paste the above JSON into a file, and connect with:
    $> ipython <app> --existing <file>
or, if you are local, you can connect with just:
    $> ipython <app> --existing kernel-d1257959-b06b-4687-bc5b-3713761962b9.json 
or even just:
    $> ipython <app> --existing 
if this is the most recent IPython session you have started.

In [60]:
data = pd.read_csv('data/FPD-non-redundant-processed.csv', delimiter='\t')

In [69]:
data.ix[:5, 3]


Out[69]:
0    SYG
1    AYG
2    CYG
3    LYG
4    TYG
5    SFG
Name: Chromophore Class, dtype: object

In [108]:
TOOLS = "pan,wheel_zoom,box_zoom,reset,hover"

class Console(object):
    
    def __init__(self, data):
        self.data = data
        self.chrph = sorted(self.data.ix[:,3])[0]  # set a default chromophore
        
        '''create a dropdown list for all chromophores'''
        
        # create a list of unique chromophores
        self.chromophores = set(self.data.ix[:,3])
        self.chrph_drop_list = widgets.DropdownWidget(
                                    values=dict(zip(self.chromophores, self.chromophores)),
                                    value=self.chrph,
                                    description='Chromophores',
                                )
        display(self.chrph_drop_list)
        self.chrph_drop_list.on_trait_change(self.on_select_chrph, 'value')
        self.display_data(self.filter_data_by_chrph())
        
        '''create a slider for IDs'''
        self.excitation_slider = widgets.IntSliderWidget(description='Excitation')
        display(self.excitation_slider)
        self.adjust_excitation_slider()
        
        self.excitation_slider.on_trait_change(self.on_excitation_slider_change, 'value')
    
    def display_data(self, filtered_data):
        '''Display molecules with a select chromophore'''
        
        clear_output()
        display(HTML(filtered_data.ix[:,[0,1,3,4,-4,-2]].to_html()))

    def on_select_chrph(self, name, value):    
        self.chrph = value
        self.display_data(self.filter_data_by_chrph())
        self.adjust_excitation_slider()

    def adjust_excitation_slider(self):
        filtered_data = self.filter_data_by_chrph()
        self.excitation_slider.min = min(filtered_data['excitation_new'])
        self.excitation_slider.max = max(filtered_data['excitation_new'])
        self.excitation_slider.value = self.excitation_slider.min
    
    def filter_data_by_chrph(self):
        filtered_data = self.data[self.data.ix[:,3] == self.chrph]
        return filtered_data
    
    def filter_data_by_chrph_and_pid(self, min_value):
        filtered_data = self.filter_data_by_chrph()
        filtered_data = filtered_data[
                            filtered_data['excitation_new'].notnull()]
        filtered_data = filtered_data[
                            filtered_data['excitation_new'] >= min_value]
        return filtered_data
    
    def plot_excitation_emission(self, filtered_data):
        clear_output()
        p = figure(title=self.chrph, tools=TOOLS)
        d = filtered_data.ix[:, ['excitation_new', 'emission_new']].dropna()
        excitation = np.asarray(d['excitation_new'])
        emission = np.asarray(d['emission_new'])
        excitation_colors = np.asarray(filtered_data.ix[d.index,]['excitation_color_new'])
        d_ = filtered_data.ix[d.index,]
        source = ColumnDataSource(
                        data=dict(
                                    fpid=d_['FPID'],
                                    excitation_color_class=d_['Excitation Color Class'],
                                    emission_color_class=d_['Emission Color Class'],
                                    chromophore=d_['Chromophore Name'],
                                    excitation_new=d_['excitation_new'],
                                    excitation_alt=d_['excitation_alt'],
                                    emission_new=d_['emission_new'],
                                    emission_alt=d_['emission_alt'],
                                ))
        
        #values = OrderedDict([('Excitation',excitation),
        #                      ('Emission', emission)])
        p.circle(x=excitation, y=emission, source=source, size=10, alpha=0.4,
               fill_color=excitation_colors,
               fill_alpha=0.6,
               line_color='#000000',
               title=self.chrph)
        #scatter = Scatter(values)
        #scatter.title("Excitation vs Emission").xlabel("Excitation").ylabel("Emission")
        hover = p.select(dict(type=HoverTool))
        hover.tooltips = [
            ("FPID", "@fpid"),
            ("Chromophore", "@chromophore"),
            ("Excitation Color Class", "@excitation_color_class"),
            ("Emission color class", "@emission_color_class"),
            ("Primary excitation", "@excitation_new"),
            ("Secondary excitation", "@excitation_alt"),
            ("Primary emission", "@emission_new"),
            ("Secondary emission", "@emission_alt"),
        ]
        show(p)
        
    def on_excitation_slider_change(self, name, value):
        '''Show records with ID greater than a given ID'''

        #self.display_data(self.filter_data_by_chrph_and_pid(value))
        self.plot_excitation_emission(self.filter_data_by_chrph_and_pid(value))

In [109]:
# GSG fails (missing excitation and emission)

FPD Viewer

Select data by chromophores and excitation wavelength (nm)


In [110]:
d = Console(data)



In [ ]: