Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
for (f,to) in le:
#print (" Add? " + str(to))
new = sub.union([to])
lnew = list(new)
lnew.sort()
snew = ",".join(lnew)
#print (" Test:" + snew + " in " + str(newSubsS))
if(not snew in newSubsS):
newSubsS.add(snew)
newSubsL.append(new)
if d in nodeNumbers:
# struc = getEdgesBetweenThem(new, self.elabels.keys())
# sg1 = smallGraph.SmallGraph()
# for n in new:
# sg1.nodes[n] = "".join(self.nlabels[n].keys())
# for (a,b) in struc:
# sg1.edges[(a,b)] = "".join(self.elabels[(a,b)].keys())
yield self.getSubSmallGraph(new)
#print (" Added: " + str(new))
subStruct = newSubsL
def getSubSmallGraph(self, nodelist):
"""return the small graph with the primitives in nodelist and all edges
between them. The used label is the merged list of labels from nodes/edges"""
sg = smallGraph.SmallGraph()
for n in nodelist:
sg.nodes[n] = "".join(self.nlabels[n].keys())
for e in getEdgesBetweenThem(nodelist,self.elabels.keys()):
sg.edges[e] = "".join(self.elabels[e].keys())
return sg
#compare the substructure
def compareSubStruct(self, olg, depths):
"""return the list of couple of substructure which disagree
the substructure from self are used as references"""
for struc in olg.subStructIterator(depths):
sg1 = self.getSubSmallGraph(struc.nodes.keys())
if(not (struc == sg1)):
allerrors.append((struc,sg1))
return allerrors
def compareSegmentsStruct(self, lgGT,depths):
"""Compute the number of differing segments, and record disagreements
in a list.
The primitives in each subgraph should be of the same number and names
(identifiers). Nodes are merged that have identical (label,value)
pairs on nodes and all identical incoming and outgoing edges.
If used for classification evaluation, the ground-truth should be lgGT.
The first key value of the matrix is the lgGT obj structure, which
gives the structure of the corresponding primitives which is the key
to get the error structure in self"""
(sp1, ps1, _, sre1) = self.segmentGraph()
(spGT, psGT, _, sreGT) = lgGT.segmentGraph()
#byValue = lambda pair: pair[1] # define key for sort comparisons.
#FIX : this this not the case in spare representation
segDiffs = set()
correctSegments = set()
# Make sure to skip primitives that were missing ('ABSENT'),
# as in that case the graphs disagree on all non-identical node
# pairs for this primitive, and captured in self.absentEdges.
if not 'ABSENT' in self.nlabels[primitive] and \
not 'ABSENT' in lgGT.nlabels[primitive]:
# Obtain sets of primitives sharing a segment for the current
# primitive for both graphs.
# Each of sp1/spGT are a map of ( {prim_set}, label ) pairs.
segPrimSet1 = sp1[ ps1[primitive] ][0]
segPrimSet2 = spGT[ psGT[primitive] ][0]
# Only create an entry where there are disagreements.
if segPrimSet1 != segPrimSet2:
segDiffs.add( ( psGT[primitive], ps1[primitive]) )
# DEBUG: don't record differences for a single node.
elif len(self.nlabels.keys()) > 1:
# If node was missing in this graph or the other, treat
# this graph as having a miss segment
# do not count the segment in graph with 1 primitive
segDiffs.add(( psGT[primitive], ps1[primitive]) )
# now check if the labels are identical
for seg in correctSegments:
# Get label for the first primtives (all primitives have identical
# labels in a segment).
# DEBUG: use only the set of labels, not confidence values.
if (0,[]) != self.cmpNodes(self.nlabels[ firstPrim ].keys(),lgGT.nlabels[ firstPrim ].keys()):
segDiffs.add(( psGT[primitive], ps1[primitive]) )
allSegWithErr = set([p for (p,_) in segDiffs])
# start to build the LG at the object level
# add nodes for objet with the labels from the first prim
lgObj = Lg()
for (sid,lprim) in spGT.iteritems():
lgObj.nlabels[sid] = lgGT.nlabels[list(lprim[0])[0]]
# Compute the specific 'segment-level' graph edges that disagree, at the
# level of primitive-pairs. This means that invalid segmentations may
# still have valid layouts in some cases.
# Add also the edges in the smallGraph
segEdgeErr = set()
for thisPair in sreGT.keys():
# TODO : check if it is sp1[thisPair[0]] instead of sp1[thisPair[0]][0]
thisParentIds = set(spGT[ thisPair[0] ][0])
thisChildIds = set(spGT[thisPair[1] ][0])
lgObj.elabels[thisPair] = lgGT.elabels[ (list(thisParentIds)[0], list(thisChildIds)[0])]
# A 'correct' edge has the same label between all primitives
# in the two segments.
# NOTE: we are not checking the consitency of label in each graph
# ie if all labels from thisParentIds to thisChildIds in self are
# the same
for parentId in thisParentIds:
for childId in thisChildIds:
# DEBUG: compare only label sets, not values.
if not (parentId, childId) in self.elabels.keys() or \
(0,[]) != self.cmpEdges(self.elabels[ (parentId, childId) ].keys(),lgGT.elabels[ (parentId, childId) ].keys()):
segEdgeErr.add(thisPair)
continue
listOfAllError = []
for smg in lgObj.subStructIterator(depths):
#if one segment is in the segment error set
showIt = False
if len(set(smg.nodes.keys()).intersection(allSegWithErr)) > 0:
showIt = True
for pair in smg.edges.keys():
if pair in segEdgeErr:
showIt = True
continue
if showIt:
#build the smg for the prim from lgGT
allPrim = []
for s in smg.nodes.keys():
#print allPrim
smgPrim1 = self.getSubSmallGraph(allPrim)
#build the smg for the prim from lgGT
smgPrimGT = lgGT.getSubSmallGraph(allPrim)
listOfAllError.append((smg,smgPrimGT,smgPrim1))
return listOfAllError
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
################################################################
# Utility functions
################################################################
def mergeLabelLists(llist1, weight1, llist2, weight2, combfn):
"""Combine values in two label lists according to the passed combfn
function, and passed weights for each label list."""
# Combine values for each label in lg2 already in self.
allLabels = set(llist1.items())\
.union(set(llist2.items()))
# have to test whether labels exist
# in one or both list.
for (label, value) in allLabels:
if label in llist1.keys() and \
label in llist2.keys():
llist1[ label ] = \
combfn( llist1[label], weight1,\
llist2[label], weight2 )
elif label in llist2.keys():
llist1[ label ] = \
weight2 * llist2[label]
else:
llist1[ label ] = \
weight1 * llist1[label]
def mergeMaps(map1, weight1, map2, weight2, combfn):
"""Combine values in two maps according to the passed combfn
function, and passed weights for each map."""
# Odds are good that there are built-in function for this
# operation.
objects1 = map1.keys()
objects2 = map2.keys()
allObjects = set(objects1).union(set(objects2))
for object in allObjects:
if object in objects1 and object in objects2:
# Combine values for each label in lg2 already in self.
mergeLabelLists(map1[object],weight1, map2[object], weight2, combfn )
# DEBUG: no relationship ('missing') edges should
# be taken as certain (value 1.0 * weight) where not explicit.
elif object in objects2:
# Use copy to avoid aliasing problems.
# Use appropriate weight to update value.
map1[ object ] = copy.deepcopy( map2[ object ] )
for (label, value) in map1[object].items():
map1[object][label] = weight2 * value
map1[object]['_'] = weight1
else:
# Only in current map: weight value appropriately.
for (label, value) in map1[object].items():
map1[object][label] = weight1 * value
map1[object]['_'] = weight2
def getEdgesToNeighbours(nodes,edges):
"""return all edges which are coming from one of the nodes to out of these nodes"""
neigb = set([])
for (n1,n2) in edges:
if (n1 in nodes and not n2 in nodes):
neigb.add((n1,n2))
return neigb
def getEdgesBetweenThem(nodes,edges):
"""return all edges which are coming from one of the nodes to out of these nodes"""
edg = set([])
for (n1,n2) in edges:
if (n1 in nodes and n2 in nodes):
edg.add((n1,n2))
return edg
def defaultMetric(labelList1, labelList2):
#new way but with 1 label per node
diff = set(labelList1) ^ (set(labelList2)) # symetric diff
if len(diff) == 0:
return (0,[])
else:
ab = diff&set(labelList1)
ba = diff&set(labelList2)
return (max(len(ab),len(ba) ),[(":".join(ab),":".join(ba))])
#old way : return set(labelList1) == set(labelList2)
def defaultMetricXx(labelList1, labelList2):
#new way but with 1 label per node
syn = {'X':'x','\\times':'x', 'P':'p', 'O':'o','C':'c'}
def replace(x):
if x in syn.keys():
return syn[x]
else:
return x
a = map(replace, labelList1)
b = map(replace, labelList2)
diff = set(a) ^ (set(b)) # symetric diff
if len(diff) == 0:
return (0,[])
else:
ab = diff&set(a)
ba = diff&set(b)
return (max(len(ab),len(ba) ),[(":".join(ab),":".join(ba))])
#old way : return set(labelList1) == set(labelList2)