comparison 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
comparison
equal deleted inserted replaced
104:2f6e687938e2 105:edd82eb89b4b
5 @author: mariapanteli 5 @author: mariapanteli
6 """ 6 """
7 7
8 import numpy as np 8 import numpy as np
9 import matplotlib.pyplot as plt 9 import matplotlib.pyplot as plt
10 from bokeh.models import HoverTool, TapTool, CustomJS, Title, Label 10 from bokeh.models import HoverTool, TapTool, CustomJS, Title, Label#, OpenURL, Button
11 #from bokeh.io import show, vform
11 from bokeh.plotting import figure, show, save, output_file, ColumnDataSource 12 from bokeh.plotting import figure, show, save, output_file, ColumnDataSource
12 from mpl_toolkits.basemap import Basemap 13 from mpl_toolkits.basemap import Basemap
13 from shapely.geometry import Point, Polygon 14 from shapely.geometry import Point, Polygon
14 import random 15 import random
15 from bokeh.models.widgets import Panel, Tabs 16 from bokeh.models.widgets import Panel, Tabs
122 alpha_color[y_pred==False] = 0.3 123 alpha_color[y_pred==False] = 0.3
123 124
124 circle_color = np.repeat('grey', repeats=len(y_pred)) 125 circle_color = np.repeat('grey', repeats=len(y_pred))
125 circle_color[y_pred] = 'red' 126 circle_color[y_pred] = 'red'
126 127
127 bl_inds = np.where(np.isnan(df['BuyLinkTrackDownload']))[0] 128 #bl_inds = np.where(np.isnan(df['BuyLinkTrackDownload']))[0]
129 bl_inds = np.where(df['Collection']=='British Library')[0]
128 collection = np.repeat('Smithsonian Folkways', len(df)) 130 collection = np.repeat('Smithsonian Folkways', len(df))
129 collection[bl_inds] = 'British Library' 131 collection[bl_inds] = 'British Library'
130 132
131 outlier_info = [] 133 outlier_info = []
132 for i in range(len(MD)): 134 for i in range(len(MD)):
140 x=data_x, 142 x=data_x,
141 y=data_y, 143 y=data_y,
142 name=df['Country'].get_values(), 144 name=df['Country'].get_values(),
143 color=circle_color, 145 color=circle_color,
144 alpha=alpha_color, 146 alpha=alpha_color,
145 info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre_Album'].get_values()), 147 #info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre_Album'].get_values()),
148 info = zip(df['Culture'].get_values(),df['Language'].get_values(),df['Genre'].get_values()),
146 #outlierMD=[str(y_pred[i])+'('+str(int(MD[i]))+')' for i in range(len(MD))], 149 #outlierMD=[str(y_pred[i])+'('+str(int(MD[i]))+')' for i in range(len(MD))],
147 outlierMD = outlier_info, 150 outlierMD = outlier_info,
148 collection = collection, 151 collection = collection,
149 url=df['songurls_Album'].get_values() 152 #url=df['songurls_Album'].get_values()
153 url=df['Url'].get_values()
150 )) 154 ))
151 155
152 TOOLS="wheel_zoom,box_zoom,pan,reset,save" 156 TOOLS="wheel_zoom,box_zoom,pan,reset,save"
153 157
154 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.") 158 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.")
155 outlier_ind = np.argmax(MD) 159 outlier_ind = np.argmax(MD)
156 nonoutlier_ind = np.argmin(MD) 160 nonoutlier_ind = np.argmin(MD)
157 rleg1 = p.circle(data_x[outlier_ind], data_y[outlier_ind], fill_color='red', alpha=alpha_color[outlier_ind], size=6, 161 rleg1 = p.circle(data_x[outlier_ind], data_y[outlier_ind], fill_color='red', alpha=alpha_color[outlier_ind], size=6,
158 line_color=None, selection_color="firebrick", nonselection_color='white', legend="outliers") 162 line_color=None, selection_color="firebrick", nonselection_color='white', legend="outliers")
159 rleg2 = p.circle(data_x[nonoutlier_ind], data_y[nonoutlier_ind], fill_color='grey', alpha=alpha_color[nonoutlier_ind], 163 rleg2 = p.circle(data_x[nonoutlier_ind], data_y[nonoutlier_ind], fill_color='grey', alpha=alpha_color[nonoutlier_ind],
163 selection_color="firebrick", nonselection_color='color', source=source) 167 selection_color="firebrick", nonselection_color='color', source=source)
164 168
165 p = add_bokeh_interactivity(p, r2, hover_outlier=True) 169 p = add_bokeh_interactivity(p, r2, hover_outlier=True)
166 p = beautify_bokeh_background(p) 170 p = beautify_bokeh_background(p)
167 171
172 #from bokeh.layouts import widgetbox
173 #button = Button(label="Button", callback=OpenURL(url='http://www.cnn.com/'), button_type="success")
174 #button = Button(label="Button", callback=CustomJS(code="""window.open('http://www.cnn.com/');"""), button_type="success")
175 #show(vform(button))
176 #button.js_on_event(events.ButtonClick, OpenURL(url="http://www.colors.commutercreative.com/"))
177
178 #test_url = "http://www.colors.commutercreative.com/"
168 #explanation = Label(x=70, y=70, x_units='screen', y_units='screen', 179 #explanation = Label(x=70, y=70, x_units='screen', y_units='screen',
169 # 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, 180 # text='For more info click here.', border_line_color='black', border_line_alpha=1.0,
170 # background_fill_color='white', background_fill_alpha=1.0) 181 # background_fill_color='white', background_fill_alpha=1.0)
171 #p.add_layout(explanation) 182 #p.add_layout(explanation)
172 183 #r3 = p.scatter(x=70, y=70, size=20)
184 #p.add_tools(TapTool(renderers=[r3], callback=OpenURL(url=test_url)))
185 #p.scatter(x=70, y=70, size=20, source=source_url)
186
187 #callback = CustomJS(args=dict(r=r3), code="""
188 # var inds = cb_obj.get('selected')['1d'].indices;
189 # var d1 = cb_obj.get('data');
190 # url = d1['url'][inds[0]];
191 # if (url){
192 # window.open(url);}""")
193 #p.add_tools(TapTool(renderers=[r3], callback = callback))
194 #taptool = p.select(type=TapTool)
195 #taptool.callback = OpenURL(url=url)
196
173 if out_file is not None: 197 if out_file is not None:
174 output_file(out_file) 198 output_file(out_file)
175 save(p) 199 save(p)
176 #show(p) 200 #show(p)
177 return p 201 return p