diff scripts/interactive_plot.py @ 105:edd82eb89b4b branch-tests tip

Merge
author Maria Panteli
date Sun, 15 Oct 2017 13:36:59 +0100
parents 0768d0589e7b
children
line wrap: on
line diff
--- a/scripts/interactive_plot.py	Sun Oct 15 13:36:52 2017 +0100
+++ b/scripts/interactive_plot.py	Sun Oct 15 13:36:59 2017 +0100
@@ -7,7 +7,8 @@
 
 import numpy as np
 import matplotlib.pyplot as plt
-from bokeh.models import HoverTool, TapTool, CustomJS, Title, Label
+from bokeh.models import HoverTool, TapTool, CustomJS, Title, Label#, OpenURL, Button
+#from bokeh.io import show, vform
 from bokeh.plotting import figure, show, save, output_file, ColumnDataSource
 from mpl_toolkits.basemap import Basemap
 from shapely.geometry import Point, Polygon
@@ -124,7 +125,8 @@
     circle_color = np.repeat('grey', repeats=len(y_pred))
     circle_color[y_pred] = 'red'
 
-    bl_inds = np.where(np.isnan(df['BuyLinkTrackDownload']))[0]
+    #bl_inds = np.where(np.isnan(df['BuyLinkTrackDownload']))[0]
+    bl_inds = np.where(df['Collection']=='British Library')[0]
     collection = np.repeat('Smithsonian Folkways', len(df))
     collection[bl_inds] = 'British Library'
 
@@ -142,16 +144,18 @@
         name=df['Country'].get_values(),
         color=circle_color,
         alpha=alpha_color,
-        info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre_Album'].get_values()),
+        #info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre_Album'].get_values()),
+        info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre'].get_values()),
         #outlierMD=[str(y_pred[i])+'('+str(int(MD[i]))+')' for i in range(len(MD))],  
         outlierMD = outlier_info,
         collection = collection,
-        url=df['songurls_Album'].get_values()
+        #url=df['songurls_Album'].get_values()
+        url=df['Url'].get_values()
     ))
     
     TOOLS="wheel_zoom,box_zoom,pan,reset,save"
     
-    p = figure(tools=TOOLS, plot_width=1200, title="Outlier recordings per country. Click on each point to be redirected to the Smithsonian Folkways or British Library website to listen to the audio.")
+    p = figure(tools=TOOLS, plot_width=1200, title="Outlier recordings per country (click on each point to listen to the audio). More info at: github.com/mpanteli/music-outliers/tree/master/demo/README.md.")
     outlier_ind = np.argmax(MD)
     nonoutlier_ind = np.argmin(MD)
     rleg1 = p.circle(data_x[outlier_ind], data_y[outlier_ind], fill_color='red', alpha=alpha_color[outlier_ind], size=6,
@@ -165,11 +169,31 @@
     p = add_bokeh_interactivity(p, r2, hover_outlier=True)
     p = beautify_bokeh_background(p)
     
+    #from bokeh.layouts import widgetbox
+    #button = Button(label="Button", callback=OpenURL(url='http://www.cnn.com/'), button_type="success")
+    #button = Button(label="Button", callback=CustomJS(code="""window.open('http://www.cnn.com/');"""), button_type="success")
+    #show(vform(button))
+    #button.js_on_event(events.ButtonClick, OpenURL(url="http://www.colors.commutercreative.com/"))
+    
+    #test_url = "http://www.colors.commutercreative.com/"
     #explanation = Label(x=70, y=70, x_units='screen', y_units='screen',
-    #             text='Scatter points represent recordings from each country randomly drawn within the boundaries of the country. Red points represent recordings detected as outliers and grey points as non-outliers. Hover over your mouse to get additional information of the Country, Language, Culture, Genre, and whether the recording was detected as outlier (True/False) and its Mahalanobis distance (MD). Click on each point to be redirected to the Smithsonian Folkways or British Library website to listen to the audio.', border_line_color='black', border_line_alpha=1.0,
+    #             text='For more info click here.', border_line_color='black', border_line_alpha=1.0,
     #             background_fill_color='white', background_fill_alpha=1.0)
     #p.add_layout(explanation)
+    #r3 = p.scatter(x=70, y=70, size=20)
+    #p.add_tools(TapTool(renderers=[r3], callback=OpenURL(url=test_url)))
+    #p.scatter(x=70, y=70, size=20, source=source_url)
     
+    #callback = CustomJS(args=dict(r=r3), code="""
+    #    var inds = cb_obj.get('selected')['1d'].indices;
+    #    var d1 = cb_obj.get('data');
+    #    url = d1['url'][inds[0]];
+    #    if (url){
+    #        window.open(url);}""")
+    #p.add_tools(TapTool(renderers=[r3], callback = callback))
+    #taptool = p.select(type=TapTool)
+	#taptool.callback = OpenURL(url=url)
+
     if out_file is not None:
         output_file(out_file)
         save(p)