forked from Erotemic/ibeis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.py
executable file
·386 lines (331 loc) · 12.3 KB
/
dev.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python2.7
"""
This is a hacky script meant to be run interactively
"""
# TODO: ADD COPYRIGHT TAG
from __future__ import absolute_import, division, print_function
import multiprocessing
# Dev
from _devscript import devcmd, DEVCMD_FUNCTIONS
import utool
import ibeis
if __name__ == '__main__':
multiprocessing.freeze_support()
ibeis._preload()
from ibeis.dev.all_imports import * # NOQA
#utool.util_importer.dynamic_import(__name__, ('_devcmds_ibeis', None),
# developing=True)
from _devcmds_ibeis import * # NOQA
# Tools
from plottool import draw_func2 as df2
# IBEIS
from ibeis.dev import main_helpers
from ibeis.dev import dbinfo
from ibeis.viz import interact
from ibeis.dev import experiment_configs
from ibeis.dev import experiment_harness
from ibeis.dev import results_all
print, print_, printDBG, rrr, profile = utool.inject(__name__, '[dev]', DEBUG=False)
#--------------------
# RUN DEV EXPERIMENTS
#--------------------
#@utool.indent_func('[dev]')
@profile
def run_experiments(ibs, qaid_list):
"""
This function runs tests passed in with the -t flag
"""
print('\n')
print('[dev] run_experiments')
print('==========================')
print('RUN EXPERIMENTS %s' % ibs.get_dbname())
print('==========================')
input_test_list = params.args.tests[:]
print('input_test_list = %r' % (input_test_list,))
# fnum = 1
valid_test_list = [] # build list for printing in case of failure
valid_test_helpstr_list = [] # for printing
def intest(*args, **kwargs):
helpstr = kwargs.get('help', '')
valid_test_helpstr_list.append(' -t ' + ', '.join(args) + helpstr)
for testname in args:
valid_test_list.append(testname)
ret = testname in input_test_list
ret2 = testname in params.unknown # Let unparsed args count towards tests
if ret or ret2:
if ret:
input_test_list.remove(testname)
else:
ret = ret2
print('+===================')
print('| running testname = %s' % (args,))
return ret
return False
valid_test_helpstr_list.append(' # --- Simple Tests ---')
# Explicit (simple) test functions
if intest('export'):
export(ibs)
if intest('dbinfo'):
dbinfo.get_dbinfo(ibs)
if intest('info'):
print(ibs.get_infostr())
if intest('printcfg'):
printcfg(ibs)
if intest('tables'):
ibs.print_tables()
if intest('imgtbl'):
ibs.print_image_table()
valid_test_helpstr_list.append(' # --- Decor Tests ---')
locals_ = locals()
# Implicit (decorated) test functions
for (func_aliases, func) in DEVCMD_FUNCTIONS:
if intest(*func_aliases):
with utool.Indenter('[dev.' + func.func_name + ']'):
ret = func(ibs, qaid_list)
if isinstance(ret, dict):
locals_.update(ret)
valid_test_helpstr_list.append(' # --- Config Tests ---')
# Config driven test functions
# Allow any testcfg to be in tests like: vsone_1 or vsmany_3
for test_cfg_name in experiment_configs.TEST_NAMES:
if intest(test_cfg_name):
test_cfg_name_list = [test_cfg_name]
fnum = df2.next_fnum()
experiment_harness.test_configurations(ibs, qaid_list, test_cfg_name_list, fnum)
valid_test_helpstr_list.append(' # --- Help ---')
if intest('help'):
print('valid tests are:')
print('\n'.join(valid_test_helpstr_list))
return
if len(input_test_list) > 0:
print('valid tests are: \n')
print('\n'.join(valid_test_list))
raise Exception('Unknown tests: %r ' % input_test_list)
return locals_
#-------------------
# CUSTOM DEV FUNCS
#-------------------
__ALLRES_CACHE__ = {}
def get_allres(ibs, qaid_list):
print('[dev] get_allres')
allres_cfgstr = ibs.qreq.get_cfgstr()
try:
allres = __ALLRES_CACHE__[allres_cfgstr]
except KeyError:
qaid2_qres = ibs.query_all(qaid_list)
allres = results_all.init_allres(ibs, qaid2_qres)
# Cache save
__ALLRES_CACHE__[allres_cfgstr] = allres
return allres
#------------------
# DEV DEVELOPMENT
#------------------
# This is where you write all of the functions that will become pristine
# and then go in _devcmds_ibeis.py
@devcmd('dists', 'dist', 'desc_dists')
def desc_dists(ibs, qaid_list):
""" Plots the distances between matching descriptors
with groundtruth (true/false) data """
print('[dev] desc_dists')
allres = get_allres(ibs, qaid_list)
# Get the descriptor distances of true matches
orgtype_list = ['top_false', 'true']
disttype = 'L2'
orgres2_distmap = results_analyzer.get_orgres_desc_match_dists(allres, orgtype_list)
results_analyzer.print_desc_distances_map(orgres2_distmap)
#true_desc_dists = orgres2_distmap['true']['L2']
#false_desc_dists = orgres2_distmap['false']['L2']
#scores_list = [false_desc_dists, true_desc_dists]
dists_list = [orgres2_distmap[orgtype][disttype] for orgtype in orgtype_list]
dists_lbls = orgtype_list
dists_markers = ['x', 'o--']
plottool.plots.draw_scores_cdf(dists_list, dists_lbls, dists_markers)
df2.set_figtitle('Descriptor Distance CDF d(x)' + ibs.qreq.get_cfgstr())
return locals()
@devcmd('inspect')
def inspect_matches(ibs, qaid_list):
print('<inspect_matches>')
from ibeis.gui import inspect_gui
from ibeis.viz.interact import interact_qres2 # NOQA
allres = get_allres(ibs, qaid_list)
guitool.ensure_qapp()
tblname = 'qres'
qaid2_qres = allres.qaid2_qres
ranks_lt = 5
# This object is created inside QresResultsWidget
#qres_api = inspect_gui.make_qres_api(ibs, qaid2_qres) # NOQA
# This is where you create the result widigt
print('[inspect_matches] make_qres_widget')
qres_wgt = inspect_gui.QueryResultsWidget(ibs, qaid2_qres, ranks_lt=ranks_lt)
print('[inspect_matches] show')
qres_wgt.show()
print('[inspect_matches] raise')
qres_wgt.raise_()
#query_review = interact_qres2.Interact_QueryResult(ibs, qaid2_qres)
#self = interact_qres2.Interact_QueryResult(ibs, qaid2_qres, ranks_lt=ranks_lt)
print('</inspect_matches>')
# simulate double click
qres_wgt._on_click(qres_wgt.model.index(2, 2))
#qres_wgt._on_doubleclick(qres_wgt.model.index(2, 0))
return locals()
@devcmd('scores', 'score')
def annotationmatch_scores(ibs, qaid_list):
print('[dev] annotationmatch_scores')
allres = get_allres(ibs, qaid_list)
# Get the descriptor distances of true matches
orgtype_list = ['false', 'true']
orgtype_list = ['top_false', 'top_true']
#markers_map = {'false': 'o', 'true': 'o-', 'top_true': 'o-', 'top_false': 'o'}
markers_map = defaultdict(lambda: 'o')
cmatch_scores_map = results_analyzer.get_orgres_annotationmatch_scores(allres, orgtype_list)
results_analyzer.print_annotationmatch_scores_map(cmatch_scores_map)
#true_cmatch_scores = cmatch_scores_map['true']
#false_cmatch_scores = cmatch_scores_map['false']
scores_list = [cmatch_scores_map[orgtype] for orgtype in orgtype_list]
scores_lbls = orgtype_list
scores_markers = [markers_map[orgtype] for orgtype in orgtype_list]
plottool.plots.draw_scores_cdf(scores_list, scores_lbls, scores_markers)
df2.set_figtitle('Chipmatch Scores ' + ibs.qreq.get_cfgstr())
return locals()
@devcmd('gv')
def gvcomp(ibs, qaid_list):
"""
GV = With gravity vector
RI = With rotation invariance
"""
print('[dev] gvcomp')
def testcomp(ibs, qaid_list):
allres = get_allres(ibs, qaid_list)
for qaid in qaid_list:
qres = allres.get_qres(qaid)
interact.ishow_qres(ibs, qres, annote_mode=2)
return allres
ibs_GV = ibs
ibs_RI = ibs.clone_handle(nogravity_hack=True)
allres_GV = testcomp(ibs_GV, qaid_list)
allres_RI = testcomp(ibs_RI, qaid_list)
return locals()
def get_ibslist(ibs):
print('[dev] get_ibslist')
ibs_GV = ibs
ibs_RI = ibs.clone_handle(nogravity_hack=True)
ibs_RIW = ibs.clone_handle(nogravity_hack=True, gravity_weighting=True)
ibs_list = [ibs_GV, ibs_RI, ibs_RIW]
return ibs_list
@devcmd('gv_scores')
def compgrav_annotationmatch_scores(ibs, qaid_list):
print('[dev] compgrav_annotationmatch_scores')
ibs_list = get_ibslist(ibs)
for ibs_ in ibs_list:
annotationmatch_scores(ibs_, qaid_list)
#------------------
# DEV MAIN
#------------------
def dev_snippets(main_locals):
""" Common variables for convineince when interacting with IPython """
print('[dev] dev_snippets')
species = 'zebra_grevys'
quick = True
fnum = 1
# Get reference to IBEIS Controller
ibs = main_locals['ibs']
if 'back' in main_locals:
# Get reference to GUI Backend
back = main_locals['back']
if back is not None:
# Get reference to GUI Frontend
front = getattr(back, 'front', None)
ibswgt = front
view = ibswgt.views['images']
model = ibswgt.models['names_tree']
selection_model = view.selectionModel()
if ibs is not None:
#ibs.dump_tables()
aid_list = ibs.get_valid_aids()
gid_list = ibs.get_valid_gids()
nid_list = ibs.get_valid_nids()
#valid_nid_list = ibs.get_annot_nids(aid_list)
#valid_aid_names = ibs.get_annot_names(aid_list)
#valid_aid_gtrues = ibs.get_annot_groundtruth(aid_list)
return locals()
def devfunc(ibs, qaid_list):
""" Function for developing something """
print('[dev] devfunc')
allres = get_allres(ibs, qaid_list)
locals_ = locals()
#locals_.update(annotationmatch_scores(ibs, qaid_list))
return locals_
def run_dev(main_locals):
print('[dev] run_dev')
# Get references to controller
ibs = main_locals['ibs']
if ibs is not None:
# Get aids marked as test cases
qaid_list = main_helpers.get_test_qaids(ibs)
print('test_qaids = %r' % qaid_list)
print('len(test_qaids) = %d' % len(qaid_list))
# Warn on no test cases
try:
assert len(qaid_list) > 0, 'assert!'
except AssertionError as ex:
utool.printex(ex, 'len(qaid_list) = 0', iswarning=True)
#qaid_list = ibs.get_valid_aids()[0]
if len(qaid_list) > 0:
# Prepare the IBEIS controller for querys
ibs.prep_qreq_db(qaid_list)
# Run the dev experiments
expt_locals = run_experiments(ibs, qaid_list)
# Add experiment locals to local namespace
exec(utool.execstr_dict(expt_locals, 'expt_locals'))
if '--devmode' in sys.argv:
# Execute the dev-func and add to local namespace
devfunc_locals = devfunc(ibs, qaid_list)
exec(utool.execstr_dict(devfunc_locals, 'devfunc_locals'))
return locals()
if __name__ == '__main__':
"""
The Developer Script
A command line interface to almost everything
-w # wait / show the gui / figures are visible
--cmd # ipython shell to play with variables
-t # run list of tests
Examples:
./dev.py -t query -w
"""
multiprocessing.freeze_support() # for win32
#
#
# Run IBEIS Main, create controller, and possibly gui
print('++dev')
main_locals = ibeis.main(gui='--gui' in sys.argv)
#
#
# Load snippet variables
SNIPPITS = True
if SNIPPITS:
snippet_locals = dev_snippets(main_locals)
snippet_execstr = utool.execstr_dict(snippet_locals, 'snippet_locals')
exec(snippet_execstr)
#
#
# Development code
RUN_DEV = True # RUN_DEV = '__IPYTHON__' in vars()
if RUN_DEV:
dev_locals = run_dev(main_locals)
dev_execstr = utool.execstr_dict(dev_locals, 'dev_locals')
exec(dev_execstr)
#
#
# Main Loop (IPython interaction, or some exec loop)
if '--nopresent' not in sys.argv:
df2.present()
ipy = ('--gui' not in sys.argv) or ('--cmd' in sys.argv)
main_execstr = ibeis.main_loop(main_locals, ipy=ipy)
exec(main_execstr)
#
#
# Memory profile
if '--memprof' in sys.argv:
utool.print_resource_usage()
utool.memory_profile()
print('exiting dev')