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 [4]:
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 [5]:
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 [6]:
point = pointplot_from_data_frame(level_results, "level", y_vars,
                                  group_by="Dataset", color_by="Method",
                                  color_palette=color_palette)



In [7]:
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 [8]:
result = per_level_kruskal_wallis(level_results, y_vars, group_by='Method', 
                                  dataset_col='Dataset', alpha=0.05, 
                                  pval_correction='fdr_bh')
result


Out[8]:
Dataset Variable 1 2 3 4 5 6
0 F1-REF-FL Precision 9.033229e-84 4.932252e-61 2.640063e-70 1.309081e-62 1.703449e-57 1.267542e-74
1 F1-REF-FL Recall 6.702850e-135 8.291583e-90 1.283373e-76 2.914572e-75 2.918109e-84 1.294745e-53
2 F1-REF-FL F-measure 1.338238e-121 3.618864e-85 4.075347e-75 9.394210e-76 4.076531e-96 6.528475e-68
3 B1-REF-FL Precision 2.121709e-67 1.122153e-57 6.519551e-38 4.620913e-27 9.371285e-33 7.818720e-50
4 B1-REF-FL Recall 1.172918e-91 1.339179e-87 9.644333e-86 1.424603e-90 5.592487e-100 7.585728e-139
5 B1-REF-FL F-measure 5.590312e-84 1.062676e-77 2.185621e-76 6.191650e-81 1.508837e-104 2.528383e-90
6 F1-REF Precision 2.914994e-41 2.584764e-43 1.789329e-86 6.978102e-113 6.526361e-96 2.601665e-56
7 F1-REF Recall 8.418474e-164 4.331661e-130 2.993841e-117 6.239364e-111 4.211015e-117 2.429501e-103
8 F1-REF F-measure 6.843749e-149 7.013133e-122 1.364713e-112 1.071506e-111 3.789133e-123 2.952317e-103
9 B1-REF Precision 1.333450e-18 2.098784e-50 1.694184e-40 1.108418e-24 5.608904e-14 7.071744e-21
10 B1-REF Recall 3.089757e-73 4.205516e-76 3.861743e-99 8.866104e-143 1.098918e-182 1.708527e-209
11 B1-REF F-measure 2.256010e-76 1.206356e-82 3.280669e-108 4.222668e-158 2.676103e-258 4.262668e-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 [9]:
heatmap_from_data_frame(level_results, metric="Precision", rows=["Method", "Parameters"], cols=["Dataset", "level"])