def loadClassifier(ctb, project): tidF = WndcharmStorage.getAttachedTableFile(ctb.tcF, project) tidW = WndcharmStorage.getAttachedTableFile(ctb.tcW, project) tidL = WndcharmStorage.getAttachedTableFile(ctb.tcL, project) if tidF is None or tidW is None or tidL is None: raise Exception("Incomplete set of classifier tables: %s" % (tidF, tidW, tidL)) ctb.openTables(tidF, tidW, tidL) version = unwrap(ctb.versiontag.getTextValue()) cls = ctb.loadClassifierTables() # ids,trainClassIds,featureMatrix,featureNames,weights,classIds,classNames trainFts = wndcharm.FeatureSet.FeatureSet_Discrete() # if cls['classIds'] != sorted(cls['classIds']): if cls["classIds"] != range(len(cls["classIds"])): raise Exception("Incorrectly ordered class IDs") trainFts.classnames_list = cls["classNames"] # Objects should be in order of increasing class ID, this makes rebuilding # data_list much faster nclasses = len(cls["classNames"]) classCounts = [0] * nclasses cprev = -1 for c in cls["trainClassIds"]: if c < cprev: raise Exception("Incorrectly ordered training class feature data") cprev = c classCounts[c] += 1 trainFts.classsizes_list = classCounts trainFts.feature_vector_version = version classFts = [[] for n in xrange(len(cls["classNames"]))] p = 0 for i in xrange(nclasses): classFts[i] = numpy.array(cls["featureMatrix"][p : (p + classCounts[i])]) p += classCounts[i] trainFts.data_list = classFts trainFts.num_images = sum(classCounts) trainFts.num_features = len(cls["featureNames"]) trainFts.num_classes = nclasses trainFts.featurenames_list = cls["featureNames"] trainFts.imagenames_list = [str(i) for i in cls["ids"]] tmp = trainFts.ContiguousDataMatrix() weights = wndcharm.FeatureSet.FisherFeatureWeights( data_dict={"names": cls["featureNames"], "values": cls["weights"]} ) return (trainFts, weights)
def addToFeatureSet(ftb, ds, fts, classId): message = "" tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds) if tid: if not ftb.openTable(tid): return message + "\nERROR: Table not opened" version = unwrap(ftb.versiontag.getTextValue()) message += "Opened table id:%d version:%s\n" % (tid, version) else: message += "ERROR: Table not found for Dataset id:%d" % ds.getId() return message # fts = wndcharm.FeatureSet.FeatureSet_Discrete({'num_images': 0}) for image in ds.listChildren(): imId = image.getId() message += "\tProcessing features for image id:%d\n" % imId # message += extractFeatures(tc, d, im = image) + '\n' sig = wndcharm.FeatureSet.Signatures() (sig.names, sig.values) = ftb.loadFeatures(imId) # sig.source_file = image.getName() sig.source_file = str(imId) sig.version = version fts.AddSignature(sig, classId) fts.classnames_list[classId] = ds.getName() return message
def addToFeatureSet(ftb, ds, fts, classId, imagesOnly): message = '' tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds) if tid: if not ftb.openTable(tid): return message + '\nERROR: Table not opened' version = unwrap(ftb.versiontag.getTextValue()) message += 'Opened table id:%d version:%s\n' % (tid, version) else: message += 'ERROR: Table not found for Dataset id:%d' % ds.getId() return message #fts = wndcharm.FeatureSet.FeatureSet_Discrete({'num_images': 0}) if imagesOnly: for image in ds.listChildren(): imId = image.getId() message += '\tProcessing features for image id:%d\n' % imId sig = wndcharm.FeatureSet.Signatures() (sig.names, sig.values) = ftb.loadFeatures(imId) sig.source_file = str(imId) sig.version = version fts.AddSignature(sig, classId) else: names, values, ids = ftb.bulkLoadFeatures() message += '\tProcessing all features for dataset id:%d\n' % ds.getId() for imId, vals in izip(ids, values): sig = wndcharm.FeatureSet.Signatures() sig.names = names sig.values = vals sig.source_file = str(imId) sig.version = version fts.AddSignature(sig, classId) fts.classnames_list[classId] = ds.getName() return message
def countCompleted(ftb, ds): message = '' tc = ftb.tc imIds = [im.getId() for im in ds.listChildren()] tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds) if tid is None: message += 'Image feature status PRESENT:%d ABSENT:%d\n' % \ (0, len(imIds)) return message if not ftb.openTable(tid): message += 'ERROR: Table not opened\n' message += 'Image feature status UNKNOWN:%d\n' % len(imIds) return message message += 'Opened table id:%d\n' % tid d = tc.chunkedRead([0], 0, tc.table.getNumberOfRows(), 100) ftImIds = d.columns[0].values matchedIds = set(imIds).intersection(ftImIds) message += 'Image feature status PRESENT:%d ABSENT:%d\n' % \ (len(matchedIds), len(imIds) - len(matchedIds)) return message
def extractFeatures(ftb, ds, newOnly, chNames, imageId = None, im = None): message = '' tc = ftb.tc # dataset must be explicitly provided because an image can be linked to # multiple datasets in which case im.getDataset() doesn't work if not im: if not imageId: #raise Exception('No input image') raise omero.ServerError('No input image') im = ftb.conn.getObject('Image', imageId) if not im: return 'Image id:%d not found\n' % imageId else: imageId = im.getId() tid = WndcharmStorage.getAttachedTableFile(ftb.tc, ds) if tid: if not ftb.openTable(tid): return message + '\nERROR: Table not opened\n' version = unwrap(ftb.versiontag.getTextValue()) # version seems to be in unicode message += 'Opened table id:%d version:%s\n' % (tid, str(version)) if newOnly and ftb.tableContainsId(imageId): return message + 'Image id:%d features already in table' % imageId # FIXME: default is convert multichannel to greyscale unless user input # Calculate features for an image channel # Optionally prepend the channel label to each feature name and combine ftall = None for c in xrange( len( chNames ) ): wndcharm_matrix = PyImageMatrix() wndcharm_matrix.allocate( im.getSizeX(), im.getSizeY() ) numpy_matrix = wndcharm_matrix.as_ndarray() numpy_matrix[:] = im.getPrimaryPixels().getPlane(theZ=0,theC=c,theT=0) feature_plan = wndcharm.StdFeatureComputationPlans.getFeatureSet(); options = "" # This is where you can tell wnd-charm to normalize pixel intensities, # take ROIs etc. ... leave blank for now. ft = Signatures.NewFromFeatureComputationPlan( wndcharm_matrix, feature_plan, options ) ft.names = [WndcharmStorage.insert_channel_name( n, chNames[c]) for n in ft.names] ft.source_path = im.getName() if not ftall: ftall = ft else: ftall.names += ft.names ftall.values += ft.values # Save the features to a table if not tid: ftb.createTable(ftall.names, ft.version) version = unwrap(ftb.versiontag.getTextValue()) message += 'Created new table id:%d version:%s\n' % ( ftb.tc.tableId, version) message += WndcharmStorage.addFileAnnotationTo(tc, ds) if version != ft.version: return message + 'Incompatible version: Stored=%s Calculated=%s' % ( version, ft.version) ftb.saveFeatures(imageId, ftall) return message + 'Extracted features from Image id:%d\n' % imageId