def _do_classifyplot(df, out_file, title=None, size=None, samples=None, callers=None):
    """Plot using classification-based plot using seaborn.
    """
    metric_labels = {"fdr": "False discovery rate",
                     "fnr": "False negative rate"}
    metrics = [("fnr", "tpr"), ("fdr", "spc")]
    is_mpl2 = LooseVersion(mpl.__version__) >= LooseVersion('2.0')
    colors = ["light grey", "greyish"] * 10
    data_dict = df.set_index(["sample", "caller", "vtype"]).T.to_dict()
    plt.ioff()
    plt.style.use('seaborn-white')
    vtypes = sorted(df["vtype"].unique(), reverse=True)
    if not callers:
        callers = sorted(df["caller"].unique())
    if not samples:
        samples = sorted(df["sample"].unique())
    if len(samples) >= len(callers):
        cats, groups = (samples, callers)
        data_dict = df.set_index(["sample", "caller", "vtype"]).T.to_dict()
    else:
        cats, groups = (callers, samples)
        data_dict = df.set_index(["caller", "sample", "vtype"]).T.to_dict()
    fig, axs = plt.subplots(len(vtypes) * len(groups), len(metrics))
    fig.text(.5, .95, title if title else "", horizontalalignment='center', size=14)
    for vi, vtype in enumerate(vtypes):
        for gi, group in enumerate(groups):
            for mi, (metric, label) in enumerate(metrics):
                row_plots = axs if len(vtypes) * len(groups) == 1 else axs[vi * len(groups) + gi]
                cur_plot = row_plots if len(metrics) == 1 else row_plots[mi]
                vals, labels = [], []
                for cat in cats:
                    cur_data = data_dict.get((cat, group, vtype))
                    if cur_data:
                        vals.append(cur_data[metric])
                        labels.append(cur_data[label])
                cur_plot.barh(np.arange(len(vals)), vals, color=sns.xkcd_palette([colors[vi]]))
                all_vals = []
                for k, d in data_dict.items():
                    if k[-1] == vtype:
                        for m in metrics:
                            all_vals.append(d[m[0]])
                metric_max = max(all_vals)
                cur_plot.set_xlim(0, metric_max)
                pad = 0.1 * metric_max
                ai_adjust = 0.0 if is_mpl2 else 0.35
                for ai, (val, label) in enumerate(zip(vals, labels)):
                    cur_plot.annotate(label, (pad + (0 if max(vals) > metric_max / 2.0 else max(vals)),
                                              ai + ai_adjust),
                                      va='center', size=7)
                cur_plot.locator_params(nbins=len(cats) + (2 if len(cats) > 2 else 1), axis="y", tight=True)
                if mi == 0:
                    cur_plot.tick_params(axis='y', which='major', labelsize=8)
                    plot_cats = ([""] + cats) if is_mpl2 else cats
                    plot_va = "center" if is_mpl2 else "bottom"
                    cur_plot.set_yticklabels(plot_cats, size=8, va=plot_va)
                    cur_plot.set_title("%s: %s" % (vtype, group), fontsize=12, loc="left")
                else:
                    cur_plot.get_yaxis().set_ticks([])
                if gi == len(groups) - 1:
                    cur_plot.tick_params(axis='x', which='major', labelsize=8)
                    cur_plot.get_xaxis().set_major_formatter(
                        mpl_ticker.FuncFormatter(lambda v, p: "%s%%" % (int(v) if round(v) == v else v)))
                    if vi == len(vtypes) - 1:
                        cur_plot.get_xaxis().set_label_text(metric_labels[metric], size=12)
                else:
                    cur_plot.get_xaxis().set_ticks([])
                    cur_plot.spines['bottom'].set_visible(False)
                cur_plot.spines['left'].set_visible(False)
                cur_plot.spines['top'].set_visible(False)
                cur_plot.spines['right'].set_visible(False)
    x, y = (6, len(vtypes) * len(groups) + 1 * 0.5 * len(cats)) if size is None else size
    fig.set_size_inches(x, y)
    fig.tight_layout(rect=(0, 0, 1, 0.95))
    plt.subplots_adjust(hspace=0.6)
    fig.savefig(out_file)
def _do_classifyplot ( df , out_file , title = None , size = None , samples = None , callers = None ) : metric_labels = { "fdr" : "False discovery rate" , "fnr" : "False negative rate" } metrics = [ ( "fnr" , "tpr" ) , ( "fdr" , "spc" ) ] is_mpl2 = LooseVersion ( mpl . __version__ ) >= LooseVersion ( '2.0' ) colors = [ "light grey" , "greyish" ] * 10 data_dict = df . set_index ( [ "sample" , "caller" , "vtype" ] ) . T . to_dict ( ) plt . ioff ( ) plt . style . use ( 'seaborn-white' ) vtypes = sorted ( df [ "vtype" ] . unique ( ) , reverse = True ) if not callers : callers = sorted ( df [ "caller" ] . unique ( ) ) if not samples : samples = sorted ( df [ "sample" ] . unique ( ) ) if len ( samples ) >= len ( callers ) : cats , groups = ( samples , callers ) data_dict = df . set_index ( [ "sample" , "caller" , "vtype" ] ) . T . to_dict ( ) else : cats , groups = ( callers , samples ) data_dict = df . set_index ( [ "caller" , "sample" , "vtype" ] ) . T . to_dict ( ) fig , axs = plt . subplots ( len ( vtypes ) * len ( groups ) , len ( metrics ) ) fig . text ( .5 , .95 , title if title else "" , horizontalalignment = 'center' , size = 14 ) for vi , vtype in enumerate ( vtypes ) : for gi , group in enumerate ( groups ) : for mi , ( metric , label ) in enumerate ( metrics ) : row_plots = axs if len ( vtypes ) * len ( groups ) == 1 else axs [ vi * len ( groups ) + gi ] cur_plot = row_plots if len ( metrics ) == 1 else row_plots [ mi ] vals , labels = [ ] , [ ] for cat in cats : cur_data = data_dict . get ( ( cat , group , vtype ) ) if cur_data : vals . append ( cur_data [ metric ] ) labels . append ( cur_data [ label ] ) cur_plot . barh ( np . arange ( len ( vals ) ) , vals , color = sns . xkcd_palette ( [ colors [ vi ] ] ) ) all_vals = [ ] for k , d in data_dict . items ( ) : if k [ - 1 ] == vtype : for m in metrics : all_vals . append ( d [ m [ 0 ] ] ) metric_max = max ( all_vals ) cur_plot . set_xlim ( 0 , metric_max ) pad = 0.1 * metric_max ai_adjust = 0.0 if is_mpl2 else 0.35 for ai , ( val , label ) in enumerate ( zip ( vals , labels ) ) : cur_plot . annotate ( label , ( pad + ( 0 if max ( vals ) > metric_max / 2.0 else max ( vals ) ) , ai + ai_adjust ) , va = 'center' , size = 7 ) cur_plot . locator_params ( nbins = len ( cats ) + ( 2 if len ( cats ) > 2 else 1 ) , axis = "y" , tight = True ) if mi == 0 : cur_plot . tick_params ( axis = 'y' , which = 'major' , labelsize = 8 ) plot_cats = ( [ "" ] + cats ) if is_mpl2 else cats plot_va = "center" if is_mpl2 else "bottom" cur_plot . set_yticklabels ( plot_cats , size = 8 , va = plot_va ) cur_plot . set_title ( "%s: %s" % ( vtype , group ) , fontsize = 12 , loc = "left" ) else : cur_plot . get_yaxis ( ) . set_ticks ( [ ] ) if gi == len ( groups ) - 1 : cur_plot . tick_params ( axis = 'x' , which = 'major' , labelsize = 8 ) cur_plot . get_xaxis ( ) . set_major_formatter ( mpl_ticker . FuncFormatter ( lambda v , p : "%s%%" % ( int ( v ) if round ( v ) == v else v ) ) ) if vi == len ( vtypes ) - 1 : cur_plot . get_xaxis ( ) . set_label_text ( metric_labels [ metric ] , size = 12 ) else : cur_plot . get_xaxis ( ) . set_ticks ( [ ] ) cur_plot . spines [ 'bottom' ] . set_visible ( False ) cur_plot . spines [ 'left' ] . set_visible ( False ) cur_plot . spines [ 'top' ] . set_visible ( False ) cur_plot . spines [ 'right' ] . set_visible ( False ) x , y = ( 6 , len ( vtypes ) * len ( groups ) + 1 * 0.5 * len ( cats ) ) if size is None else size fig . set_size_inches ( x , y ) fig . tight_layout ( rect = ( 0 , 0 , 1 , 0.95 ) ) plt . subplots_adjust ( hspace = 0.6 ) fig . savefig ( out_file )
Plot using classification-based plot using seaborn.