Newer
Older
self.elabels[ edge ][ label ] = 1.0 - currentValue
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
def subStructIterator(self, nodeNumbers):
""" return an iterator which gives all substructures with n nodes
n belonging to the list depths"""
if(isinstance(nodeNumbers, int)):
nodeNumbers = [nodeNumbers]
subStruct = []
#init the substruct with isolated nodes
for n in self.nlabels.keys():
subStruct.append(set([n]))
if 1 in nodeNumbers:
yield smallGraph.SmallGraph([(n, "".join(self.nlabels[n].keys()))], [])
#print(subStruct)
for d in range(2,max(nodeNumbers)+1):
#add one node to each substructure
newSubsS = set([])
newSubsL = []
for sub in subStruct:
#print (" with " + str(sub))
le = getEdgesToNeighbours(sub,self.elabels.keys())
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 primitive in nodelist and all edges
between them"""
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"""
allerrors = []
for struc in self.subStructIterator(depths):
sg2 = olg.getSubSmallGraph(struc.nodes.keys())
if(not (struc == sg2)):
allerrors.append((struc,sg2))
return allerrors
def compareSegmentsStruct(self, lg2,depths):
"""Compute the number of differing segments, and record disagreements
in a confusion matrix (histogramm).
The primitives in each graph 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.
the first key value of the matrix is the self obj structure, which
gives the structure of the corresponding primitives which is the key
to get the error structure"""
(sp1, ps1, _, sre1) = self.segmentGraph()
(sp2, ps2, _, sre2) = lg2.segmentGraph()
#byValue = lambda pair: pair[1] # define key for sort comparisons.
allNodes = set(ps1.keys())
#FIX : this this not the case in spare representation
assert allNodes == set(ps2.keys())
segDiffs = set()
correctSegments = set()
for primitive in ps1.keys():
# 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 lg2.nlabels[primitive]:
# Obtain sets of primitives sharing a segment for the current
# primitive for both graphs.
# Each of sp1/sp2 are a map of ( {prim_set}, label ) pairs.
segPrimSet1 = sp1[ ps1[primitive] ][0]
segPrimSet2 = sp2[ ps2[primitive] ][0]
# Only create an entry where there are disagreements.
if segPrimSet1 != segPrimSet2:
segDiffs.add( ( ps1[primitive], ps2[primitive]) )
else:
correctSegments.add(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(( ps1[primitive], ps2[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.
firstPrim = list(sp1[seg][0])[0]
if set(self.nlabels[ firstPrim ].keys()) != set(lg2.nlabels[ firstPrim ].keys()):
segDiffs.add(( ps1[primitive], ps2[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 sp1.iteritems():
lgObj.nlabels[sid] = self.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 sre1.keys():
# TODO : check if it sp1[thisPair[0]] instead sp1[thisPair[0]][0]
thisParentIds = set(sp1[ thisPair[0] ][0])
thisChildIds = set(sp1[thisPair[1] ][0])
lgObj.elabels[thisPair] = self.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 lg2.elabels.keys() or \
not set(self.elabels[ (parentId, childId) ].keys()) == \
set(lg2.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 self
allPrim = []
for s in smg.nodes.keys():
allPrim.extend(sp1[s][0])
print allPrim
smgPrim1 = self.getSubSmallGraph(allPrim)
#build the smg for the prim from lg2
allPrim = []
for s in smg.nodes.keys():
allPrim.extend(sp2[s][0])
smgPrim2 = lg2.getSubSmallGraph(allPrim)
listOfAllError.append((smg,smgPrim1,smgPrim2))
return listOfAllError
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
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
################################################################
# 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