-
Notifications
You must be signed in to change notification settings - Fork 15
/
result_plotting.py
149 lines (124 loc) · 5.91 KB
/
result_plotting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import shutil
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import argparse
# Argument parsing
parser = argparse.ArgumentParser(description='Script to plot differences between actual lepidopteran measurements versus predicted measurements.')
parser.add_argument('-a', '--actual',
type=str,
help='File name with actual measurements',
required=True)
parser.add_argument('-n', '--name',
type=str,
help='Name of column containing file name of each measured image',
required=True)
parser.add_argument('-l', '--left',
type=str,
help='Name of column containing left wing measurements in actual measurements file',
required=True)
parser.add_argument('-r', '--right',
type=str,
help='Name of column containing right wing measurements in actual measurements file',
required=True)
parser.add_argument('-p', '--predicted',
type=str,
help='File name with predicted measurement results (assumed to be a csv produced by our pipeline)',
default='results.csv')
parser.add_argument('-c', '--comparison',
action='store_true',
help='Produce an "comparison.csv" file containing all measurements and the differences')
parser.add_argument('-o', '--outliers',
action='store_true',
help='Produce an "outliers.csv" file containing only measurements that are deemed outliers')
parser.add_argument('-sd', '--sd',
type=float,
help="The number of SD's that are used as a theshold for classifying an outlier",
default=2)
parser.add_argument('-co', '--copy_outliers',
type=str,
help='Create a folder "outliers" and copy images from the specified path that correspond to outliers')
args = parser.parse_args()
# Reading in both actual measurements file, and select desired columns
actual_file, actual_ext = os.path.splitext(args.actual)
actual = pd.DataFrame()
if actual_ext.lower() == ".xlsx":
actual = pd.read_excel(args.actual)
elif actual_ext.lower() == ".csv":
actual = pd.read_csv(args.actual)
actual = actual[[args.name, args.left, args.right]]
actual.rename(columns={args.name: "image_id", args.left: "actual_left", args.right: "actual_right"}, inplace=True)
# Reading in predicted results from csv
predicted = pd.read_csv(args.predicted)
predicted.rename(columns={"left_wing (mm)": "predicted_left", "right_wing (mm)": "predicted_right"}, inplace=True)
# Merging them together and creating new columns for the difference
both = pd.merge(actual, predicted, on="image_id", how='inner')
both['left_diff'] = both['predicted_left'] - both['actual_left']
both['right_diff'] = both['predicted_right'] - both['actual_right']
all_diffs = both['right_diff'].append(both['left_diff'])
mean = np.mean(all_diffs)
sd = np.std(all_diffs)
both['left_SD'] = (both['left_diff'] - mean)/sd
both['right_SD'] = (both['right_diff'] - mean)/sd
both['is_outlier'] = (abs(both['left_SD'])>args.sd) | (abs(both['right_SD'])>args.sd)
# Print statistics about differences
lower = mean - args.sd * sd
upper = mean + args.sd * sd
num_outlier_measurements = len(all_diffs[(all_diffs < lower) | (all_diffs > upper)])
num_outlier_images = np.count_nonzero(both['is_outlier'])
print("DIFFERENCE STATISTICS")
print(f" Mean Differences: {mean}")
print(f" Differences SD: {sd}.")
print(f" Lower Bound (-{args.sd} SD) of Differences: {lower}")
print(f" Upper Bound (+{args.sd} SD) of Differences: {upper}")
print(f" Number of outlying measurements: {num_outlier_measurements}")
print(f" Number of images with outlying measurements: {num_outlier_images}")
print("")
# Plot histogram of differences
all_diffs_nonoutlier = all_diffs[(all_diffs >= lower) & (all_diffs <= upper)]
fig, ax = plt.subplots(figsize=(10, 5))
ax = all_diffs_nonoutlier.hist(bins='auto')
# Saving the plot
filename = 'result_plot.png'
output_path = os.path.normpath(filename)
plt.xlabel('Difference between (predicted - actual) in mm')
start, end = ax.get_xlim()
plt.ylabel('Number of images')
plt.title('Error in predicted measurements')
plt.savefig(output_path)
plt.close()
print(f"Saved plot of differences to {filename}")
# Printing either full comparison csv or outliers csv
both['SD_sum'] = abs(both['left_SD']) + abs(both['left_SD'])
both.sort_values('SD_sum', ascending=False, inplace=True)
both.drop('SD_sum', axis=1, inplace=True)
both.sort_values('is_outlier', ascending=False, inplace=True, kind='mergesort')
if args.comparison:
comparison_filename = 'comparison.csv'
outlier_col_str = both["is_outlier"].replace({True:"TRUE", False:""})
both_outlier_col_str = both.copy()
both_outlier_col_str["is_outlier"] = outlier_col_str
both_outlier_col_str.to_csv(comparison_filename)
print(f"Saved all differences to {comparison_filename}")
if args.outliers:
outliers_filename = 'outliers.csv'
both_outliers_only = both[both['is_outlier']].copy()
both_outliers_only.drop('is_outlier', axis=1, inplace=True)
both_outliers_only.to_csv(outliers_filename)
print(f'Saved {num_outlier_images} rows to {outliers_filename}')
# Fetching outlier images
if args.copy_outliers:
outliers_folder = 'outliers/'
if os.path.exists(outliers_folder):
oldList = os.listdir(outliers_folder)
for oldFile in oldList:
os.remove(os.path.join(outliers_folder, oldFile))
else:
os.mkdir(outliers_folder)
image_list = both[both['is_outlier']]['image_id']
print(f'Copying {len(image_list)} outlier images to {outliers_folder} ...', end="")
for image_name in image_list:
image_path = os.path.join(args.copy_outliers, image_name)
shutil.copy(image_path, outliers_folder)
print("done")