import numpy as np
from blaze import *
from bokeh.plotting import *
from bokeh.objects import HoverTool, ColumnDataSource
from bokeh.embed import *
from bokeh.resources import Resources
from collections import OrderedDict

from toolz import concat
import itertools

"""
Run this script AFTER you have done make html in the same directory you run make html.
It should automatically put the graph where it needs to be.
"""

capabilities = {
    'Streaming Python':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': False,
    },
    'Pandas':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': True,
    },
    'SQL':{
        'Scalar Expressions':"Generally yes, but math is limited in SQLite",
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':False,
        'Indices':True,
        'Column Store': False,
    },
    'PySpark':{
        'Scalar Expressions':True,
        'Reductions':True,
        'Selections':True,
        'Split-Apply-Combine':True,
        'Join':True,
        'Python Functions':True,
        'Indices':False,
        'Column Store': False,
    },
    'MongoDB':{
        'Scalar Expressions':False,
        'Reductions':True,
        'Selections':"Limited due to lack of scalar expressions",
        'Split-Apply-Combine':"Limited",
        'Join':False,
        'Python Functions':False,
        'Indices':True,
        'Column Store': False,
    },
    'PyTables':{
        'Scalar Expressions': "Using NumExpr",
        'Reductions': True,
        'Selections': "Using NumExpr",
        'Split-Apply-Combine':"Defaults to Streaming Python",
        'Join':False,
        'Python Functions':"Using streaming Python",
        'Indices':True,
        'Column Store': False,
    },
    'BColz':{
        'Scalar Expressions':"Using chunked NumPy",
        'Reductions':"Using chunked NumPy",
        'Selections':"Using NumExpr",
        'Split-Apply-Combine':"Using streaming Python",
        'Join':False,
        'Python Functions': "Using streaming Python",
        'Indices':False,
        'Column Store': True,
    }
}

colormap = {True: "#5EDA9E", False: "#FFFFFF"}

backends = ['Streaming Python', 'Pandas', 'PySpark', 'SQL', 'PyTables', 'BColz',
            'MongoDB']
operations = ['Scalar Expressions', 'Selections', 'Reductions',
              'Split-Apply-Combine', 'Join', 'Python Functions', 'Indices', 'Column Store']

statuses = [capabilities[backend][op] for backend in backends
                                      for op in operations]

x, y = zip(*itertools.product(backends, operations))
x = list(x)
y = list(y)

colors = [colormap[1] if value else colormap[0] for value in statuses]

reset_output()

output_file('build/html/capabilities.html', mode='cdn')


source = ColumnDataSource(
    data=dict(
        xname=x,
        yname=y,
        statuses=statuses,
        colors=colors,
    )
)

figure()

rect('xname', 'yname', 0.99, 0.99, source=source,
     x_range=backends, y_range=list(reversed(operations)),
     x_axis_location="above",
     color='colors', line_color=None,
     tools="hover,previewsave", title="Blaze Capabilities by Backend",
     plot_width=800, plot_height=600)

grid().grid_line_color = None
axis().axis_line_color = None
axis().major_tick_line_color = None
axis().major_label_text_font_size = "5pt"
axis().major_label_standoff = 0

hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0]
hover.tooltips = OrderedDict([
    ('names', '@yname, @xname'),
    ('status', '@statuses'),
])

save()      # show the plot'

@@ -688,31 +688,85 @@
 re': False,%0A
+        'Bigger-than-memory data': %22Using Streaming%22,%0A
     %7D,%0A
-
     'Pandas'
@@ -998,24 +998,66 @@
 ore': True,%0A
+        'Bigger-than-memory data': False,%0A
     %7D,%0A    '
@@ -1332,32 +1332,73 @@
  Store': False,%0A
+        'Bigger-than-memory data': True,%0A
     %7D,%0A    'PySp
@@ -1631,32 +1631,73 @@
  Store': False,%0A
+        'Bigger-than-memory data': True,%0A
     %7D,%0A    'Mong
@@ -1976,32 +1976,73 @@
  Store': False,%0A
+        'Bigger-than-memory data': True,%0A
     %7D,%0A    'PyTa
@@ -2198,19 +2198,13 @@
 e':%22
-Defaults to
+Using
  Str
@@ -2341,32 +2341,98 @@
  Store': False,%0A
+        'Bigger-than-memory data': %22With fast compressed access%22,%0A
     %7D,%0A    'BCol
@@ -2753,24 +2753,90 @@
 ore': True,%0A
+        'Bigger-than-memory data': %22With fast compressed access%22,%0A
     %7D%0A%7D%0A%0Acol
@@ -3123,30 +3123,71 @@
 ndices',
- 'Column Store
+%0A              'Column Store', 'Bigger-than-memory data
 '%5D%0A%0Astat

add bigger than memory data to capabilities