def line_profile(fac, cmd='fac.query(1)'): # Meliae # from meliae import loader # om = loader.load('filename.json') # s = om.summarize(); import line_profiler iom = fac.hs.iom logmsg('Line Profiling Command: ' + cmd) line_profile_fpath = iom.get_temp_fpath('line_profile.' + cmd + '.profile') lp = line_profiler.LineProfiler() from inspect import getmembers, isfunction, ismethod for module in [ fac.hs, fac.hs.cm, fac.hs.gm, fac.hs.nm, fac.hs.qm, fac.hs.vm, fac.hs.am, fac.hs.dm ]: for (method_name, method) in getmembers(module, ismethod): lp.add_function(method.im_func) #functions_list = [o for o in getmembers(module, isfunction)] lp.runctx(cmd, globals(), locals()) lp.dump_stats(line_profile_fpath) lp.print_stats() rsr_fpath = 'C:\\Python27\\Scripts\\runsnake.exe' view_cmd = rsr_fpath + ' ' + line_profile_fpath os.system(view_cmd) return lp
def default_prefs(fac): uim = fac.hs.uim fac.hs.reload_preferences() logmsg( 'The change to defaults will not become permanent until you save or change one' ) if uim.hsgui != None: uim.hsgui.epw.pref_model.layoutChanged.emit()
def match_all_above_thresh(fac, threshold=None): 'do matching and assign all above thresh' if threshold == None: # User ask dlg = QInputDialog() threshres = dlg.getText( None, 'Threshold Selector', 'Enter a matching threshold.\n' + 'The system will query each chip and assign all matches above this thresh' ) if not threshres[1]: logmsg('Cancelled all match') return try: threshold = float(str(threshres[0])) except ValueError: logerr('The threshold must be a number') qm = fac.hs.qm cm = fac.hs.cm nm = fac.hs.nm vm = fac.hs.vm # Get model ready vm.sample_train_set() fac.hs.ensure_model() # Do all queries for qcx in iter(cm.get_valid_cxs()): qcid = cm.cx2_cid[qcx] logmsg('Querying CID=' + str(qcid)) query_name = cm.cx2_name(qcx) logdbg(str(qcx)) logdbg(str(type(qcx))) cm.load_features(qcx) res = fac.hs.query(qcid) # Match only those above a thresh res.num_top_min = 0 res.num_extra_return = 0 res.top_thresh = threshold top_cx = res.top_cx() if len(top_cx) == 0: print('No matched for cid=' + str(qcid)) continue top_names = cm.cx2_name(top_cx) all_names = np.append(top_names, [query_name]) if all([nm.UNIDEN_NAME() == name for name in all_names]): # If all names haven't been identified, make a new one new_name = nm.get_new_name() else: # Rename to the most frequent non ____ name seen from collections import Counter name_freq = Counter(np.append(top_names, [query_name])).most_common() new_name = name_freq[0][0] if new_name == nm.UNIDEN_NAME(): new_name = name_freq[1][0] # Do renaming cm.rename_chip(qcx, new_name) for cx in top_cx: cm.rename_chip(cx, new_name) fac.hs.uim.populate_tables()
def match_all_above_thresh(fac, threshold=None): 'do matching and assign all above thresh' if threshold == None: # User ask dlg = QInputDialog() threshres = dlg.getText(None, 'Threshold Selector', 'Enter a matching threshold.\n'+ 'The system will query each chip and assign all matches above this thresh') if not threshres[1]: logmsg('Cancelled all match') return try: threshold = float(str(threshres[0])) except ValueError: logerr('The threshold must be a number') qm = fac.hs.qm cm = fac.hs.cm nm = fac.hs.nm vm = fac.hs.vm # Get model ready vm.sample_train_set() fac.hs.ensure_model() # Do all queries for qcx in iter(cm.get_valid_cxs()): qcid = cm.cx2_cid[qcx] logmsg('Querying CID='+str(qcid)) query_name = cm.cx2_name(qcx) logdbg(str(qcx)) logdbg(str(type(qcx))) cm.load_features(qcx) res = fac.hs.query(qcid) # Match only those above a thresh res.num_top_min = 0 res.num_extra_return = 0 res.top_thresh = threshold top_cx = res.top_cx() if len(top_cx) == 0: print('No matched for cid='+str(qcid)) continue top_names = cm.cx2_name(top_cx) all_names = np.append(top_names,[query_name]) if all([nm.UNIDEN_NAME() == name for name in all_names]): # If all names haven't been identified, make a new one new_name = nm.get_new_name() else: # Rename to the most frequent non ____ name seen from collections import Counter name_freq = Counter(np.append(top_names,[query_name])).most_common() new_name = name_freq[0][0] if new_name == nm.UNIDEN_NAME(): new_name = name_freq[1][0] # Do renaming cm.rename_chip(qcx, new_name) for cx in top_cx: cm.rename_chip(cx, new_name) fac.hs.uim.populate_tables()
def call_graph(fac, cmd='fac.query(1)'): import pycallgraph import Image iom = fac.hs.iom logmsg('Call Graph Command: ' + cmd) callgraph_fpath = iom.get_temp_fpath('callgraph' + cmd + '.png') pycallgraph.start_trace() eval(cmd) pycallgraph.stop_trace() pycallgraph.make_dot_graph(callgraph_fpath) Image.open(callgraph_fpath).show()
def call_graph(fac, cmd='fac.query(1)'): import pycallgraph import Image iom = fac.hs.iom logmsg('Call Graph Command: '+cmd) callgraph_fpath = iom.get_temp_fpath('callgraph'+cmd+'.png') pycallgraph.start_trace() eval(cmd) pycallgraph.stop_trace() pycallgraph.make_dot_graph(callgraph_fpath) Image.open(callgraph_fpath).show()
def add_new_prop(fac, propname=None): 'add a new property to keep track of' if propname is None: # User ask dlg = QInputDialog() textres = dlg.getText(None, 'New Metadata Property','What is the new property name? ') if not textres[1]: logmsg('Cancelled new property') return propname = str(textres[0]) logmsg('Adding property '+propname) fac.hs.cm.add_user_prop(propname) fac.hs.uim.populate_tables()
def add_new_prop(fac, propname=None): 'add a new property to keep track of' if propname is None: # User ask dlg = QInputDialog() textres = dlg.getText(None, 'New Metadata Property', 'What is the new property name? ') if not textres[1]: logmsg('Cancelled new property') return propname = str(textres[0]) logmsg('Adding property ' + propname) fac.hs.cm.add_user_prop(propname) fac.hs.uim.populate_tables()
def query(fac, qcid=None): 'Performs a query' uim, cm, qm, vm, nm = fac.hs.get_managers('uim', 'cm', 'qm', 'vm', 'nm') try: if qcid is None: qcid = uim.sel_cid else: uim.select_cid(qcid) qcx = cm.cx(qcid) uim.update_state('Querying') print('Querying Chip: ' + cm.cx2_info(qcx, clbls)) logdbg('\n\nQuerying Chip: ' + cm.cx2_info(qcx, clbls)) uim.sel_res = fac.hs.query(qcid) logmsg('\n\nFinished Query') uim.update_state('done_querying') logmsg(str(uim.sel_res)) logdbg('\n\n*** Populating Results Tables ***') uim.populate_result_table() logdbg('\n\n*** Switching To Result Views ***') uim.update_state('result_view') logdbg('\n\n*** Redrawing UI ***') uim.draw() logdbg('\n\n*** Done Redrawing UI ***') # QUICK AND DIRTY CODE. PLEASE FIXME try: cx1 = uim.sel_res.rr.qcx cx2 = uim.sel_res.top_cx()[0] if uim.ui_prefs.prompt_after_result and cm.cx2_nx[ cx1] == nm.UNIDEN_NX( ) and cm.cx2_nx[cx2] != nm.UNIDEN_NX(): logdbg('Quick and dirty prompting') fac._quick_and_dirty_result_prompt(uim.sel_res.rr.qcx, uim.sel_res.top_cx()[0]) else: logdbg('No Quick and dirty prompting') except Exception as ex: logdbg('bad quick and dirty facade code: ' + str(ex)) pass logdbg('\n\n-----------Query OVER-------------\n\n') except Exception as ex: uim.update_state('done_querying') uim.update_state('query_failed') raise
def query(fac, qcid=None): 'Performs a query' uim, cm, qm, vm, nm = fac.hs.get_managers('uim', 'cm','qm','vm', 'nm') try: if qcid is None: qcid = uim.sel_cid else: uim.select_cid(qcid) qcx = cm.cx(qcid) uim.update_state('Querying') print('Querying Chip: '+cm.cx2_info(qcx, clbls)) logdbg('\n\nQuerying Chip: '+cm.cx2_info(qcx, clbls)) uim.sel_res = fac.hs.query(qcid) logmsg('\n\nFinished Query') uim.update_state('done_querying') logmsg(str(uim.sel_res)) logdbg('\n\n*** Populating Results Tables ***') uim.populate_result_table() logdbg('\n\n*** Switching To Result Views ***') uim.update_state('result_view') logdbg('\n\n*** Redrawing UI ***') uim.draw() logdbg('\n\n*** Done Redrawing UI ***') # QUICK AND DIRTY CODE. PLEASE FIXME try: cx1 = uim.sel_res.rr.qcx cx2 = uim.sel_res.top_cx()[0] if uim.ui_prefs.prompt_after_result and cm.cx2_nx[cx1] == nm.UNIDEN_NX() and cm.cx2_nx[cx2] != nm.UNIDEN_NX(): logdbg('Quick and dirty prompting') fac._quick_and_dirty_result_prompt(uim.sel_res.rr.qcx, uim.sel_res.top_cx()[0]) else: logdbg('No Quick and dirty prompting') except Exception as ex: logdbg('bad quick and dirty facade code: '+str(ex)) pass logdbg('\n\n-----------Query OVER-------------\n\n') except Exception as ex: uim.update_state('done_querying') uim.update_state('query_failed') raise
def line_profile(fac, cmd='fac.query(1)'): # Meliae # from meliae import loader # om = loader.load('filename.json') # s = om.summarize(); import line_profiler iom = fac.hs.iom logmsg('Line Profiling Command: '+cmd) line_profile_fpath = iom.get_temp_fpath('line_profile.'+cmd+'.profile') lp = line_profiler.LineProfiler() from inspect import getmembers, isfunction, ismethod for module in [fac.hs, fac.hs.cm, fac.hs.gm, fac.hs.nm, fac.hs.qm, fac.hs.vm, fac.hs.am, fac.hs.dm]: for (method_name, method) in getmembers(module, ismethod): lp.add_function(method.im_func) #functions_list = [o for o in getmembers(module, isfunction)] lp.runctx( cmd, globals(), locals()) lp.dump_stats(line_profile_fpath) lp.print_stats() rsr_fpath = 'C:\\Python27\\Scripts\\runsnake.exe' view_cmd = rsr_fpath+' '+line_profile_fpath os.system(view_cmd) return lp
def expand_rois(fac, percent_increase=None): 'expand rois by a percentage of the diagonal' if percent_increase == None: # User ask dlg = QInputDialog() percentres = dlg.getText( None, 'ROI Expansion Factor', 'Enter the percentage to expand the ROIs.\n' + 'The percentage is in terms of diagonal length') if not percentres[1]: logmsg('Cancelled all match') return try: percent_increase = float(str(percentres[0])) except ValueError: logerr('The percentage must be a number') cm = fac.hs.cm gm = fac.hs.gm logmsg('Resizing all chips') for cx in iter(cm.get_valid_cxs()): logmsg('Resizing cx=' + str(cx)) # Get ROI size and Image size [rx, ry, rw, rh] = cm.cx2_roi[cx] [gw, gh] = gm.gx2_img_size(cm.cx2_gx[cx]) # Find Diagonal Increase diag = np.sqrt(rw**2 + rh**2) scale_factor = percent_increase / 100.0 diag_increase = scale_factor * diag target_diag = diag + diag_increase # Find Width/Height Increase ar = float(rw) / float(rh) w_increase = np.sqrt(ar**2 * diag_increase**2 / (ar**2 + 1)) h_increase = w_increase / ar # Find New xywh within image constriants new_x = int(max(0, round(rx - w_increase / 2.0))) new_y = int(max(0, round(ry - h_increase / 2.0))) new_w = int(min(gw - new_x, round(rw + w_increase))) new_h = int(min(gh - new_y, round(rh + h_increase))) new_roi = [new_x, new_y, new_w, new_h] logmsg('Old Roi: ' + repr([rx, ry, rw, rh])) cm.change_roi(cx, new_roi) logmsg('\n') logmsg('Done resizing all chips')
def add_img(gm, gid=None, gname=None, aif=False, src_img=''): logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' % (gid, gname, aif, src_img)) if src_img != '': #This is an new image (dir_part, nameext_part) = os.path.split(src_img) (name_part, ext_part) = os.path.splitext(nameext_part) if not ext_part.lower() in gm.valid_img_extensions: logerr('Invalid Image: %s' % src_img) if gname is None: gname = name_part + ext_part db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if os.path.abspath(src_img) == os.path.abspath(db_img): logmsg('Readding existing dbimg:'+src_img) else: logmsg('Copying '+src_img+' to '+db_img) copyfile(src_img, db_img) db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if not os.path.exists(db_img): # Try to add an extension if it wasn't given ext_fallback_list = ['.jpg','.jpeg','.JPG','.JPEG','.png','.tif'] fb_sucess_bit = False (db_img_noext, old_ext) = os.path.splitext(db_img) (gname_noext, old_ext2) = os.path.splitext(gname) for ext_fb in ext_fallback_list: db_img_fb = db_img_noext + ext_fb if os.path.exists(db_img_fb): db_img = db_img_fb gname = gname_noext+ext_fb fb_sucess_bit = True; break if not fb_sucess_bit: logwarn('Trying to add a nonexistant image: '+db_img) return if gname in gm.gname2_gid.keys(): logdbg('Trying to add a GNAME that is already managed: '+gname) return if gid is None or gid < 1: gid = gm.next_gid else: if (gid < len(gm.gid2_gx)-1 and gm.gid2_gx[gid] > 0): logdbg('Trying to add a GID that is already managed: '+str(gid)) return gid #Check key values before memory managment #Manage Memory gx = gm.next_gx if gx >= len(gm.gx2_gid): gm.img_alloc((len(gm.gx2_gid)+1)*2+1) #Flat indexing gm.gx2_gid[gx] = gid gm.gx2_gname[gx] = gname if len(gname) > gm.max_gnamelen: gm.max_gnamelen = len(gname) gm.max_gname = gname gm.gx2_aif_bit[gx] = aif #X Reverse indexing if len(gm.gid2_gx) <= gid: gid_extend = (gid - len(gm.gid2_gx)) + 1 gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend,dtype=uint32)) gm.gid2_gx[gid] = gx gm.gname2_gid[gname] = gid #Increment gm.next_gx = gm.next_gx + 1 gm.next_gid = max(gm.next_gid+1, gid+1) gm.num_g = gm.num_g + 1 gm.max_gx = max(gm.max_gx, gx) gm.max_gid = max(gm.max_gid, gx) return gid
def add_img(gm, gid=None, gname=None, aif=False, src_img=''): logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' % (gid, gname, aif, src_img)) if src_img != '': #This is an new image (dir_part, nameext_part) = os.path.split(src_img) (name_part, ext_part) = os.path.splitext(nameext_part) if not ext_part.lower() in gm.valid_img_extensions: logerr('Invalid Image: %s' % src_img) if gname is None: gname = name_part + ext_part db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if os.path.abspath(src_img) == os.path.abspath(db_img): logmsg('Readding existing dbimg:' + src_img) else: logmsg('Copying ' + src_img + ' to ' + db_img) copyfile(src_img, db_img) db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname) if not os.path.exists(db_img): # Try to add an extension if it wasn't given ext_fallback_list = [ '.jpg', '.jpeg', '.JPG', '.JPEG', '.png', '.tif' ] fb_sucess_bit = False (db_img_noext, old_ext) = os.path.splitext(db_img) (gname_noext, old_ext2) = os.path.splitext(gname) for ext_fb in ext_fallback_list: db_img_fb = db_img_noext + ext_fb if os.path.exists(db_img_fb): db_img = db_img_fb gname = gname_noext + ext_fb fb_sucess_bit = True break if not fb_sucess_bit: logwarn('Trying to add a nonexistant image: ' + db_img) return if gname in gm.gname2_gid.keys(): logdbg('Trying to add a GNAME that is already managed: ' + gname) return if gid is None or gid < 1: gid = gm.next_gid else: if (gid < len(gm.gid2_gx) - 1 and gm.gid2_gx[gid] > 0): logdbg('Trying to add a GID that is already managed: ' + str(gid)) return gid #Check key values before memory managment #Manage Memory gx = gm.next_gx if gx >= len(gm.gx2_gid): gm.img_alloc((len(gm.gx2_gid) + 1) * 2 + 1) #Flat indexing gm.gx2_gid[gx] = gid gm.gx2_gname[gx] = gname if len(gname) > gm.max_gnamelen: gm.max_gnamelen = len(gname) gm.max_gname = gname gm.gx2_aif_bit[gx] = aif #X Reverse indexing if len(gm.gid2_gx) <= gid: gid_extend = (gid - len(gm.gid2_gx)) + 1 gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend, dtype=uint32)) gm.gid2_gx[gid] = gx gm.gname2_gid[gname] = gid #Increment gm.next_gx = gm.next_gx + 1 gm.next_gid = max(gm.next_gid + 1, gid + 1) gm.num_g = gm.num_g + 1 gm.max_gx = max(gm.max_gx, gx) gm.max_gid = max(gm.max_gid, gx) return gid
def expand_rois(fac, percent_increase=None): 'expand rois by a percentage of the diagonal' if percent_increase == None: # User ask dlg = QInputDialog() percentres = dlg.getText(None, 'ROI Expansion Factor', 'Enter the percentage to expand the ROIs.\n'+ 'The percentage is in terms of diagonal length') if not percentres[1]: logmsg('Cancelled all match') return try: percent_increase = float(str(percentres[0])) except ValueError: logerr('The percentage must be a number') cm = fac.hs.cm gm = fac.hs.gm logmsg('Resizing all chips') for cx in iter(cm.get_valid_cxs()): logmsg('Resizing cx='+str(cx)) # Get ROI size and Image size [rx, ry, rw, rh] = cm.cx2_roi[cx] [gw, gh] = gm.gx2_img_size(cm.cx2_gx[cx]) # Find Diagonal Increase diag = np.sqrt(rw**2 + rh**2) scale_factor = percent_increase/100.0 diag_increase = scale_factor * diag target_diag = diag + diag_increase # Find Width/Height Increase ar = float(rw)/float(rh) w_increase = np.sqrt(ar**2 * diag_increase**2 / (ar**2 + 1)) h_increase = w_increase / ar # Find New xywh within image constriants new_x = int(max(0, round(rx - w_increase / 2.0))) new_y = int(max(0, round(ry - h_increase / 2.0))) new_w = int(min(gw - new_x, round(rw + w_increase))) new_h = int(min(gh - new_y, round(rh + h_increase))) new_roi = [new_x, new_y, new_w, new_h] logmsg('Old Roi: '+repr([rx, ry, rw, rh])) cm.change_roi(cx, new_roi) logmsg('\n') logmsg('Done resizing all chips')
def default_prefs(fac): uim = fac.hs.uim fac.hs.reload_preferences() logmsg('The change to defaults will not become permanent until you save or change one') if uim.hsgui != None: uim.hsgui.epw.pref_model.layoutChanged.emit()