Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
 def  add_name(nm, nid_, name_):
     'Adds a name. If nid == -1 a new nid will be assigned. Returns nid'
     logdbg('Adding nid='+str(nid_)+' name='+name_)
     nid = nid_
     name = name_.strip()
     if name == '':
         logerr('Cannot add an empty name!')
     if nid < 0:
         # Generate new nid if not specified
         nid = nm.next_nid
     else:
         #If NID already exists and has an entry,
         #do not increment, and do nothing
         #This is essentially like doing a rename
         #it doesn't actually change anything
         if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0):
             logwarn('NID '+str(nid)+'already exists')
             nx = nm.nid2_nx[nid]
             return -1
     if name in nm.name2_nx.keys():
         conflict_nx = nm.name2_nx[name]
         conflict_nid = nm.nx2_nid[conflict_nx]
         conflict_msg = 'Name %s already exists in database!\n' % name + \
                        'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \
                        'CONFLICT_NID=%d\n' % conflict_nid 
         if nid_ == -1:
             logwarn(conflict_msg)
         elif nid_ > -1:
             logerr(conflict_msg)
         nid = conflict_nid
         nx  = conflict_nx
         return nid
     #Manage Memory
     nx = nm.next_nx
     logdbg(' * nx = '+str(nx))
     if nx >= len(nm.nx2_nid):
         nm.name_alloc((len(nm.nx2_nid)+1)*2+1)
     #Add to flat table
     nm.nx2_nid[nx]   = nid
     nm.nx2_name[nx]  = name
     if len(name) > nm.max_namelen:
         nm.max_namelen = len(name)
         nm.max_name = name
     #X Reverse indexing
     if len(nm.nid2_nx) <= nid:
         idAlloc = (nid - len(nm.nid2_nx)) + 1
         nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc,dtype=uint32)) 
     logdbg( ' * nid2_nx['+str(nid)+']='+str(nx) )
     nm.nid2_nx[nid]   = nx
     nm.name2_nx[name] = nx
     #Increment
     nm.next_nx  = nm.next_nx + 1
     nm.next_nid = max(nm.next_nid + 1, nid + 1)
     nm.max_nx   = max(nm.max_nx , nx)
     nm.max_nid  = max(nm.max_nid, nid)
     nm.num_n    = nm.num_n + 1
     logdbg('Added nid='+str(nid)+' name='+name)
     return nid
Ejemplo n.º 3
0
 def add_name(nm, nid_, name_):
     'Adds a name. If nid == -1 a new nid will be assigned. Returns nid'
     logdbg('Adding nid=' + str(nid_) + ' name=' + name_)
     nid = nid_
     name = name_.strip()
     if name == '':
         logerr('Cannot add an empty name!')
     if nid < 0:
         # Generate new nid if not specified
         nid = nm.next_nid
     else:
         #If NID already exists and has an entry,
         #do not increment, and do nothing
         #This is essentially like doing a rename
         #it doesn't actually change anything
         if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0):
             logwarn('NID ' + str(nid) + 'already exists')
             nx = nm.nid2_nx[nid]
             return -1
     if name in nm.name2_nx.keys():
         conflict_nx = nm.name2_nx[name]
         conflict_nid = nm.nx2_nid[conflict_nx]
         conflict_msg = 'Name %s already exists in database!\n' % name + \
                        'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \
                        'CONFLICT_NID=%d\n' % conflict_nid
         if nid_ == -1:
             logwarn(conflict_msg)
         elif nid_ > -1:
             logerr(conflict_msg)
         nid = conflict_nid
         nx = conflict_nx
         return nid
     #Manage Memory
     nx = nm.next_nx
     logdbg(' * nx = ' + str(nx))
     if nx >= len(nm.nx2_nid):
         nm.name_alloc((len(nm.nx2_nid) + 1) * 2 + 1)
     #Add to flat table
     nm.nx2_nid[nx] = nid
     nm.nx2_name[nx] = name
     if len(name) > nm.max_namelen:
         nm.max_namelen = len(name)
         nm.max_name = name
     #X Reverse indexing
     if len(nm.nid2_nx) <= nid:
         idAlloc = (nid - len(nm.nid2_nx)) + 1
         nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc, dtype=uint32))
     logdbg(' * nid2_nx[' + str(nid) + ']=' + str(nx))
     nm.nid2_nx[nid] = nx
     nm.name2_nx[name] = nx
     #Increment
     nm.next_nx = nm.next_nx + 1
     nm.next_nid = max(nm.next_nid + 1, nid + 1)
     nm.max_nx = max(nm.max_nx, nx)
     nm.max_nid = max(nm.max_nid, nid)
     nm.num_n = nm.num_n + 1
     logdbg('Added nid=' + str(nid) + ' name=' + name)
     return nid
Ejemplo n.º 4
0
 def gx2_img_fpath(gm, gx): # returns full path
     'Gets the img fpath with checks'
     iom = gm.hs.iom
     gname = gm.gx2_gname[gx]
     if gname is None: logerr('There is no image for GX='+str(gx))
     img_fpath = iom.get_img_fpath(gname)
     if not os.path.exists(img_fpath):
         logerr('The data is gone!\nGX=%d, img_fpath=%s' % (gx, img_fpath))
     return img_fpath
Ejemplo n.º 5
0
 def gx2_img_fpath(gm, gx):  # returns full path
     'Gets the img fpath with checks'
     iom = gm.hs.iom
     gname = gm.gx2_gname[gx]
     if gname is None: logerr('There is no image for GX=' + str(gx))
     img_fpath = iom.get_img_fpath(gname)
     if not os.path.exists(img_fpath):
         logerr('The data is gone!\nGX=%d, img_fpath=%s' % (gx, img_fpath))
     return img_fpath
Ejemplo n.º 6
0
 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()
Ejemplo n.º 7
0
    def select_next(fac):
        uim = fac.hs.uim
        if uim.state == 'chip_view':
            fac.next_unident_chip()
        elif uim.state == 'image_view':
            if not fac.next_empty_image():
                if not fac.next_equal_size_chip():
                    fac.next_0_theta_chip()

        else:
            logerr('Cannot goto next in state: '+uim.state)
Ejemplo n.º 8
0
 def qad_batch_rename():
     print 'qad batch renaming'
     try:
         name1 = str(self.dlg_skel.oldNameEdit.text())
         name2 = str(self.dlg_skel.newNameEdit.text())
         fac.hs.batch_rename(name1, name2)
     except Exception as ex:
         logerr(str(ex))
     fac.hs.uim.populate_tables()
     fac.hs.uim.draw()
     self.close()
Ejemplo n.º 9
0
    def select_next(fac):
        uim = fac.hs.uim
        if uim.state == 'chip_view':
            fac.next_unident_chip()
        elif uim.state == 'image_view':
            if not fac.next_empty_image():
                if not fac.next_equal_size_chip():
                    fac.next_0_theta_chip()

        else:
            logerr('Cannot goto next in state: ' + uim.state)
Ejemplo n.º 10
0
 def qad_batch_rename():
     print 'qad batch renaming'
     try:
         name1 = str(self.dlg_skel.oldNameEdit.text())
         name2 = str(self.dlg_skel.newNameEdit.text())
         fac.hs.batch_rename(name1, name2)
     except Exception as ex:
         logerr(str(ex))
     fac.hs.uim.populate_tables()
     fac.hs.uim.draw()
     self.close()
Ejemplo n.º 11
0
 def change_view(fac, new_state):
     uim = fac.hs.uim
     prevBlock = uim.hsgui.main_skel.tablesTabWidget.blockSignals(True)
     # THIS LIST IS IN THE ORDER OF THE TABS. 
     # THIS SHOULD CHANGE TO BE INDEPENDENT OF THAT FIXME
     if not new_state in uim.tab_order:
         if new_state in xrange(len(uim.tab_order)):
             new_state = uim.tab_order[new_state]+'_view'
         else:
             logerr('State is: '+str(new_state)+', but it must be one of: '+str(uim.tab_order))
     uim.update_state(new_state)
     uim.draw()
     uim.hsgui.main_skel.tablesTabWidget.blockSignals(prevBlock)
Ejemplo n.º 12
0
 def change_view(fac, new_state):
     uim = fac.hs.uim
     prevBlock = uim.hsgui.main_skel.tablesTabWidget.blockSignals(True)
     # THIS LIST IS IN THE ORDER OF THE TABS.
     # THIS SHOULD CHANGE TO BE INDEPENDENT OF THAT FIXME
     if not new_state in uim.tab_order:
         if new_state in xrange(len(uim.tab_order)):
             new_state = uim.tab_order[new_state] + '_view'
         else:
             logerr('State is: ' + str(new_state) +
                    ', but it must be one of: ' + str(uim.tab_order))
     uim.update_state(new_state)
     uim.draw()
     uim.hsgui.main_skel.tablesTabWidget.blockSignals(prevBlock)
Ejemplo n.º 13
0
 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')
Ejemplo n.º 14
0
 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')
Ejemplo n.º 15
0
 def show_edit_preference_widget(fac):
     uim = fac.hs.uim
     if not uim.hsgui is None:
         uim.hsgui.epw.show()
     else:
         logerr('GUI does not exist')
Ejemplo n.º 16
0
 def show_main_window(fac):
     uim = fac.hs.uim
     if uim.hsgui != None:
         uim.hsgui.show()
     else:
         logerr('GUI does not exist')
Ejemplo n.º 17
0
 def show_main_window(fac):
     uim = fac.hs.uim
     if uim.hsgui != None:
         uim.hsgui.show()
     else: 
         logerr('GUI does not exist')
Ejemplo n.º 18
0
 def show_edit_preference_widget(fac):
     uim = fac.hs.uim
     if not uim.hsgui is None:
         uim.hsgui.epw.show()
     else: 
         logerr('GUI does not exist')
Ejemplo n.º 19
0
    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
Ejemplo n.º 20
0
 def gx(gm, gid):
     if not gm.is_valid(gid):
         logerr('GID=%s is invalid' % str(gid))
     return gm.gid2_gx[gid]
Ejemplo n.º 21
0
 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
Ejemplo n.º 22
0
 def gx(gm, gid):
     if not gm.is_valid(gid):
         logerr('GID=%s is invalid' % str(gid))
     return gm.gid2_gx[gid]