-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_functions_for_lab_checks.py
311 lines (261 loc) · 13.6 KB
/
print_functions_for_lab_checks.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND/intropylab-classifying-images/print_functions_for_lab_checks.py
#
# PROGRAMMER: Jennifer S.
# DATE CREATED: 05/14/2018
# REVISED DATE: <=(Date Revised - if any)
# PURPOSE: This set of functions can be used to check your code after programming
# each function. The top section of each part of the lab contains
# the section labeled 'Checking your code'. When directed within this
# section of the lab one can use these functions to more easily check
# your code. See the docstrings below each function for details on how
# to use the function within your code.
#
##
# Functions below defined to help with "Checking your code", specifically
# running these functions with the appropriate input arguments within the
# main() funtion will print out what's needed for "Checking your code"
#
def check_command_line_arguments(in_arg):
"""
For Lab: Classifying Images - 7. Command Line Arguments
Prints each of the command line arguments passed in as parameter in_arg,
assumes you defined all three command line arguments as outlined in
'7. Command Line Arguments'
Parameters:
in_arg -data structure that stores the command line arguments object
Returns:
Nothing - just prints to console
"""
if in_arg is None:
print("* Doesn't Check the Command Line Arguments because 'get_input_args' hasn't been defined.")
else:
# prints command line agrs
print("Command Line Arguments:\n dir =", in_arg.dir,
"\n arch =", in_arg.arch, "\n dogfile =", in_arg.dogfile)
def check_creating_pet_image_labels(results_dic):
""" For Lab: Classifying Images - 9/10. Creating Pet Image Labels
Prints first 10 key-value pairs and makes sure there are 40 key-value
pairs in your results_dic dictionary. Assumes you defined the results_dic
dictionary as was outlined in
'9/10. Creating Pet Image Labels'
Parameters:
results_dic - Dictionary with key as image filename and value as a List
(index)idx 0 = pet image label (string)
Returns:
Nothing - just prints to console
"""
if results_dic is None:
print("* Doesn't Check the Results Dictionary because 'get_pet_labels' hasn't been defined.")
else:
# Code to print 10 key-value pairs (or fewer if less than 10 images)
# & makes sure there are 40 pairs, one for each file in pet_images/
stop_point = len(results_dic)
if stop_point > 10:
stop_point = 10
print("\nPet Image Label Dictionary has", len(results_dic),
"key-value pairs.\nBelow are", stop_point, "of them:")
# counter - to count how many labels have been printed
n = 0
# for loop to iterate through the dictionary
for key in results_dic:
# prints only first 10 labels
if n < stop_point:
print("{:2d} key: {:>30} label: {:>26}".format(n+1, key,
results_dic[key][0]) )
# Increments counter
n += 1
# If past first 10 (or fewer) labels the breaks out of loop
else:
break
def check_classifying_images(results_dic):
""" For Lab: Classifying Images - 11/12. Classifying Images
Prints Pet Image Label and Classifier Label for ALL Matches followed by ALL
NOT matches. Next prints out the total number of images followed by how
many were matches and how many were not-matches to check all 40 images are
processed. Assumes you defined the results_dic dictionary as was
outlined in '11/12. Classifying Images'
Parameters:
results_dic - Dictionary with key as image filename and value as a List
(index)idx 0 = pet image label (string)
idx 1 = classifier label (string)
idx 2 = 1/0 (int) where 1 = match between pet image and
classifer labels and 0 = no match between labels
Returns:
Nothing - just prints to console
"""
if results_dic is None:
print("* Doesn't Check the Results Dictionary because 'classify_images' hasn't been defined.")
elif len(results_dic[next(iter(results_dic))]) < 2:
print("* Doesn't Check the Results Dictionary because 'classify_images' hasn't been defined.")
else:
# Code for checking classify_images -
# Checks matches and not matches are classified correctly
# Checks that all 40 images are classified as a Match or Not-a Match
# Sets counters for matches & NOT-matches
n_match = 0
n_notmatch = 0
# Prints all Matches first
print("\n MATCH:")
for key in results_dic:
# Prints only if a Match Index 2 == 1
if results_dic[key][2] == 1:
# Increments Match counter
n_match += 1
print("\n{:>30}: \nReal: {:>26} Classifier: {:>30}".format(key,
(results_dic[key][0]), (results_dic[key][1])))
# Prints all NOT-Matches next
print("\n NOT A MATCH:")
for key in results_dic:
# Prints only if NOT-a-Match Index 2 == 0
if results_dic[key][2] == 0:
# Increments Not-a-Match counter
n_notmatch += 1
print("\n{:>30}: \nReal: {:>26} Classifier: {:>30}".format(key,
(results_dic)[key][0], (results_dic)[key][1]))
# Prints Total Number of Images - expects 40 from pet_images folder
print("\n# Total Images",n_match + n_notmatch, "# Matches:",n_match ,
"# NOT Matches:",n_notmatch)
def check_classifying_labels_as_dogs(results_dic):
""" For Lab: Classifying Images - 13. Classifying Labels as Dogs
Prints Pet Image Label, Classifier Label, whether Pet Label is-a-dog(1=Yes,
0=No), and whether Classifier Label is-a-dog(1=Yes, 0=No) for ALL Matches
followed by ALL NOT matches. Next prints out the total number of images
followed by how many were matches and how many were not-matches to check
all 40 images are processed. Assumes you defined the results_dic dictionary
as was outlined in '13. Classifying Labels as Dogs'
Parameters:
results_dic - Dictionary with key as image filename and value as a List
(index)idx 0 = pet image label (string)
idx 1 = classifier label (string)
idx 2 = 1/0 (int) where 1 = match between pet image and
classifer labels and 0 = no match between labels
idx 3 = 1/0 (int) where 1 = pet image 'is-a' dog and
0 = pet Image 'is-NOT-a' dog.
idx 4 = 1/0 (int) where 1 = Classifier classifies image
'as-a' dog and 0 = Classifier classifies image
'as-NOT-a' dog.
Returns:
Nothing - just prints to console
"""
if results_dic is None:
print("* Doesn't Check the Results Dictionary because 'adjust_results4_isadog' hasn't been defined.")
elif len(results_dic[next(iter(results_dic))]) < 4 :
print("* Doesn't Check the Results Dictionary because 'adjust_results4_isadog' hasn't been defined.")
else:
# Code for checking adjust_results4_isadog -
# Checks matches and not matches are classified correctly as "dogs" and
# "not-dogs" Checks that all 40 images are classified as a Match or Not-a
# Match
# Sets counters for matches & NOT-matches
n_match = 0
n_notmatch = 0
# Prints all Matches first
print("\n MATCH:")
for key in results_dic:
# Prints only if a Match Index 2 == 1
if results_dic[key][2] == 1:
# Increments Match counter
n_match += 1
print("\n{:>30}: \nReal: {:>26} Classifier: {:>30} \nPetLabelDog: {:1d} ClassLabelDog: {:1d}".format(key,
results_dic[key][0], results_dic[key][1], results_dic[key][3],
results_dic[key][4]))
# Prints all NOT-Matches next
print("\n NOT A MATCH:")
for key in results_dic:
# Prints only if NOT-a-Match Index 2 == 0
if results_dic[key][2] == 0:
# Increments Not-a-Match counter
n_notmatch += 1
print("\n{:>30}: \nReal: {:>26} Classifier: {:>30} \nPetLabelDog: {:1d} ClassLabelDog: {:1d}".format(key,
results_dic[key][0], results_dic[key][1], results_dic[key][3],
results_dic[key][4]))
# Prints Total Number of Images - expects 40 from pet_images folder
print("\n# Total Images",n_match + n_notmatch, "# Matches:",n_match ,
"# NOT Matches:",n_notmatch)
def check_calculating_results(results_dic, results_stats_dic):
""" For Lab: Classifying Images - 14. Calculating Results
Prints First statistics from the results stats dictionary (that was created
by the calculates_results_stats() function), then prints the same statistics
that were calculated in this function using the results dictionary.
Assumes you defined the results_stats dictionary and the statistics
as was outlined in '14. Calculating Results '
Parameters:
results_dic - Dictionary with key as image filename and value as a List
(index)idx 0 = pet image label (string)
idx 1 = classifier label (string)
idx 2 = 1/0 (int) where 1 = match between pet image and
classifer labels and 0 = no match between labels
idx 3 = 1/0 (int) where 1 = pet image 'is-a' dog and
0 = pet Image 'is-NOT-a' dog.
idx 4 = 1/0 (int) where 1 = Classifier classifies image
'as-a' dog and 0 = Classifier classifies image
'as-NOT-a' dog.
results_stats_dic - Dictionary that contains the results statistics (either
a percentage or a count) where the key is the statistic's
name (starting with 'pct' for percentage or 'n' for count)
and the value is the statistic's value
Returns:
Nothing - just prints to console
"""
if results_stats_dic is None:
print("* Doesn't Check the Results Dictionary because 'calculates_results_stats' hasn't been defined.")
else:
# Code for checking results_stats_dic -
# Checks calculations of counts & percentages BY using results_dic
# to re-calculate the values and then compare to the values
# in results_stats_dic
# Initialize counters to zero and number of images total
n_images = len(results_dic)
n_pet_dog = 0
n_class_cdog = 0
n_class_cnotd = 0
n_match_breed = 0
# Interates through results_dic dictionary to recompute the statistics
# outside of the calculates_results_stats() function
for key in results_dic:
# match (if dog then breed match)
if results_dic[key][2] == 1:
# isa dog (pet label) & breed match
if results_dic[key][3] == 1:
n_pet_dog += 1
# isa dog (classifier label) & breed match
if results_dic[key][4] == 1:
n_class_cdog += 1
n_match_breed += 1
# NOT dog (pet_label)
else:
# NOT dog (classifier label)
if results_dic[key][4] == 0:
n_class_cnotd += 1
# NOT - match (not a breed match if a dog)
else:
# NOT - match
# isa dog (pet label)
if results_dic[key][3] == 1:
n_pet_dog += 1
# isa dog (classifier label)
if results_dic[key][4] == 1:
n_class_cdog += 1
# NOT dog (pet_label)
else:
# NOT dog (classifier label)
if results_dic[key][4] == 0:
n_class_cnotd += 1
# calculates statistics based upon counters from above
n_pet_notd = n_images - n_pet_dog
pct_corr_dog = ( n_class_cdog / n_pet_dog )*100
pct_corr_notdog = ( n_class_cnotd / n_pet_notd )*100
pct_corr_breed = ( n_match_breed / n_pet_dog )*100
# prints calculated statistics
print("\n ** Statistics from calculates_results_stats() function:")
print("N Images: {:2d} N Dog Images: {:2d} N NotDog Images: {:2d} \nPct Corr dog: {:5.1f} Pct Corr NOTdog: {:5.1f} Pct Corr Breed: {:5.1f}".format(
results_stats_dic['n_images'], results_stats_dic['n_dogs_img'],
results_stats_dic['n_notdogs_img'], results_stats_dic['pct_correct_dogs'],
results_stats_dic['pct_correct_notdogs'],
results_stats_dic['pct_correct_breed']))
print("\n ** Check Statistics - calculated from this function as a check:")
print("N Images: {:2d} N Dog Images: {:2d} N NotDog Images: {:2d} \nPct Corr dog: {:5.1f} Pct Corr NOTdog: {:5.1f} Pct Corr Breed: {:5.1f}".format(
n_images, n_pet_dog, n_pet_notd, pct_corr_dog, pct_corr_notdog,
pct_corr_breed))