-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinaccuracy.py
More file actions
64 lines (55 loc) · 2.33 KB
/
Copy pathinaccuracy.py
File metadata and controls
64 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# inaccuracy.py: Inaccuracy computations for identity linking model
# Copyright (C) 2018 Libor Polčák <ipolcak@fit.vutbr.cz>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from id_attrs import id_attrs
class inaccuracy_list(object):
""" This datastructure stores a list of identifiers and their minimal inaccuracy """
def __init__(self, iterable):
self.__content = {}
self.__update(iterable)
def __update(self, iterable):
""" Updates current list with items in the iterable. """
for item in iterable:
if item.value in self.__content:
self.__content[item.value] = min(self.__content[item.value], \
item.inaccuracy)
else:
self.__content[item.value] = item.inaccuracy
def update(self, other):
""" Updates current list with items in the other inaccuracy_list. """
self.__update(other.get_all())
def get_all(self):
""" Returns all items. """
return [id_attrs(k, v) for k, v in self.__content.items()]
def __iter__(self):
""" Returns an iterator. """
return iter(self.get_all())
def compute_inaccuracy(g, p):
""" Returns minimal path inaccuracy. """
inaccuracy = 0
for n1, n2 in zip(range(len(p)), range(1, len(p))):
src = p[n1]
dst = p[n2]
inaccuracy += __get_edge_min_inaccuracy(g.edge[src][dst])
return inaccuracy
def __get_edge_min_inaccuracy(multie):
""" Returns minimal inaccuracy of the multiedge. """
min_inaccuracy = None
for e in multie.values():
try:
min_inaccuracy = min(min_inaccuracy, e["inaccuracy"])
except TypeError: # No inaccuracy set, yet
min_inaccuracy = e["inaccuracy"]
return min_inaccuracy