def heatmap_tab(df_means, df_stdev, Time, Treatments, number_cmpds_run): Cmpd0 = df_means.columns[len(Treatments) + len(Time)] cmpd_options = cmpd_options_func(df_means, len(Treatments) + len(Time), number_cmpds_run) df_means = df_means.reindex(index=order_by_index( df_means.index, index_natsorted(df_means[Time[0]]))) if len(Treatments) == 4: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') df_means[Treatments[3]] = df_means[Treatments[3]].astype('str') elif len(Treatments) == 3: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') elif len(Treatments) == 2: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') elif len(Treatments) == 1: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means['Treatment'] = df_means[Time[0]].str.cat(df_means[Treatments], sep=' - ') df_stdev['Treatment'] = df_means[Time[0]].str.cat(df_means[Treatments], sep=' - ') treatments = list(df_means['Treatment']) df_m = df_means[['Treatment', Cmpd0]].copy() df_m2 = df_m.set_index(df_m[df_m.columns[0]].astype(str)) df_m2.drop(df_m.columns[0], axis=1, inplace=True) df_s = df_stdev[['Treatment', Cmpd0]].copy() df_s2 = df_s.set_index(df_s[df_s.columns[0]].astype(str)) df_s2.drop(df_s2.columns[0], axis=1, inplace=True) df_HM = pd.DataFrame(index=df_means['Treatment'], columns=df_means['Treatment']) df_HM.index.name = 'Treatment1' df_HM.columns.name = 'Treatment2' for i_t in treatments: for j_t in treatments: m1 = df_m2.loc[i_t, Cmpd0] m2 = df_m2.loc[j_t, Cmpd0] sd1 = df_s2.loc[i_t, Cmpd0] sd2 = df_s2.loc[j_t, Cmpd0] a1 = [m1 - sd1, m1 + sd1] b1 = [m2 - sd2, m2 + sd2] a2 = [m1 - 2 * sd1, m1 + 2 * sd1] b2 = [m2 - 2 * sd2, m2 + 2 * sd2] a3 = [m1 - 3 * sd1, m1 + 3 * sd1] b3 = [m2 - 3 * sd2, m2 + 3 * sd2] if getOverlap(a1, b1) > 0: marker = 'green' elif getOverlap(a2, b2) > 0: marker = 'yellow' elif getOverlap(a3, b3) > 0: marker = 'orange' else: marker = 'red' df_HM.loc[i_t, j_t] = marker hm_colors = df_HM.values.reshape(-1).tolist() t2 = treatments * len(treatments) t1 = [] for tt in treatments: for i in range(len(treatments)): t1.append(tt) source = ColumnDataSource({ 'treat1': t1, 'treat2': t2, 'colors': hm_colors }) p = figure(title="Categorical Heatmap", x_range=treatments, y_range=treatments, plot_height=1000, plot_width=1000) p.rect(x='treat1', y='treat2', color='colors', width=1, height=1, line_color='black', line_width=2, source=source) p.xaxis.major_label_orientation = np.pi / 2 select = Select(title='Select your compound:', value=Cmpd0, options=cmpd_options) def update_data(attrname, old, new): cmpd = select.value df_m = df_means[['Treatment', cmpd]].copy() df_m2 = df_m.set_index(df_m[df_m.columns[0]].astype(str)) df_m2.drop(df_m.columns[0], axis=1, inplace=True) df_s = df_stdev[['Treatment', cmpd]].copy() df_s2 = df_s.set_index(df_stdev[df_s.columns[0]].astype(str)) df_s2.drop(df_s2.columns[0], axis=1, inplace=True) df_HM = pd.DataFrame(index=df_means['Treatment'], columns=df_means['Treatment']) df_HM.index.name = 'Treatment1' df_HM.columns.name = 'Treatment2' for i_t in treatments: for j_t in treatments: m1 = df_m2.loc[i_t, cmpd] m2 = df_m2.loc[j_t, cmpd] sd1 = df_s2.loc[i_t, cmpd] sd2 = df_s2.loc[j_t, cmpd] a1 = [m1 - sd1, m1 + sd1] b1 = [m2 - sd2, m2 + sd2] a2 = [m1 - 2 * sd1, m1 + 2 * sd1] b2 = [m2 - 2 * sd2, m2 + 2 * sd2] a3 = [m1 - 3 * sd1, m1 + 3 * sd1] b3 = [m2 - 3 * sd2, m2 + 3 * sd2] if getOverlap(a1, b1) > 0: marker = 'green' elif getOverlap(a2, b2) > 0: marker = 'yellow' elif getOverlap(a3, b3) > 0: marker = 'orange' else: marker = 'red' df_HM.loc[i_t, j_t] = marker hm_colors = df_HM.values.reshape(-1).tolist() t2 = treatments * len(treatments) t1 = [] for tt in treatments: for i in range(len(treatments)): t1.append(tt) results1 = {'treat1': t1, 'treat2': t2, 'colors': hm_colors} source.data = results1 for w in [select]: w.on_change('value', update_data) # Create a row layout inputs = widgetbox(select) layout = row(inputs, p, width=1500) tab = Panel(child=layout, title='Heatmap') return tab
def bar_tab(df_means, df_stdev, Time, Treatments, number_cmpds_run): colors = [ "firebrick", "navy", 'green', 'orange', 'violet', 'lawngreen', 'powderblue', 'lightgreen', 'yellow', 'olive', 'red', 'grey', 'skyblue', 'indigo', 'slategray', 'hotpink', 'peachpuff', 'powderblue' ] Cmpd0 = df_means.columns[len(Treatments) + len(Time)] cmpd_options = cmpd_options_func(df_means, len(Treatments) + len(Time), number_cmpds_run) time_vals = df_means[Time[0]].drop_duplicates().tolist() time_vals = natsorted(time_vals) df_means = df_means.reindex(index=order_by_index( df_means.index, index_natsorted(df_means[Time[0]]))) MEANs = df_means.groupby(Treatments)[Cmpd0].apply(list).to_dict() STDs = df_stdev.groupby(Treatments)[Cmpd0].apply(list).to_dict() keys = [] u_keys = [] l_keys = [] results = {'time_vals': time_vals} for h in range(len(MEANs)): kk = list(MEANs.keys())[h][0] for tot in range(1, len(Treatments)): sk = list(MEANs.keys())[h][tot] if type(sk).__name__ != 'str': sk = str(sk) kk += '_' + sk keys.append(kk) u_keys.append('upper ' + kk) l_keys.append('lower ' + kk) mu = list(MEANs.values())[h] sd = list(STDs.values())[h] upper = [x + e for x, e in zip(mu, sd)] lower = [x - e for x, e in zip(mu, sd)] results.update({keys[h]: mu}) results.update({u_keys[h]: upper}) results.update({l_keys[h]: lower}) source = ColumnDataSource(data=results) p = figure(x_range=time_vals, plot_height=1000, plot_width=1000, title=Cmpd0, toolbar_location="right") legend_it = [] for hh in range(len(MEANs)): c = p.vbar(x=dodge('time_vals', -0.4 + (.8 * hh / len(MEANs)), range=p.x_range), top=keys[hh], width=(0.8 / len(MEANs)), source=source, color=colors[hh]) p.add_layout( Whisker(source=source, base=dodge('time_vals', -0.4 + (.8 * hh / len(MEANs)), range=p.x_range), upper=u_keys[hh], lower=l_keys[hh], level="overlay")) legend_it.append((keys[hh], [c])) legend = Legend(items=legend_it, location=(0, 0)) legend.click_policy = "mute" p.add_layout(legend, 'right') p.x_range.range_padding = 0.1 p.xgrid.grid_line_color = None p.legend.orientation = "vertical" #This is where the widget is setup select = Select(title='Select your compound:', value=Cmpd0, options=cmpd_options) select_sd = Select(title="Standard Deviation:", value='1', options=['0', '1', '2', '3']) def update_title(attrname, old, new): p.title.text = select.value select.on_change('value', update_title) def update_data(attrname, old, new): cmpd = select.value std = int(select_sd.value) MEANs = df_means.groupby(Treatments)[cmpd].apply(list).to_dict() STDs = df_stdev.groupby(Treatments)[cmpd].apply(list).to_dict() results1 = {'time_vals': time_vals} for y in range(len(MEANs)): mu = list(MEANs.values())[y] sd = list(STDs.values())[y] upper = [x + std * e for x, e in zip(mu, sd)] lower = [x - std * e for x, e in zip(mu, sd)] results1.update({keys[y]: mu}) results1.update({u_keys[y]: upper}) results1.update({l_keys[y]: lower}) source.data = results1 for w in [select, select_sd]: w.on_change('value', update_data) # Set up layouts and add to document inputs = widgetbox(select, select_sd) layout = row(column(inputs), p, width=1000) tab = Panel(child=layout, title='Bar Charts') return tab
def intervals_tab(df_means,df_stdev, Time, Treatments ,number_cmpds_run): len_t=len(Treatments) time_vals=df_means[Time[0]].drop_duplicates().tolist() time_vals=natsorted(time_vals) df_means=df_means.reindex(index=order_by_index(df_means.index, index_natsorted(df_means[Time[0]]))) cmpd_options=cmpd_options_func(df_means,len_t+1,number_cmpds_run) if len(Treatments) == 4: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') df_means[Treatments[3]] = df_means[Treatments[3]].astype('str') elif len(Treatments) == 3: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') elif len(Treatments) == 2: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') elif len(Treatments) == 1: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') def_cmpds=[] for i in range(5): def_cmpds.append(df_means.columns[len_t+1+i]) df_means['Treatment'] = df_means[Time[0]].str.cat(df_means[Treatments], sep=' - ') treats=df_means['Treatment'].tolist() xs=[] ys=[] delta=0 err_xs_t = [] err_ys_t = [] results={} for cc, cmpd in enumerate(def_cmpds): xs.append(np.linspace(0+delta,(len(treats)-1)+delta,len(treats))) delta=delta+.2 ys.append(df_means[cmpd]) yerrs=df_stdev[cmpd].tolist() err_xs = [] err_ys = [] for x, y, yerr in zip(xs[cc], ys[cc], yerrs): err_xs.append((x, x)) err_ys.append((y - yerr, y + yerr)) err_xs_t.append(err_xs) err_ys_t.append(err_ys) results.update({'x_'+str(cc) : xs[cc]}) results.update({'y_'+str(cc) : ys[cc]}) results.update({'xe_'+str(cc) : err_xs_t[cc]}) results.update({'ye_'+str(cc) : err_ys_t[cc]}) source = ColumnDataSource(data=results) pt_colors=['red','blue','green','black','orange'] ln_colors=['lightcoral','lightblue','lightgreen','gray','peachpuff'] p = figure(title='Simple bar chart', plot_height=1000, plot_width=1000) for cc, cmpd in enumerate(def_cmpds): l_str='Opt'+str(int(cc+1)) p.multi_line(xs='xe_'+str(cc), ys='ye_'+str(cc), color=ln_colors[cc],line_width=6, line_alpha=.8,source=source) p.circle('x_'+str(cc), 'y_'+str(cc), color=pt_colors[cc], size=10, line_alpha=0,legend=l_str,source=source) x_dict={} for x,tr in zip(xs[1],treats): x_dict.update({int(x):tr}) p.xaxis.ticker = xs[0] p.xaxis.major_label_overrides = x_dict p.xaxis.major_label_orientation = np.pi/2 p.xgrid[0].ticker=FixedTicker(ticks=np.arange(-.5,len(treats)-.5,len(time_vals))) #widget for pie charts select = Select(title="Standard Deviation:", value='1', options=['0','1','2','3','4']) select_c1 = Select(title='Opt1: Select your 1st compound:', value=def_cmpds[0], options=cmpd_options) select_c2 = Select(title='Opt2: Select your 2nd compound:', value=def_cmpds[1], options=cmpd_options) select_c3 = Select(title='Opt3: Select your 3rd compound:', value=def_cmpds[2], options=cmpd_options) select_c4 = Select(title='Opt4: Select your 4th compound:', value=def_cmpds[3], options=cmpd_options) select_c5 = Select(title='Opt5: Select your 5th compound:', value=def_cmpds[4], options=cmpd_options) def update_title(attrname, old, new): p.title.text = select.value+'std deviation for' + select_c1.value + ' and ' + select_c2.value+ ' and ' + select_c3.value+ ' and ' + select_c4.value+ ' and ' + select_c5.value def update_data(attrname, old, new): sd=int(select.value) xs=[] ys=[] delta=0 err_xs_t = [] err_ys_t = [] results1={} def_cmpds=[select_c1.value,select_c2.value,select_c3.value,select_c4.value,select_c5.value] for cc, cmpd in enumerate(def_cmpds): xs.append(np.linspace(0+delta,(len(treats)-1)+delta,len(treats))) delta=delta+.2 ys.append(df_means[cmpd]) yerrs=df_stdev[cmpd].tolist() err_xs = [] err_ys = [] for x, y, yerr in zip(xs[cc], ys[cc], yerrs): err_xs.append((x, x)) err_ys.append((y - sd*yerr, y + sd*yerr)) err_xs_t.append(err_xs) err_ys_t.append(err_ys) results1.update({'x_'+str(cc) : xs[cc]}) results1.update({'y_'+str(cc) : ys[cc]}) results1.update({'xe_'+str(cc) : err_xs_t[cc]}) results1.update({'ye_'+str(cc) : err_ys_t[cc]}) source.data = results1 select_c1.on_change('value', update_title) select_c2.on_change('value', update_title) select_c3.on_change('value', update_title) select_c4.on_change('value', update_title) select_c5.on_change('value', update_title) select.on_change('value', update_title) for w in [select_c1,select_c2,select_c3,select_c4,select_c5,select]: w.on_change('value', update_data) ## Set up the widgets for panel 2 inputs1 = widgetbox(select) inputs2 = widgetbox(select_c1,select_c2,select_c3,select_c4,select_c5) layout=row(column(inputs1),column(inputs2), p, width=1500) tab = Panel(child=layout,title='Intervals') return tab
def single_pie_tab(df_means, df_stdev, Time, Treatments, number_cmpds_run): len_t = len(Treatments) colors = [ "firebrick", "navy", 'green', 'orange', 'violet', 'lawngreen', 'lightgreen', 'yellow', 'olive', 'red', 'grey', 'skyblue', 'indigo', 'slategray', 'hotpink', 'peachpuff', 'powderblue' ] df_means = df_means.reindex(index=order_by_index( df_means.index, index_natsorted(df_means[Time[0]]))) cmpd_options = cmpd_options_func(df_means, len_t + 1, number_cmpds_run) time_vals = df_means[Time[0]].drop_duplicates().tolist() if len(Treatments) == 4: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') df_means[Treatments[3]] = df_means[Treatments[3]].astype('str') tm0_vals = df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals = df_means[Treatments[1]].drop_duplicates().tolist() tm2_vals = df_means[Treatments[2]].drop_duplicates().tolist() tm3_vals = df_means[Treatments[3]].drop_duplicates().tolist() df_for_pie = df_means.loc[(df_means[Time[0]] == time_vals[0]) & (df_means[Treatments[0]] == tm0_vals[0]) & (df_means[Treatments[1]] == tm1_vals[0]) & (df_means[Treatments[2]] == tm2_vals[0]) & (df_means[Treatments[3]] == tm3_vals[0])] elif len(Treatments) == 3: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') tm0_vals = df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals = df_means[Treatments[1]].drop_duplicates().tolist() tm2_vals = df_means[Treatments[2]].drop_duplicates().tolist() df_for_pie = df_means.loc[(df_means[Time[0]] == time_vals[0]) & (df_means[Treatments[0]] == tm0_vals[0]) & (df_means[Treatments[1]] == tm1_vals[0]) & (df_means[Treatments[2]] == tm2_vals[0])] elif len(Treatments) == 2: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') tm0_vals = df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals = df_means[Treatments[1]].drop_duplicates().tolist() df_for_pie = df_means.loc[(df_means[Time[0]] == time_vals[0]) & (df_means[Treatments[0]] == tm0_vals[0]) & (df_means[Treatments[1]] == tm1_vals[0])] elif len(Treatments) == 1: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') tm0_vals = df_means[Treatments[0]].drop_duplicates().tolist() df_for_pie = df_means.loc[(df_means[Time[0]] == time_vals[0]) & (df_means[Treatments[0]] == tm0_vals[0])] df_means = df_means.replace(float('nan'), 0) def_cmpds = [] for i in range(5): def_cmpds.append(df_means.columns[len_t + 1 + i]) len_cmpds = len(def_cmpds) df_pie_values = df_for_pie[def_cmpds] pie_vals = list(df_pie_values.values.flatten()) pie_fracs = [x / sum(pie_vals) for x in pie_vals] percents = [0] per_labels = [] for f in range(0, len(pie_fracs)): ff = percents[f] + pie_fracs[f] percents.append(ff) per_labels.append(df_means.columns[len_t + 1 + f] + ':' + str("{0:0.1f}".format(100.0 * pie_fracs[f]))) ss = [per * 2 * pi for per in percents[:-1]] ee = [per * 2 * pi for per in percents[1:]] mid = [] for pp in range(len(ss)): mid.append(.5 * ss[pp] + .5 * ee[pp]) cc = colors[0:len_cmpds] source = ColumnDataSource(data=dict(starts=ss, ends=ee, color=cc)) x_label, y_label = circ(.5, mid) p_label_data = ColumnDataSource({ 'x_label': x_label, 'y_label': y_label, 'p_labels': per_labels }) p = figure(match_aspect=True, plot_height=1000, plot_width=1000) x_vals, y_vals = circ(1) p.line(x_vals, y_vals, line_width=5, color='black') p.wedge(x=0, y=0, radius=1, start_angle='starts', end_angle='ends', color='color', source=source) p_labels = LabelSet(x="x_label", y="y_label", text="p_labels", source=p_label_data, text_color='black') p.add_layout(p_labels) #widget for pie charts sel_t = Select(title="Choose a time:", value=time_vals[0], options=time_vals) if len(Treatments) == 4: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) select2 = Select(title=Treatments[2], value=str(tm2_vals[0]), options=tm2_vals) select3 = Select(title=Treatments[3], value=str(tm3_vals[0]), options=tm3_vals) elif len(Treatments) == 3: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) select2 = Select(title=Treatments[2], value=str(tm2_vals[0]), options=tm2_vals) elif len(Treatments) == 2: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) elif len(Treatments) == 1: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) checkbox_group = CheckboxGroup(labels=cmpd_options, active=[0, 1, 2, 3, 4]) def update_data(attrname, old, new): if len(Treatments) == 4: df_for_pie = df_means.loc[ (df_means[Time[0]] == sel_t.value) & (df_means[Treatments[0]] == select0.value) & (df_means[Treatments[1]] == select1.value) & (df_means[Treatments[2]] == select2.value) & (df_means[Treatments[3]] == select3.value)] elif len(Treatments) == 3: df_for_pie = df_means.loc[ (df_means[Time[0]] == sel_t.value) & (df_means[Treatments[0]] == select0.value) & (df_means[Treatments[1]] == select1.value) & (df_means[Treatments[2]] == select2.value)] elif len(Treatments) == 2: df_for_pie = df_means.loc[ (df_means[Time[0]] == sel_t.value) & (df_means[Treatments[0]] == select0.value) & (df_means[Treatments[1]] == select1.value)] elif len(Treatments) == 1: df_for_pie = df_means.loc[(df_means[Time[0]] == sel_t.value) & ( df_means[Treatments[0]] == select0.value)] compounds_in_pie = [] for i in checkbox_group.active: compounds_in_pie.append(cmpd_options[i]) df_pie_values = df_for_pie[compounds_in_pie] pie_vals = list(df_pie_values.values.flatten()) pie_fracs = [x / sum(pie_vals) for x in pie_vals] percents = [0] per_labels = [] for f in range(0, len(pie_fracs)): ff = percents[f] + pie_fracs[f] percents.append(ff) per_labels.append(compounds_in_pie[f] + ':' + str("{0:0.1f}".format(100.0 * pie_fracs[f]))) starts = [per * 2 * pi for per in percents[:-1]] ends = [per * 2 * pi for per in percents[1:]] if len(ends) == 0: ends = [0] * len(compounds_in_pie) starts = [0] * len(compounds_in_pie) #per_labels=[0] * len(compounds_in_pie) #per_labels=['{:.2f}'.format(x) for x in per_labels] mid = [] for pp in range(len(starts)): mid.append(.5 * starts[pp] + .5 * ends[pp]) pie_colors = colors[0:len(compounds_in_pie)] x_label, y_label = circ(.5, mid) result2_label = { 'x_label': x_label, 'y_label': y_label, 'p_labels': per_labels } result2 = {'starts': starts, 'ends': ends, 'color': pie_colors} source.data = result2 p_label_data.data = result2_label if len(Treatments) == 4: for w in [sel_t, select0, select1, select2, select3]: w.on_change('value', update_data) elif len(Treatments) == 3: for w in [sel_t, select0, select1, select2]: w.on_change('value', update_data) elif len(Treatments) == 2: for w in [sel_t, select0, select1]: w.on_change('value', update_data) elif len(Treatments) == 1: for w in [sel_t, select0]: w.on_change('value', update_data) checkbox_group.on_change('active', update_data) ## Set up the widgets for pie if len(Treatments) == 4: inputs1 = widgetbox(sel_t, select0, select1, select2, select3) elif len(Treatments) == 3: inputs1 = widgetbox(sel_t, select0, select1, select2) elif len(Treatments) == 2: inputs1 = widgetbox(sel_t, select0, select1) elif len(Treatments) == 1: inputs1 = widgetbox(sel_t, select0) inputs2 = widgetbox(checkbox_group) # Set up layouts and add to document layout = row(column(inputs1), inputs2, p, width=1500) tab = Panel(child=layout, title='Pie Charts') return tab
def rings_tab(df_means,df_stdev, Time, Treatments ,number_cmpds_run): len_t=len(Treatments) colors=["firebrick", "navy", 'green', 'orange', 'violet','lawngreen','lightgreen', 'yellow','olive','red', 'grey','skyblue','indigo','slategray','hotpink','peachpuff','powderblue'] df_means=df_means.reindex(index=order_by_index(df_means.index, index_natsorted(df_means[Time[0]]))) cmpd_options=cmpd_options_func(df_means,len_t+1,number_cmpds_run) time_vals=df_means[Time[0]].drop_duplicates().tolist() time_vals=natsorted(time_vals) if len(Treatments) == 4: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') df_means[Treatments[3]] = df_means[Treatments[3]].astype('str') tm0_vals=df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals=df_means[Treatments[1]].drop_duplicates().tolist() tm2_vals=df_means[Treatments[2]].drop_duplicates().tolist() tm3_vals=df_means[Treatments[3]].drop_duplicates().tolist() elif len(Treatments) == 3: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') df_means[Treatments[2]] = df_means[Treatments[2]].astype('str') tm0_vals=df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals=df_means[Treatments[1]].drop_duplicates().tolist() tm2_vals=df_means[Treatments[2]].drop_duplicates().tolist() elif len(Treatments) == 2: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') df_means[Treatments[1]] = df_means[Treatments[1]].astype('str') tm0_vals=df_means[Treatments[0]].drop_duplicates().tolist() tm1_vals=df_means[Treatments[1]].drop_duplicates().tolist() elif len(Treatments) == 1: df_means[Treatments[0]] = df_means[Treatments[0]].astype('str') tm0_vals=df_means[Treatments[0]].drop_duplicates().tolist() df_means = df_means.replace(float('nan'),0) start_keys=[] end_keys=[] mid_keys=[] per_keys=[] for h in time_vals: start_keys.append('start '+h) end_keys.append('end '+h) mid_keys.append('mid '+h) per_keys.append('per '+h) def_cmpds=[] for i in range(5): def_cmpds.append(df_means.columns[len_t+1+i]) len_cmpds=len(def_cmpds) p=figure(match_aspect=True, plot_height=1000, plot_width=1000) starts=[] ends=[] mid=[] per_labels=[] ring_colors=colors[0:len_cmpds] results3 = {'color' : ring_colors} labels_dicts={} p_x_strs=[] p_y_strs=[] for tt, tt_str in enumerate(time_vals): if len(Treatments) == 4: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==tm0_vals[0]) & (df_means[Treatments[1]]==tm1_vals[0]) & (df_means[Treatments[2]]==tm2_vals[0]) & (df_means[Treatments[3]]==tm3_vals[0])] elif len(Treatments) == 3: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==tm0_vals[0]) & (df_means[Treatments[1]]==tm1_vals[0]) & (df_means[Treatments[2]]==tm2_vals[0]) ] elif len(Treatments) == 2: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==tm0_vals[0]) & (df_means[Treatments[1]]==tm1_vals[0]) ] elif len(Treatments) == 1: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==tm0_vals[0]) ] df_ring_values=df_for_ring[def_cmpds] ring_vals=list(df_ring_values.values.flatten()) ring_sums=sum(ring_vals) ring_fracs = [x / ring_sums for x in ring_vals] percents=[0] perper_labels=[] for f in range(0,len(ring_fracs)): ff=percents[f]+ring_fracs[f] percents.append(ff) if tt==(len(time_vals)-1): perper_labels.append(def_cmpds[f]+': '+str("{0:0.1f}".format(100.0*ring_fracs[f]))) else: perper_labels.append(str("{0:0.1f}".format(100.0*ring_fracs[f]))) per_labels.append(perper_labels) starts=[per*2*pi for per in percents[:-1]] ends=[per*2*pi for per in percents[1:]] mid=[] for pp in range(len(starts)): mid.append(.5*starts[pp]+.5*ends[pp]) results3.update({start_keys[tt]:starts}) results3.update({end_keys[tt]:ends}) results3.update({mid_keys[tt]:mid}) labels_dicts.update({per_keys[tt]:list(per_labels[tt])}) x_label, y_label=circ(.5+tt,mid) x_str='xlabel_'+str(tt) p_x_strs.append(x_str) y_str='ylabel_'+str(tt) p_y_strs.append(y_str) labels_dicts.update({x_str:x_label}) labels_dicts.update({y_str:y_label}) source3 = ColumnDataSource(data=results3) p_label_source=ColumnDataSource(data=labels_dicts) for tt, tt_str in enumerate(time_vals): p.annular_wedge(x=0, y=0, inner_radius=tt, outer_radius=tt+1, start_angle=start_keys[tt], end_angle=end_keys[tt], color='color',source=source3) x_vals,y_vals=circ(tt+1) p.line(x_vals, y_vals, line_width=3,color='black') p_labels = LabelSet(x=p_x_strs[tt],y=p_y_strs[tt],text=per_keys[tt],source=p_label_source) p.add_layout(p_labels) #widget for ring charts if len(Treatments) == 4: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) select2 = Select(title=Treatments[2], value=str(tm2_vals[0]), options=tm2_vals) select3 = Select(title=Treatments[3], value=str(tm3_vals[0]), options=tm3_vals) elif len(Treatments) == 3: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) select2 = Select(title=Treatments[2], value=str(tm2_vals[0]), options=tm2_vals) elif len(Treatments) == 2: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) select1 = Select(title=Treatments[1], value=str(tm1_vals[0]), options=tm1_vals) elif len(Treatments) == 1: select0 = Select(title=Treatments[0], value=str(tm0_vals[0]), options=tm0_vals) checkbox_group = CheckboxGroup( labels=cmpd_options, active=[0, 1, 2, 3, 4]) #widget for ring charts if len(Treatments) == 4: def update_title(attrname, old, new): p.title.text = select0.value+' '+select1.value+' '+select2.value+' '+select3.value elif len(Treatments) == 3: def update_title(attrname, old, new): p.title.text = select0.value+' '+select1.value+' '+select2.value elif len(Treatments) == 2: def update_title(attrname, old, new): p.title.text = select0.value+' '+select1.value elif len(Treatments) == 1: def update_title(attrname, old, new): p.title.text = select0.value #widget for ring charts if len(Treatments) == 4: select0.on_change('value', update_title) select1.on_change('value', update_title) select2.on_change('value', update_title) select3.on_change('value', update_title) elif len(Treatments) == 3: select0.on_change('value', update_title) select1.on_change('value', update_title) select2.on_change('value', update_title) elif len(Treatments) == 2: select0.on_change('value', update_title) select1.on_change('value', update_title) elif len(Treatments) == 1: select0.on_change('value', update_title) def update_data(attrname, old, new): compounds_in_ring=[] for i in checkbox_group.active: compounds_in_ring.append(cmpd_options[i]) ring_colors=colors[0:len(compounds_in_ring)] starts=[] ends=[] mid=[] per_labels=[] results3 = {'color' : ring_colors} labels_dicts={} p_x_strs=[] p_y_strs=[] for tt, tt_str in enumerate(time_vals): if len(Treatments) == 4: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==select0.value) & (df_means[Treatments[1]]==select1.value) & (df_means[Treatments[2]]==select2.value) & (df_means[Treatments[3]]==select3.value)] elif len(Treatments) == 3: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==select0.value) & (df_means[Treatments[1]]==select1.value) & (df_means[Treatments[2]]==select2.value) ] elif len(Treatments) == 2: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==select0.value) & (df_means[Treatments[1]]==select1.value) ] elif len(Treatments) == 1: df_for_ring=df_means.loc[(df_means[Time[0]] == tt_str) & (df_means[Treatments[0]]==select0.value) ] df_ring_values=df_for_ring[compounds_in_ring] ring_vals=list(df_ring_values.values.flatten()) ring_sums=sum(ring_vals) ring_fracs = [x / ring_sums for x in ring_vals] percents=[0] perper_labels=[] for f in range(0,len(ring_fracs)): ff=percents[f]+ring_fracs[f] percents.append(ff) if tt==(len(time_vals)-1): perper_labels.append(compounds_in_ring[f]+': '+str("{0:0.1f}".format(100.0*ring_fracs[f]))) else: perper_labels.append(str("{0:0.1f}".format(100.0*ring_fracs[f]))) per_labels.append(perper_labels) starts=[per*2*pi for per in percents[:-1]] ends=[per*2*pi for per in percents[1:]] if len(ends)==0: ends=[0] * len(compounds_in_ring) starts=[0] * len(compounds_in_ring) #per_labels=[0] * len(compounds_in_pie) #per_labels=['{:.2f}'.format(x) for x in per_labels] mid=[] for pp in range(len(starts)): mid.append(.5*starts[pp]+.5*ends[pp]) results3.update({start_keys[tt]:starts}) results3.update({end_keys[tt]:ends}) results3.update({mid_keys[tt]:mid}) labels_dicts.update({per_keys[tt]:list(per_labels[tt])}) x_label, y_label=circ(.5+tt,mid) x_str='xlabel_'+str(tt) p_x_strs.append(x_str) y_str='ylabel_'+str(tt) p_y_strs.append(y_str) labels_dicts.update({x_str:x_label}) labels_dicts.update({y_str:y_label}) p_label_source.data=labels_dicts source3.data=results3 if len(Treatments) == 4: for w in [select0,select1,select2,select3]: w.on_change('value', update_data) elif len(Treatments) == 3: for w in [select0,select1,select2]: w.on_change('value', update_data) elif len(Treatments) == 2: for w in [select0,select1]: w.on_change('value', update_data) elif len(Treatments) == 1: for w in [select0]: w.on_change('value', update_data) checkbox_group.on_change('active', update_data) ## Set up the widgets for rings if len(Treatments) == 4: inputs1 = widgetbox(select0,select1,select2,select3) elif len(Treatments) == 3: inputs1 = widgetbox(select0,select1,select2) elif len(Treatments) == 2: inputs1 = widgetbox(select0,select1) elif len(Treatments) == 1: inputs1 = widgetbox(select0) inputs2 = widgetbox(checkbox_group) layout=row(column(inputs1),inputs2, p, width=1500) tab = Panel(child=layout, title="Pie in Pie Chart") return tab