def test_confusion_matrix_from_data(self): """confusion_matrix_from_data should work for binary and quantitative data""" #binary test exp = [1,1,1,0,0,0,1,1,1,0,1,0,1] obs = [1,1,0,1,0,0,1,1,1,1,1,1,0] exp_tp = 6.0 exp_fp = 3.0 exp_fn = 2.0 exp_tn = 2.0 tp,fp,fn,tn = confusion_matrix_from_data(obs,exp) self.assertEqual([tp,fp,fn,tn],\ [exp_tp,exp_fp,exp_fn,exp_tn]) #quantitative test #results should be indentical to binary test #since only presence/absence is considered exp = [1,1,1,0,0,0,1,1,1,0,1,0,1] obs = [13.7,6.5,0,1,0,0,2.3,1,1.0,1.3,1.5,1,0] exp_tp = 6.0 exp_fp = 3.0 exp_fn = 2.0 exp_tn = 2.0 tp,fp,fn,tn = confusion_matrix_from_data(obs,exp) self.assertEqual([tp,fp,fn,tn],\ [exp_tp,exp_fp,exp_fn,exp_tn])
def test_roc_points(self): """roc_points should calculate the points for a Receiver Operating Characteristics curve """ #The set up here is a bit elaborate since I generate the test datasets #based on the values we need in the confusion matrix. #I test the intermediate results though, so any errors should be due #to the actual function, not the test tn_obs = 0 tn_exp = 0 fp_obs = 1 fp_exp = 0 tp_obs = 1 tp_exp = 1 fn_obs = 0 fn_exp = 1 #point A obs = [tp_obs] * 63 + [fp_obs] *28 + [fn_obs] * 37 + [tn_obs]*72 exp = [tp_exp] * 63 + [fp_exp] *28 + [fn_exp] * 37 + [tn_exp]*72 trial_a_results = confusion_matrix_from_data(obs,exp) #Check that this is correct self.assertEqual(trial_a_results,(63,28,37,72)) trial_a = (obs,exp) #point B obs = [tp_obs] * 77 + [fp_obs] *77 + [fn_obs] * 23 + [tn_obs]*23 exp = [tp_exp] * 77 + [fp_exp] *77 + [fn_exp] * 23 + [tn_exp]*23 trial_b_results = confusion_matrix_from_data(obs,exp) #Check that this is correct self.assertEqual(trial_b_results,(77,77,23,23)) trial_b = (obs,exp) #point c obs = [tp_obs] * 24 + [fp_obs] *88 + [fn_obs] * 76 + [tn_obs]*12 exp = [tp_exp] * 24 + [fp_exp] *88 + [fn_exp] * 76 + [tn_exp]*12 trial_c_results = confusion_matrix_from_data(obs,exp) #Check that this is correct self.assertEqual(trial_c_results,(24,88,76,12)) trial_c_results = calculate_accuracy_stats_from_observations(obs,exp) #Check that this is correct self.assertFloatEqual(trial_c_results["false_positive_rate"],0.88) trial_c = (obs,exp) trials = [trial_a, trial_b,trial_c] #Finally the actual test obs_points = roc_points(trials) exp_points = [(0.28,0.63),(0.77,0.77),(0.88,0.24)] self.assertFloatEqual(obs_points,exp_points)