In [65]:
from bokeh.charts import Donut, output_file, show
from lightcurve_pipeline.utils.utils import SETTINGS
from lightcurve_pipeline.database.database_interface import session
from lightcurve_pipeline.database.database_interface import Metadata

In [64]:
# from bokeh.charts import Donut, output_file, show

# # dict, OrderedDict, lists, arrays and DataFrames are valid inputs
xyvalues = [[1., 5., 10.], [1., 5., 10.], [1., 5., 10.], [0., 5., 10.]]

donut = Donut(xyvalues, ['cpu1', 'cpu2', 'cpu3'])

output_file('donut.html')
show(donut)

In [53]:
# Query for data
query = session.query(Metadata.instrume, Metadata.opt_elem).all()
instrumes = [result[0] for result in query]
opt_elems = [result[1] for result in query]
opt_elems_set = set(opt_elems)

In [54]:
# Initialize dictionaries that store all optical elements
cos_dict, stis_dict = {}, {}
for opt_elem in opt_elems_set:
    cos_dict[opt_elem] = 0
    stis_dict[opt_elem] = 0

In [55]:
# Count number of opt_elems
for instrument, opt_elem in zip(instrumes, opt_elems):
    if instrument == 'COS':
        cos_dict[opt_elem] += 1
    elif instrument == 'STIS':
        stis_dict[opt_elem] += 1

In [56]:
values = [cos_dict.values(), stis_dict.values()]
labels = ['COS', 'STIS']


Out[56]:
[[0, 914, 0, 0, 0, 273, 0, 0, 0, 0, 2040, 1745, 242, 107, 28, 0, 323, 0, 251],
 [150, 715, 126, 31, 319, 0, 22, 51, 295, 8, 0, 0, 0, 0, 0, 18, 132, 76, 0]]

In [61]:
help(Donut)


Help on function Donut in module bokeh.charts.builder.donut_builder:

Donut(values, cat=None, width=800, height=800, xgrid=False, ygrid=False, **kws)
    Creates a Donut chart using  :class:`DonutBuilder <bokeh.charts.builder.donut_builder.DonutBuilder>`
    to render the geometry from values and cat.
    
    Args:
        values (iterable): iterable 2d representing the data series
            values matrix.
        cat (list or bool, optional): list of string representing the categories.
            Defaults to None.
    
    In addition the the parameters specific to this chart,
    :ref:`userguide_charts_generic_arguments` are also accepted as keyword parameters.
    
    Returns:
        a new :class:`Chart <bokeh.charts.Chart>`
    
    Examples:
    
    .. bokeh-plot::
        :source-position: above
    
        from bokeh.charts import Donut, output_file, show
    
        # dict, OrderedDict, lists, arrays and DataFrames are valid inputs
        xyvalues = [[2., 5., 3.], [4., 1., 4.], [6., 4., 3.]]
    
        donut = Donut(xyvalues, ['cpu1', 'cpu2', 'cpu3'])
    
        output_file('donut.html')
        show(donut)


In [50]:
# Make plot
output_file('opt_elems.html')
donut = Donut(values, labels)
# show(donut)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-50-67ee49c6bd67> in <module>()
      1 # Make plot
      2 output_file('opt_elems.html')
----> 3 donut = Donut(values, labels)
      4 # show(donut)

/Users/bourque/anaconda/lib/python2.7/site-packages/bokeh/charts/builder/donut_builder.pyc in Donut(values, cat, width, height, xgrid, ygrid, **kws)
     66     return create_and_build(
     67         DonutBuilder, values, cat=cat, width=width, height=height,
---> 68         xgrid=xgrid, ygrid=ygrid, **kws
     69     )
     70 

/Users/bourque/anaconda/lib/python2.7/site-packages/bokeh/charts/_builder.pyc in create_and_build(builder_class, values, **kws)
     40     chart_kws = { k:v for k,v in kws.items() if k not in builder_props}
     41     chart = Chart(**chart_kws)
---> 42     chart.add_builder(builder)
     43 
     44     return chart

/Users/bourque/anaconda/lib/python2.7/site-packages/bokeh/charts/_chart.pyc in add_builder(self, builder)
    116     def add_builder(self, builder):
    117         self._builders.append(builder)
--> 118         builder.create(self)
    119 
    120         # Add tools if supposed to

/Users/bourque/anaconda/lib/python2.7/site-packages/bokeh/charts/_builder.pyc in create(self, chart)
    163     def create(self, chart=None):
    164         self._adapt_values()
--> 165         self._process_data()
    166         self._set_sources()
    167         renderers = self._yield_renderers()

/Users/bourque/anaconda/lib/python2.7/site-packages/bokeh/charts/builder/donut_builder.pyc in _process_data(self)
     96         dd = dict(zip(self._values.keys(), self._values.values()))
     97         self._df = df = pd.DataFrame(dd)
---> 98         self._groups = df.index = self.cat
     99         df.columns = self._values.keys()
    100 

/Users/bourque/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __setattr__(self, name, value)
   2159         try:
   2160             object.__getattribute__(self, name)
-> 2161             return object.__setattr__(self, name, value)
   2162         except AttributeError:
   2163             pass

pandas/src/properties.pyx in pandas.lib.AxisProperty.__set__ (pandas/lib.c:42548)()

/Users/bourque/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in _set_axis(self, axis, labels)
    411 
    412     def _set_axis(self, axis, labels):
--> 413         self._data.set_axis(axis, labels)
    414         self._clear_item_cache()
    415 

/Users/bourque/anaconda/lib/python2.7/site-packages/pandas/core/internals.pyc in set_axis(self, axis, new_labels)
   2217         if new_len != old_len:
   2218             raise ValueError('Length mismatch: Expected axis has %d elements, '
-> 2219                              'new values have %d elements' % (old_len, new_len))
   2220 
   2221         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 19 elements, new values have 2 elements

In [ ]: