# Path: picrust/evaluate_test_datasets.py
# def calculate_accuracy_stats_from_observations(obs,exp,success_criterion='binary',\
#   verbose=False):
#     """Return statistics derived from the confusion matrix
#     obs -- a list of floats representing observed values
#     exp -- a list of floats for expected values (same order as obs)
#     verbose -- print verbose output
#     """
# 
#     tp,fp,fn,tn =\
#       confusion_matrix_from_data(obs,exp,success_criterion=success_criterion,verbose=verbose)
# 
#     result =\
#       calculate_accuracy_stats_from_confusion_matrix(tp,fp,fn,tn,verbose=verbose)
# 
#     result['sum_expected']=sum(exp)
#     result['sum_observed']=sum(obs)
# 
#     #pearson correlation (using new method from pycogent 1.5.3)
#     pearson_r,pearson_para_p,pearson_permuted_r,pearson_nonpara_p,pearson_ci =correlation_test(obs,exp,method='pearson',permutations=0)
#     result['pearson_r']=pearson_r
#     #result['pearson_permuted_r']=pearson_permuted_r
#     #result['pearson_nonpara_p']=pearson_nonpara_p
#     result['pearson_p']=pearson_para_p
#     #result['pearson_ci']=pearson_ci
#     result['pearson_r2']=pearson_r**2
# 
#     #spearman correlation (using new method from pycogent 1.5.3)
#     spearman_r,spearman_para_p,spearman_permuted_r,spearman_nonpara_p,spearman_ci =correlation_test(obs,exp,method='spearman',permutations=0)
#     result['spearman_r']=spearman_r
#     #result['spearman_permuted_r']=spearman_permuted_r
#     #result['spearman_nonpara_p']=spearman_nonpara_p
#     result['spearman_p']=spearman_para_p
#     #result['spearman_ci']=spearman_ci
#     result['spearman_r2']=spearman_r**2
# 
#     #add in correlations (old)
#     #pearson_r,pearson_t_prob =correlation(obs,exp)
#     #result['pearson']=pearson_r
#     #result['pearson_prob']=pearson_t_prob
#     #result['pearson_r2']=pearson_r**2
# 
#     #spearman_r,spearman_t_prob =spearman_correlation(obs,exp)
#     #result['spearman']=spearman_r
#     #result['spearman_prob']=spearman_t_prob
#     #result['spearman_r2']=spearman_r**2
#     return result
# 
# Path: picrust/util.py
# def make_output_dir_for_file(filepath):
#     """Create sub-directories for a new file if they don't already exist"""
#     dirpath = dirname(filepath)
#     if not isdir(dirpath) and not dirpath == '':
#         makedirs(dirpath)


from collections import defaultdict
from os import listdir
from numpy import array,ravel,asarray
from os.path import join,basename
from cogent.util.option_parsing import parse_command_line_parameters,\
  make_option
from picrust.evaluate_test_datasets import calculate_accuracy_stats_from_observations
from biom import load_table, Table
from picrust.util import make_output_dir_for_file
from random import shuffle
    delimiter="\t"


    for observed_file in observed_files:
        observed_file_name = basename(observed_file)

        if verbose:
            print "Loading predicted trait table file:",observed_file_name

        obs_table = load_table(observed_file)

        if opts.compare_observations:
            if verbose:
                print "Transposing tables to allow evaluation of observations (instead of samples)..."
            obs_table = obs_table.transpose()
            exp_table = exp_table.transpose()

        if verbose:
           print "Matching predicted and expected tables..."

        obs,exp=match_biom_tables(obs_table,exp_table,verbose=verbose,limit_to_expected_observations=opts.limit_to_expected_observations,limit_to_observed_observations=opts.limit_to_observed_observations,normalize=opts.normalize,shuffle_samples=opts.shuffle_samples)

        if verbose:
            print "Calculating accuracy stats for all observations..."

        #import pdb; pdb.set_trace()
        for i in obs:
            if verbose:
                print "Calculating stats for: ",i
            if opts.not_relative_abundance_scores:

# Path: picrust/evaluate_test_datasets.py
# def calculate_accuracy_stats_from_observations(obs,exp,success_criterion='binary',\
#   verbose=False):
#     """Return statistics derived from the confusion matrix
#     obs -- a list of floats representing observed values
#     exp -- a list of floats for expected values (same order as obs)
#     verbose -- print verbose output
#     """
# 
#     tp,fp,fn,tn =\
#       confusion_matrix_from_data(obs,exp,success_criterion=success_criterion,verbose=verbose)
# 
#     result =\
#       calculate_accuracy_stats_from_confusion_matrix(tp,fp,fn,tn,verbose=verbose)
# 
#     result['sum_expected']=sum(exp)
#     result['sum_observed']=sum(obs)
# 
#     #pearson correlation (using new method from pycogent 1.5.3)
#     pearson_r,pearson_para_p,pearson_permuted_r,pearson_nonpara_p,pearson_ci =correlation_test(obs,exp,method='pearson',permutations=0)
#     result['pearson_r']=pearson_r
#     #result['pearson_permuted_r']=pearson_permuted_r
#     #result['pearson_nonpara_p']=pearson_nonpara_p
#     result['pearson_p']=pearson_para_p
#     #result['pearson_ci']=pearson_ci
#     result['pearson_r2']=pearson_r**2
# 
#     #spearman correlation (using new method from pycogent 1.5.3)
#     spearman_r,spearman_para_p,spearman_permuted_r,spearman_nonpara_p,spearman_ci =correlation_test(obs,exp,method='spearman',permutations=0)
#     result['spearman_r']=spearman_r
#     #result['spearman_permuted_r']=spearman_permuted_r
#     #result['spearman_nonpara_p']=spearman_nonpara_p
#     result['spearman_p']=spearman_para_p
#     #result['spearman_ci']=spearman_ci
#     result['spearman_r2']=spearman_r**2
# 
#     #add in correlations (old)
#     #pearson_r,pearson_t_prob =correlation(obs,exp)
#     #result['pearson']=pearson_r
#     #result['pearson_prob']=pearson_t_prob
#     #result['pearson_r2']=pearson_r**2
# 
#     #spearman_r,spearman_t_prob =spearman_correlation(obs,exp)
#     #result['spearman']=spearman_r
#     #result['spearman_prob']=spearman_t_prob
#     #result['spearman_r2']=spearman_r**2
#     return result
# 
# Path: picrust/util.py
# def make_output_dir_for_file(filepath):
#     """Create sub-directories for a new file if they don't already exist"""
#     dirpath = dirname(filepath)
#     if not isdir(dirpath) and not dirpath == '':
#         makedirs(dirpath)


# Path: scripts/compare_biom.py
from collections import defaultdict
from os import listdir
from numpy import array,ravel,asarray
from os.path import join,basename
from cogent.util.option_parsing import parse_command_line_parameters,\
  make_option
from picrust.evaluate_test_datasets import calculate_accuracy_stats_from_observations
from biom import load_table, Table
from picrust.util import make_output_dir_for_file
from random import shuffle

    delimiter="\t"


    for observed_file in observed_files:
        observed_file_name = basename(observed_file)

        if verbose:
            print "Loading predicted trait table file:",observed_file_name

        obs_table = load_table(observed_file)

        if opts.compare_observations:
            if verbose:
                print "Transposing tables to allow evaluation of observations (instead of samples)..."
            obs_table = obs_table.transpose()
            exp_table = exp_table.transpose()

        if verbose:
           print "Matching predicted and expected tables..."

        obs,exp=match_biom_tables(obs_table,exp_table,verbose=verbose,limit_to_expected_observations=opts.limit_to_expected_observations,limit_to_observed_observations=opts.limit_to_observed_observations,normalize=opts.normalize,shuffle_samples=opts.shuffle_samples)

        if verbose:
            print "Calculating accuracy stats for all observations..."

        #import pdb; pdb.set_trace()
        for i in obs:
            if verbose:
                print "Calculating stats for: ",i
            if opts.not_relative_abundance_scores:

                results=calculate_accuracy_stats_from_observations(obs[i],exp[i],success_criterion='binary')