-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompositionScript.jl
More file actions
194 lines (174 loc) · 8.1 KB
/
compositionScript.jl
File metadata and controls
194 lines (174 loc) · 8.1 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using SparseArrays
using StatsBase
using ProgressMeter
using Compat, Random, Distributions
using LinearAlgebra
using PlotlyJS
using Blink
using WebIO
using ORCA
include("Lib/ModelDense.jl")
include("Lib/ModelHDSparse.jl")
include("Lib/ModelHDBinary.jl")
function generateRun(nbVectors,
nbNeighbours,
vRndGenFn,
compositionFn,
similFn)
compositionScores, randomScores, singleRetrievalScores =
Dict(), Dict(), Dict()
randVectors = [map(x -> vRndGenFn(1), 1:nbVectors)]
for i=1:nbNeighbours-1
println(i)
compositionScores[i] = (length(randVectors) == 1) ?
map((v, w) -> similFn(v, w), randVectors[1], randVectors[end]) :
map((v, w) -> similFn(v, w), randVectors[end-1], randVectors[end])
singleRetrievalScores[i] =
map((v, w) -> similFn(v, w), randVectors[1], randVectors[end])
rdnscores = Float64[]
for i=1:length(randVectors[end])
for j=1:2 # i we don't span all combinations
idx = max((i + j) % nbVectors, 1)
push!(rdnscores,
similFn(randVectors[end][i], randVectors[end][idx]))
end
end
randomScores[i] = rdnscores
push!(randVectors,
map(v -> compositionFn([v, vRndGenFn(1)]), randVectors[end]))
end
map(i -> mean_and_std(compositionScores[i]), 1:nbNeighbours-1),
map(i -> mean_and_std(singleRetrievalScores[i]), 1:nbNeighbours-1),
map(i -> mean_and_std(randomScores[i]), 1:nbNeighbours-1)
end
function makeTrace(x, y, error, dict)
if error != Nothing
error_y=attr(type="data",color=dict[:color],symmetric=true,array=error, opacity=0.75)
else
error_y=""
end
scatter(;x=x, y=y, error_y=error_y,
name=dict[:label],
mode=dict[:markers],
marker=attr(color=dict[:color], opacity=0.25,
line_color=dict[:color],
line_width=1, size=10, symbol=dict[:symbol]),
line=attr(width=1))
end
function doAnalysis(dicParameters)
name = dicParameters[:title]
plotsOverview, plotsDeltas = [], []
for (i, dicP) in enumerate(dicParameters[:params])
color = dicParameters[:colors][i]
label = mapreduce(x->string(x, " "), string,
[string(kv[1], ": ", kv[2]) for kv in collect(dicP)])
fctGenerator(x) = dicParameters[:fct](dicP)
fctComposition = dicParameters[:composition]
fctSimilarity = dicParameters[:similarity]
nbVectors, nbNeighbours =
dicParameters[:nbVectors], dicParameters[:nbNeighbours]
compositionScores, singleRetrievalScores, randomScores =
generateRun(nbVectors,
nbNeighbours,
fctGenerator,
fctComposition,
fctSimilarity)
dict = Dict(:label => string("Set_1 VS Set_N: ", "<b>", label,"</b>"),
:color => color, :symbol => "diamond", :markers => "markers")
tr1 = makeTrace(1:nbNeighbours-1,
[s[1] for s in singleRetrievalScores],
[s[2] for s in singleRetrievalScores], dict)
dict = Dict(:label => string("Set_1 VS Set_N: ","<b>", label,"</b>"),
:color => color, :symbol => "diamond", :markers => "lines+markers")
ttr1 = makeTrace(2:nbNeighbours-1,
map((x,y) -> (x[1]-y[1]-(x[2]+y[2])) / (x[2]+y[2]),
singleRetrievalScores[2:end-1],
singleRetrievalScores[3:end]),
Nothing, dict)
dict = Dict(:label => string("Set_N+1 VS Set_N"), #, label,"</b>"),
:color => color, :symbol => "square", :markers => "markers")
tr2 = makeTrace(1:nbNeighbours-1,
[s[1] for s in compositionScores],
[s[2] for s in compositionScores], dict)
dict = Dict(:label => string("Set_N+1 VS Set_N", "<b>", label,"</b>"),
:color => color, :symbol => "square", :markers => "lines+markers")
ttr2 = makeTrace(2:nbNeighbours-1,
map((x,y) -> y[1]-x[1]-(x[2]+y[2]),
compositionScores[2:end-1],
compositionScores[3:end]),
Nothing, dict)
dict = Dict(:label => string("Random VS Random"), :color => color,
:symbol => "circle", :markers => "markers")
tr3 = makeTrace(1:nbNeighbours-1,
[s[1] for s in randomScores],
[s[2] for s in randomScores], dict)
push!(plotsOverview, tr1, tr2, tr3)
push!(plotsDeltas, ttr1, ttr2)
end
layout1 = Layout(;title = name,
xaxis=attr(title="<b> Nb of Elements in Set </b>", showgrid=false, zeroline=false),
yaxis=attr(title="<b> Averaged Similarity </b>", zeroline=false),
shapes=[hline(0)],
font=attr(size=16))
layout2 = Layout(;title = name,
xaxis=attr(title="<b> Nb of Elements in Set </b>", showgrid=false, zeroline=false),
yaxis=attr(title= "<b> Distance to false positive (in std. Deviations) </b>", #"<b> (Sim_N+1-Sim_N + std_N+1+std_N) / (std_N+1+std_N) </b>" ,
zeroline=false),
yaxis=attr(range=[-2,16])
shapes=[hline(0)],
font=attr(size=16))
Dict(:plotOverview => plot(vcat(plotsOverview...), layout1),
:plotDeltas => plot(vcat(plotsDeltas...), layout2))
end
#############
# HD Sparse #
#############
params = map((n, w) -> Dict(:n => n, :w => w),
[1000, 10000, 100000, 1000000], [20, 20, 20, 20])
dicParameters = Dict(:params => params,
:fct => HDSparse.RandomGenerator,
:composition => HDSparse.composition,
:similarity => HDSparse.cosineSimilarity,
:nbVectors => 1000,
:nbNeighbours => 40,
:title => "<b> HD Sparse Vectors </b>",
:colors => ["blue", "green", "brown", "magenta", "red"])
p = dicParameters |> doAnalysis
figFileName = string("Results/HDSparseFig1Overview.jpeg")
savefig(p[:plotOverview], figFileName)
figFileName = string("Results/HDSparseFig1Deltas.jpeg")
savefig(p[:plotDeltas], figFileName)
#############
# HD Binary #
#############
params = map(n -> Dict(:n => n), [500, 5000, 10000, 15000])
dicParameters = Dict(:params => params,
:fct => HDBinary.RandomGenerator,
:composition => HDBinary.composition,
:similarity => HDBinary.cosineSimilarity,
:nbVectors => 1000,
:nbNeighbours => 40,
:title => "HD Dense Binary Vectors",
:colors => ["blue", "green", "brown", "magenta", "red"])
p = dicParameters |> doAnalysis
figFileName = string("Results/HDBinaryFig1Overview.jpeg")
savefig(p[:plotOverview], figFileName)
figFileName = string("Results/HDBinaryFig1Deltas.jpeg")
savefig(p[:plotDeltas], figFileName)
#########
# Dense #
#########
params = map(n -> Dict(:n => n), [50, 500, 1000, 5000])
dicParameters = Dict(:params => params,
:fct => Dense.RandomGenerator,
:composition => Dense.composition,
:similarity => Dense.cosineSimilarity,
:nbVectors => 1000,
:nbNeighbours => 40,
:title => "Dense Vectors",
:colors => ["blue", "green", "brown", "magenta", "red"])
p = dicParameters |> doAnalysis
figFileName = string("Results/HDDenseFig1Overview.jpeg")
savefig(p[:plotOverview], figFileName)
figFileName = string("Results/HDDenseFig1Deltas.jpeg")
savefig(p[:plotDeltas], figFileName)