-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffectiveness_estimation_functions.py
89 lines (75 loc) · 3.04 KB
/
effectiveness_estimation_functions.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# <effectiveness_estimation_functions.py>
#
# @Author: Lucas Pascotti Valem <[email protected]>
#
#-------------------------------------------------------------------------------
#
# This file is part of Unsupervised Selective Rank Fusion Framework (USRF).
#
# USRF 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 2 of the License, or
# (at your option) any later version.
#
# USRF 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 USRF. If not, see <http://www.gnu.org/licenses/>.
#
from multiprocessing import Pool
def get_effectiveness_func(effectiveness_estimation_measure):
if effectiveness_estimation_measure == "authority":
return compute_authority_score
if effectiveness_estimation_measure == "reciprocal":
return compute_reciprocal_score
print("\n ERROR: Unknown effec. estim. measure:",
effectiveness_estimation_measure)
exit(1)
def compute_authority_score(ranked_lists, index, top_k):
score = 0
rk1 = ranked_lists[index][:top_k]
for img1 in rk1:
rk2 = ranked_lists[img1][:top_k]
for img2 in rk2:
for current_img in rk1:
if img2 == current_img:
score += 1
break
return (score/(top_k**2))
def compute_reciprocal_score(ranked_lists, index, top_k):
score = 0
rk1 = ranked_lists[index][:top_k]
for img1 in rk1:
rk2 = ranked_lists[img1][:top_k]
for img2 in rk2:
for k, current_img in enumerate(rk1):
if img2 == current_img:
score += 1/(k+1)
break
return (score/(top_k**2))
def compute_rk_effectiveness(effectiveness_function, ranked_lists, top_k):
n = int(len(ranked_lists))
total = 0
for index in range(n):
total += effectiveness_function(ranked_lists, index, top_k)
return total/n
def compute_descriptors_effectiveness(parameters,
effectiveness_function,
ranked_lists,
descriptors,
top_k):
effectiveness = {}
print("\n Computing effectiveness estimations...")
n_pools = parameters["multithreading_pools"]
pool_params = [[effectiveness_function, ranked_lists[descriptor], top_k]
for descriptor in descriptors]
with Pool(n_pools) as p:
# Some print messages may not be reported while running pool map
output_effectiveness = p.starmap(compute_rk_effectiveness, pool_params)
for i, descriptor in enumerate(descriptors):
effectiveness[descriptor] = output_effectiveness[i]
print(" Done!")
return effectiveness