Evaluate classification accuracy

This notebook demonstrates how to evaluate classification accuracy of "cross-validated" simulated communities. Due to the unique nature of this analysis, the metrics that we use to evaluate classification accuracy are different from those used for mock.

The key measure here is rate of match vs. overclassification, hence P/R/F are not useful metrics. Instead, we define and measure the following as percentages:

  • Match vs. overclassification rate
    • Match: exact match at level L
    • underclassification: lineage assignment is correct, but shorter than expected (e.g., not to species level)
    • misclassification: incorrect assignment

Where L = taxonomic level being tested

Functions


In [1]:
from tax_credit.framework_functions import (novel_taxa_classification_evaluation,
                                            extract_per_level_accuracy)
from tax_credit.eval_framework import parameter_comparisons
from tax_credit.plotting_functions import (pointplot_from_data_frame,
                                           heatmap_from_data_frame,
                                           per_level_kruskal_wallis,
                                           rank_optimized_method_performance_by_dataset)

import seaborn.xkcd_rgb as colors
import pandas as pd
from os.path import expandvars, join, exists
from glob import glob
from IPython.display import display, Markdown

Evaluate classification results

First, enter in filepaths and directory paths where your data are stored, and the destination


In [2]:
project_dir = expandvars("../../")
analysis_name = "cross-validated"
precomputed_results_dir = join(project_dir, "data", "precomputed-results", analysis_name)
expected_results_dir = join(project_dir, "data", analysis_name)
summary_fp = join(precomputed_results_dir, 'evaluate_classification_summary.csv')

results_dirs = glob(join(precomputed_results_dir, '*', '*', '*', '*'))

# we can save plots in this directory
outdir = expandvars("../../plots/")

This cell performs the classification evaluation and should not be modified.


In [3]:
force = True
if force or not exists(summary_fp):
    accuracy_results = novel_taxa_classification_evaluation(results_dirs, expected_results_dir,
                                                            summary_fp, test_type='cross-validated')
else:
    accuracy_results = pd.DataFrame.from_csv(summary_fp)

Plot classification accuracy

Finally, we plot our results. Line plots show the mean +/- 95% confidence interval for each classification result at each taxonomic level (1 = phylum, 6 = species) in each dataset tested. Do not modify the cell below, except to adjust the color_palette used for plotting. This palette can be a dictionary of colors for each group, as shown below, or a seaborn color palette.

match_ratio = proportion of correct matches.

underclassification_ratio = proportion of assignments to correct lineage but to a lower level than expected.

misclassification_ratio = proportion of assignments to an incorrect lineage.


In [4]:
color_palette={
    'expected': 'black', 'rdp': colors['baby shit green'], 'sortmerna': colors['macaroni and cheese'],
    'uclust': 'coral', 'blast': 'indigo', 'blast+': colors['electric purple'], 'naive-bayes': 'dodgerblue',
    'naive-bayes-bespoke': 'blue', 'vsearch': 'firebrick'
}

level_results = extract_per_level_accuracy(accuracy_results)

y_vars = ['Precision', 'Recall', 'F-measure']

In [5]:
point = pointplot_from_data_frame(level_results, "level", y_vars,
                                  group_by="Dataset", color_by="Method",
                                  color_palette=color_palette)



In [6]:
for k, v in point.items():
    v.savefig(join(outdir, 'cross-val-{0}-lineplots.pdf'.format(k)))

Per-level classification accuracy statistic

Kruskal-Wallis FDR-corrected p-values comparing classification methods at each level of taxonomic assignment


In [7]:
result = per_level_kruskal_wallis(level_results, y_vars, group_by='Method', 
                                  dataset_col='Dataset', alpha=0.05, 
                                  pval_correction='fdr_bh')
result


Out[7]:
Dataset Variable 1 2 3 4 5 6
0 F1-REF Precision 3.009026e-41 2.714002e-43 1.322547e-86 4.236705e-113 4.153139e-96 2.741040e-56
1 F1-REF Recall 4.209237e-164 2.647126e-130 1.871151e-117 3.670214e-111 2.591394e-117 1.470487e-103
2 F1-REF F-measure 3.421874e-149 4.144124e-122 8.188278e-113 6.362069e-112 2.273480e-123 1.771390e-103
3 B1-REF Precision 1.352499e-18 2.207341e-50 1.720656e-40 1.158801e-24 5.608904e-14 7.279736e-21
4 B1-REF Recall 2.918104e-73 3.720264e-76 2.390603e-99 4.433052e-143 5.494589e-183 8.542637e-210
5 B1-REF F-measure 2.030409e-76 1.005296e-82 1.913723e-108 2.111334e-158 1.338052e-258 2.131334e-293

Heatmaps of method accuracy by parameter

Heatmaps show the performance of individual method/parameter combinations at each taxonomic level, in each reference database (i.e., for bacterial and fungal simulated datasets individually).


In [8]:
heatmap_from_data_frame(level_results, metric="Precision", rows=["Method", "Parameters"], cols=["Dataset", "level"])


Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff803cc4cf8>

In [9]:
heatmap_from_data_frame(level_results, metric="Recall", rows=["Method", "Parameters"], cols=["Dataset", "level"])


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff801b2f048>

In [10]:
heatmap_from_data_frame(level_results, metric="F-measure", rows=["Method", "Parameters"], cols=["Dataset", "level"])


Out[10]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff801c33fd0>

Rank-based statistics comparing the performance of the optimal parameter setting run for each method on each data set.

Rank parameters for each method to determine the best parameter configuration within each method. Count best values in each column indicate how many samples a given method achieved within one mean absolute deviation of the best result (which is why they may sum to more than the total number of samples).


In [11]:
for method in level_results['Method'].unique():
    top_params = parameter_comparisons(level_results, method, metrics=y_vars, 
                                       sample_col='Dataset', method_col='Method',
                                       dataset_col='Dataset')
    display(Markdown('## {0}'.format(method)))
    display(top_params[:10])


blast+

F-measure Precision Recall
0.001:1:0.51:0.8 100.0 83 100.0
0.001:1:0.75:0.8 100.0 83 100.0
0.001:1:0.99:0.8 100.0 83 100.0
0.001:10:0.51:0.8 96.0 85 90.0
0.001:10:0.75:0.8 86.0 101 80.0
0.001:10:0.99:0.8 80.0 110 79.0
0.001:10:0.75:0.97 40.0 92 3.0
0.001:10:0.99:0.97 40.0 97 3.0
0.001:10:0.51:0.97 40.0 85 4.0
0.001:1:0.51:0.97 40.0 87 4.0

naive-bayes

F-measure Precision Recall
0.001:[6,6]:0.7 91.0 90 90.0
0.001:[6,6]:0.5 90.0 81 90.0
0.001:[7,7]:0.7 89.0 89 90.0
0.001:[7,7]:0.5 88.0 81 90.0
0.001:[7,7]:0.9 88.0 98 81.0
0.001:[6,6]:0.0 84.0 80 90.0
0.001:[7,7]:0.92 84.0 99 80.0
0.001:[6,6]:0.9 84.0 99 80.0
0.001:[8,8]:0.7 84.0 90 87.0
0.001:[8,8]:0.5 84.0 81 90.0

vsearch

F-measure Precision Recall
1:0.51:0.8 100.0 82 100.0
1:0.51:0.9 100.0 90 83.0
1:0.99:0.8 100.0 82 100.0
1:0.99:0.9 100.0 90 83.0
10:0.51:0.9 97.0 90 79.0
10:0.51:0.8 96.0 87 98.0
10:0.99:0.8 90.0 110 80.0
10:0.99:0.9 90.0 110 68.0
1:0.51:0.97 40.0 90 0.0
1:0.99:0.97 40.0 90 0.0

rdp

F-measure Precision Recall
0.5 81 90 82
0.6 81 92 80
0.0 80 80 82
0.1 80 80 82
0.2 80 80 83
0.3 80 80 83
0.4 80 84 83
0.7 80 100 77
0.8 80 100 71
0.9 79 100 66

sortmerna

F-measure Precision Recall
0.51:0.8:1:0.8:1.0 80 80 80
0.76:0.8:1:0.8:1.0 80 80 80
1.0:0.8:3:0.8:1.0 80 94 79
1.0:0.8:3:0.9:1.0 80 94 79
1.0:0.8:5:0.8:1.0 80 104 78
0.51:0.8:1:0.9:1.0 80 80 80
0.76:0.8:5:0.9:1.0 80 90 78
0.76:0.8:5:0.8:1.0 80 90 78
0.76:0.8:3:0.9:1.0 80 94 79
0.76:0.8:3:0.8:1.0 80 94 79

blast

F-measure Precision Recall
1e-10 82 88 81
0.001 81 87 81
1 81 87 81
1000 81 87 81

uclust

F-measure Precision Recall
0.51:0.8:3 81 80 81
0.51:0.8:1 80 80 81
1.0:0.8:1 80 80 81
0.51:0.8:5 80 80 80
1.0:0.8:5 80 110 73
0.76:0.8:1 80 80 81
0.76:0.8:3 80 100 79
0.76:0.8:5 80 98 79
1.0:0.8:3 80 100 79
1.0:0.9:3 40 101 40

naive-bayes-bespoke

F-measure Precision Recall
0.001::[7,7]:0.7 90.0 90 90.0
0.001::[8,8]:0.92 90.0 95 82.0
0.001::[6,6]:0.9 90.0 97 83.0
0.001::[7,7]:0.5 90.0 82 90.0
0.001::[6,6]:0.92 90.0 99 83.0
0.001::[9,9]:0.96 89.0 99 70.0
0.001::[9,9]:0.9 89.0 93 78.0
0.001::[6,6]:0.7 89.0 90 90.0
0.001::[7,7]:0.9 89.0 94 85.0
0.001::[8,8]:0.9 89.0 94 84.0

Rank performance of optimized methods

Now we rank the top-performing method/parameter combination for each method at genus and species levels. Methods are ranked by top F-measure, and the average value for each metric is shown (rather than count best as above). F-measure distributions are plotted for each method, and compared using paired t-tests with FDR-corrected P-values. This cell does not need to be altered, unless if you wish to change the metric used for sorting best methods and for plotting.


In [12]:
boxes = rank_optimized_method_performance_by_dataset(level_results,
                                                     metric="F-measure",
                                                     level="level",
                                                     level_range=range(6,7),
                                                     display_fields=["Method",
                                                                     "Parameters",
                                                                     "Precision",
                                                                     "Recall",
                                                                     "F-measure"],
                                                     paired=True,
                                                     parametric=True,
                                                     color=None,
                                                     color_palette=color_palette)


F1-REF level 6

Method Parameters Precision Recall F-measure
6 uclust 0.51:0.8:5 0.684296 0.429991 0.528025
4 rdp 0.9 0.697787 0.411087 0.517324
3 naive-bayes-bespoke 0.001::[6,6]:0.99 0.653155 0.422298 0.512867
5 sortmerna 0.76:0.8:5:0.9:1.0 0.673183 0.412841 0.511767
2 naive-bayes 0.001:[6,6]:0.99 0.655488 0.419046 0.511161
7 vsearch 10:0.51:0.8 0.705452 0.397744 0.508549
1 blast+ 0.001:10:0.51:0.8 0.671470 0.378932 0.484280
0 blast 1e-10 0.255774 0.206764 0.228650
stat P FDR P
Method A Method B
blast+ naive-bayes -4.673727 1.162454e-03 2.324909e-03
vsearch -9.421093 5.864727e-06 1.824582e-05
rdp -5.851767 2.432225e-04 6.191119e-04
sortmerna -7.762333 2.814248e-05 7.879894e-05
blast 30.947561 1.881978e-10 7.527912e-10
uclust -14.520262 1.495592e-07 5.234573e-07
naive-bayes-bespoke -5.208140 5.579570e-04 1.301900e-03
naive-bayes vsearch 0.605364 5.598905e-01 6.029590e-01
rdp -1.074151 3.107085e-01 3.793092e-01
sortmerna -0.115035 9.109433e-01 9.109433e-01
blast 43.485879 8.980502e-12 8.381802e-11
uclust -2.445055 3.705609e-02 6.484816e-02
naive-bayes-bespoke -1.107819 2.966667e-01 3.793092e-01
vsearch rdp -1.588344 1.466713e-01 2.161472e-01
sortmerna -0.827187 4.295167e-01 4.810587e-01
blast 36.292970 4.532830e-11 2.285477e-10
uclust -4.998257 7.407280e-04 1.595414e-03
naive-bayes-bespoke -1.127807 2.885688e-01 3.793092e-01
rdp sortmerna 1.072111 3.115754e-01 3.793092e-01
blast 53.546691 1.389022e-12 3.889260e-11
uclust -1.908000 8.874521e-02 1.380481e-01
naive-bayes-bespoke 0.904331 3.893849e-01 4.542824e-01
sortmerna blast 36.874644 3.931868e-11 2.285477e-10
uclust -3.147397 1.178682e-02 2.200207e-02
naive-bayes-bespoke -0.209640 8.386170e-01 8.696769e-01
blast uclust -35.980379 4.897450e-11 2.285477e-10
naive-bayes-bespoke -44.680526 7.043922e-12 8.381802e-11
uclust naive-bayes-bespoke 2.361189 4.251842e-02 7.003034e-02

B1-REF level 6

Method Parameters Precision Recall F-measure
5 sortmerna 0.76:0.8:5:0.9:1.0 0.934980 0.781390 0.851284
6 uclust 0.51:0.8:3 0.873325 0.822354 0.847064
3 naive-bayes-bespoke 0.001::[6,6]:0.7 0.884186 0.786815 0.832653
2 naive-bayes 0.001:[6,6]:0.7 0.900455 0.765003 0.827215
7 vsearch 10:0.51:0.9 0.896329 0.755438 0.819837
4 rdp 0.5 0.847648 0.787429 0.816427
1 blast+ 0.001:10:0.51:0.8 0.896382 0.749011 0.816035
0 blast 1e-10 0.809746 0.807025 0.808383
stat P FDR P
Method A Method B
blast+ naive-bayes -3.413447 7.707449e-03 9.809480e-03
vsearch -3.265361 9.754774e-03 1.187538e-02
rdp -0.119500 9.075042e-01 9.075042e-01
sortmerna -15.306849 9.460483e-08 2.943261e-07
blast 2.164396 5.864626e-02 6.315751e-02
uclust -9.680066 4.687291e-06 9.515191e-06
naive-bayes-bespoke -6.737585 8.483442e-05 1.319646e-04
naive-bayes vsearch 2.421682 3.850425e-02 4.492162e-02
rdp 9.033938 8.279083e-06 1.448839e-05
sortmerna -10.652090 2.109952e-06 5.907866e-06
blast 19.515871 1.126881e-08 7.888169e-08
uclust -17.015659 3.756941e-08 1.952261e-07
naive-bayes-bespoke -3.423904 7.580973e-03 9.809480e-03
vsearch rdp 1.214379 2.555041e-01 2.649672e-01
sortmerna -16.796992 4.207132e-08 1.952261e-07
blast 3.781383 4.339772e-03 6.075681e-03
uclust -9.453657 5.700168e-06 1.064031e-05
naive-bayes-bespoke -5.952329 2.147231e-04 3.164341e-04
rdp sortmerna -16.514100 4.880652e-08 1.952261e-07
blast 8.961748 8.840670e-06 1.456110e-05
uclust -21.114242 5.625700e-09 7.875980e-08
naive-bayes-bespoke -9.662679 4.757596e-06 9.515191e-06
sortmerna blast 20.031778 8.953263e-09 7.888169e-08
uclust 2.183154 5.688143e-02 6.315751e-02
naive-bayes-bespoke 9.864858 4.006863e-06 9.349346e-06
blast uclust -30.420608 2.193630e-10 6.142165e-09
naive-bayes-bespoke -15.624536 7.911704e-08 2.769096e-07
uclust naive-bayes-bespoke 9.868633 3.994149e-06 9.349346e-06

In [13]:
for k, v in boxes.items():
    v.get_figure().savefig(join(outdir, 'cross-val-{0}-boxplots.pdf'.format(k)))

In [ ]: