Evaluate classification accuracy

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

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: assignment == L - 1 (e.g., a novel species is assigned the correct genus)
    • overclassification: assignment == L (e.g., correct genus but assigns to a near neighbor)
    • misclassification: incorrect assignment at L - 1 (e.g., wrong genus-level assignment)

Where L = taxonomic level being tested

Functions


In [1]:
from tax_credit.framework_functions import novel_taxa_classification_evaluation
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)

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

Evaluate classification results

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


In [2]:
project_dir = expandvars("$HOME/Desktop/projects/short-read-tax-assignment")
analysis_name = "novel-taxa-simulations"
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, '*', '*', '*', '*'))

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


In [3]:
if not exists(summary_fp):
    accuracy_results = novel_taxa_classification_evaluation(results_dirs, expected_results_dir, summary_fp)
else:
    accuracy_results = pd.DataFrame.from_csv(summary_fp)


/Users/nbokulich/miniconda3/envs/qiime2-2017.3-dev/lib/python3.5/site-packages/scikit_learn-0.18.1-py3.5-macosx-10.6-x86_64.egg/sklearn/metrics/classification.py:1113: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 due to no predicted samples.
  'precision', 'predicted', average, warn_for)

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_pallette used for plotting. This palette can be a dictionary of colors for each group, as shown below, or a seaborn color palette.

Precision = Proportion of classifications that were correct. For novel taxa, this means a match at the last common ancestor (LCA) (level-1). True Positives / (True Positives + False Positives)

Recall = Proportion of reads that were correctly classified. Equals the number of exact matches to the LCA. True Positives / (True Positives + False Negatives)

F-measure = Harmonic mean of Precision and Recall

overclassification_ratio = proportion of taxa that were assigned to correct lineage but to a deeper taxonomic level than expected, rather than to LCA. E.g., assignment to another species in the clade

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_pallette={
    'rdp': 'seagreen', 'sortmerna': 'gray', 'vsearch': 'brown',
    'uclust': 'blue', 'blast': 'black', 'blast+': 'purple', 'q2-nb': 'pink',
}

y_vars = ["Precision", "Recall", "F-measure",
          "overclassification_ratio", 
          "underclassification_ratio", "misclassification_ratio"]

Plot per-level classification accuracy

For novel-taxa analysis, a separate classification is performed at each taxonomic level using different test (unique taxa at level L) and training sets (ref - test taxonomies). Hence, results at each level L represent independent tests, unlike for mock and simulated communities where each level represents the accuracy of each species-level classification trimmed to level L. For novel taxa, results at level L indicate the accuracy with with method M assigns the correct lineage to a "novel" taxon, which is unrepresented in the reference at level L, e.g., level 6 indicates the performance with which each classifier assigns the correct genus to each species.


In [5]:
pointplot_from_data_frame(accuracy_results, "level", y_vars, 
                          group_by="Dataset", color_by="Method",
                          color_pallette=color_pallette)


Per-level classification accuracy statistic

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


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


Out[6]:
Dataset Variable 1 2 3 4 5 6
0 B1-REF Precision 3.715624e-12 1.198042e-11 7.427275e-09 7.478812e-08 3.504082e-04 6.586488e-05
1 B1-REF Recall 3.715624e-12 2.794139e-11 2.888930e-08 3.885609e-08 4.804298e-06 1.040800e-08
2 B1-REF F-measure 3.715624e-12 2.611085e-11 1.581851e-08 1.679884e-08 1.919682e-05 2.370710e-08
3 B1-REF overclassification_ratio 9.272498e-09 2.180811e-04 3.450349e-07 7.497222e-09 2.002816e-07 1.726080e-05
4 B1-REF underclassification_ratio 3.553270e-23 8.418411e-11 1.308216e-11 3.955306e-10 8.980685e-14 8.856378e-11
5 B1-REF misclassification_ratio 1.185327e-34 6.032379e-08 2.888930e-08 7.654820e-11 7.616718e-11 2.758384e-04
6 F1-REF Precision 3.430642e-17 1.080875e-14 3.772074e-10 1.040800e-08 9.813963e-07 4.703775e-05
7 F1-REF Recall 3.430642e-17 1.100959e-14 9.045191e-12 1.061669e-09 9.283231e-09 6.992396e-08
8 F1-REF F-measure 3.430642e-17 1.100959e-14 2.903838e-11 1.057349e-08 4.425990e-08 4.363947e-07
9 F1-REF overclassification_ratio 4.441149e-27 3.065798e-23 1.806493e-13 2.458399e-17 7.415289e-19 3.462064e-14
10 F1-REF underclassification_ratio 2.430069e-37 1.185327e-34 5.127613e-20 7.723679e-21 1.024508e-15 6.844236e-24
11 F1-REF misclassification_ratio 1.581123e-53 3.487443e-35 2.408489e-24 2.376963e-23 1.024508e-15 2.798270e-11

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 novel-taxa datasets individually).


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



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



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



In [10]:
heatmap_from_data_frame(accuracy_results, metric="overclassification_ratio", rows=["Method", "Parameters"], cols=["Dataset", "level"])



In [11]:
heatmap_from_data_frame(accuracy_results, metric="underclassification_ratio", rows=["Method", "Parameters"], cols=["Dataset", "level"])



In [12]:
heatmap_from_data_frame(accuracy_results, metric="misclassification_ratio", rows=["Method", "Parameters"], cols=["Dataset", "level"])


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 [13]:
for method in accuracy_results['Method'].unique():
    top_params = parameter_comparisons(accuracy_results[accuracy_results["level"] == 6], 
                                       method, metrics=["Precision", "Recall", 
                                                        "overclassification_ratio", 
                                                        "underclassification_ratio", 
                                                        "misclassification_ratio",
                                                        "F-measure"],
                                       ascending={"Precision": False, "Recall": False, 
                                                        "overclassification_ratio": True, 
                                                        "underclassification_ratio": True, 
                                                        "misclassification_ratio": True,
                                                        "F-measure": False},
                                       sample_col='Dataset', method_col='Method',
                                       dataset_col='Dataset')
    display(Markdown('## {0}'.format(method)))
    display(top_params[:5])


blast

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
0.001 6 6 6 2 1 6
1 6 6 6 2 1 6
1000 6 6 6 2 1 6
1e-10 6 6 6 2 2 6

blast+

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
0.001:10:0.51:0.8 6.0 3.0 6.0 0.0 6.0 0.0
0.001:10:0.99:0.8 4.0 6.0 1.0 6.0 6.0 0.0
0.001:10:0.51:0.97 3.0 0.0 3.0 0.0 6.0 0.0
0.001:10:0.99:0.97 1.0 1.0 1.0 3.0 6.0 0.0
0.001:10:0.51:0.99 0.0 0.0 0.0 3.0 6.0 0.0

rdp

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
0.8 6.0 0.0 6.0 6.0 2.0 0.0
0.9 6.0 3.0 6.0 6.0 6.0 0.0
1.0 6.0 6.0 5.0 6.0 6.0 0.0
0.7 4.0 0.0 6.0 4.0 0.0 0.0
0.6 1.0 0.0 4.0 0.0 0.0 0.0

sortmerna

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
1.0:0.8:3:0.9:1.0 6.0 6.0 6.0 6.0 6.0 0.0
0.51:0.9:5:0.8:1.0 6.0 3.0 4.0 0.0 6.0 0.0
0.76:0.9:3:0.8:1.0 6.0 6.0 3.0 6.0 6.0 0.0
1.0:0.8:5:0.9:1.0 6.0 6.0 6.0 6.0 6.0 0.0
0.76:0.8:5:0.9:1.0 6.0 6.0 6.0 5.0 6.0 0.0

uclust

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
1.0:0.9:5 6.0 6.0 4.0 6.0 6.0 0.0
1.0:0.8:3 6.0 6.0 6.0 6.0 6.0 0.0
0.51:0.8:5 6.0 3.0 6.0 0.0 6.0 0.0
1.0:0.9:3 6.0 6.0 6.0 6.0 6.0 0.0
0.51:0.9:5 6.0 3.0 6.0 3.0 6.0 0.0

vsearch

F-measure Precision Recall misclassification_ratio overclassification_ratio underclassification_ratio
10:0.51:0.8 6.0 3.0 5.0 3 6.0 2.0
10:0.99:0.8 3.0 6.0 1.0 6 6.0 0.0
10:0.51:0.99 0.0 0.0 0.0 3 6.0 0.0
10:0.99:0.99 0.0 0.0 0.0 6 6.0 0.0
1:0.51:0.8 0.0 0.0 0.0 3 0.0 6.0

Rank performance of optimized methods

Now we rank the top-performing method/parameter combination for each method at species level. 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 [14]:
rank_optimized_method_performance_by_dataset(accuracy_results, dataset="Dataset",
                                             metric="F-measure",
                                             level="level",
                                             level_range=range(6,7),
                                             display_fields=["Method",
                                                             "Parameters",
                                                             "Precision",
                                                             "Recall",
                                                             "F-measure",
                                                             "overclassification_ratio",
                                                             "underclassification_ratio",
                                                             "misclassification_ratio"],
                                             paired=True,
                                             parametric=True,
                                             color=None,
                                             color_pallette=color_pallette)


B1-REF level 6

Method Parameters Precision Recall F-measure overclassification_ratio underclassification_ratio misclassification_ratio
3 sortmerna 1.0:0.8:5:0.8:1.0 0.655971 0.312642 0.423304 0.117930 0.518544 0.050884
4 uclust 0.76:0.8:5 0.588096 0.325494 0.418949 0.136205 0.434570 0.103731
1 blast+ 0.001:10:0.51:0.8 0.498477 0.304266 0.377878 0.149445 0.366501 0.178379
5 vsearch 10:0.51:0.8 0.486433 0.298671 0.370055 0.163164 0.354123 0.183104
2 rdp 0.9 0.440478 0.242460 0.312760 0.250488 0.447655 0.059397
0 blast 0.001 0.000000 0.000000 0.000000 0.730808 0.000000 0.269192
Method A Method B P
0 blast blast+ 0.000811
1 blast rdp 0.006576
2 blast sortmerna 0.003410
3 blast uclust 0.009514
4 blast vsearch 0.007376
5 blast+ rdp 0.106850
6 blast+ sortmerna 0.155534
7 blast+ uclust 0.266088
8 blast+ vsearch 0.735837
9 rdp sortmerna 0.085166
10 rdp uclust 0.032819
11 rdp vsearch 0.099639
12 sortmerna uclust 0.823436
13 sortmerna vsearch 0.266088
14 uclust vsearch 0.099639

F1-REF level 6

Method Parameters Precision Recall F-measure overclassification_ratio underclassification_ratio misclassification_ratio
4 uclust 1.0:0.8:3 0.727888 0.402913 0.518689 0.103166 0.438252 0.055670
3 sortmerna 1.0:0.8:3:0.9:1.0 0.675696 0.388649 0.493440 0.115928 0.408362 0.087061
1 blast+ 0.001:10:0.51:0.8 0.703189 0.352098 0.469214 0.066806 0.440073 0.141023
2 rdp 0.9 0.592356 0.330554 0.424299 0.194068 0.438262 0.037116
5 vsearch 10:0.51:0.8 0.735864 0.268488 0.392185 0.056058 0.599296 0.076159
0 blast 0.001 0.000000 0.000000 0.000000 0.649630 0.000000 0.350370
Method A Method B P
0 blast blast+ 0.001067
1 blast rdp 0.001166
2 blast sortmerna 0.000502
3 blast uclust 0.000272
4 blast vsearch 0.041172
5 blast+ rdp 0.135021
6 blast+ sortmerna 0.201804
7 blast+ uclust 0.108362
8 blast+ vsearch 0.201804
9 rdp sortmerna 0.007526
10 rdp uclust 0.011038
11 rdp vsearch 0.391810
12 sortmerna uclust 0.105431
13 sortmerna vsearch 0.201804
14 uclust vsearch 0.201804

In [ ]: