-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataAndDefns.jl
241 lines (203 loc) · 7.06 KB
/
DataAndDefns.jl
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
include("SymGrpAndReps.jl")
using JSON
# 3 instances of SZN
@enum SZN WCT17=1 WCT18 WCT19
evtYearToSZN = Dict([ "2017"=>WCT17,"2018"=>WCT18,"2019"=>WCT19 ])
# 12 instances of EVT
@enum EVT BaliPro=1 BellsBeach Fiji FrancePro GoldCoast JBayOpen MargaretRiver PenichePro PipeMasters RioPro Teahupoo Trestles
evtNameToEVT = Dict([
"Bali Pro" => BaliPro,
"Bells Beach" => BellsBeach,
"Fiji" => Fiji,
"France" => FrancePro,
"Gold Coast" => GoldCoast,
"J-Bay Open" => JBayOpen,
"Margaret River" => MargaretRiver,
"Peniche Pro" => PenichePro,
"Pipe Masters" => PipeMasters,
"Rio Pro" => RioPro,
"Tahiti" => Teahupoo,
"Trestles" => Trestles
])
const RND = Tuple([i for i in 1:8])
const HT = Tuple(1:16)
const CC = false
if CC
data = JSON.parse( open("data/CleanAllDataCC.txt", "r") )
@enum ORIG AUS=1 BRA ESP FRA PRT USA ZAF FJI IDN ITA JPN NZL
const C = collect(instances(ORIG)[1:7])
#= CTRY Defined by ISO 3166-1
Using 2 digit codes
"-1"
=#
isoDict = Dict([
"Australia" => AUS,
"Brazil" => BRA,
"Basque Country" => ESP,
"Spain" => ESP,
"France" => FRA,
"Portugal" => PRT,
"United States" => USA,
"South Africa" => ZAF,
"Fiji" => FJI,
"Indonesia" => IDN,
"Italy" => ITA,
"Japan" => JPN,
"New Zealand" => NZL
])
else
data = JSON.parse( open("data/CleanAllDataNC.txt", "r") )
@enum ORIG AUS=1 BAS BRA FRA HAW PRT PYT USA ZAF ESP FJI IDN ITA JPN NZL
const C = instances(ORIG)[1:9]
#= ORIG Defined by what WSL had.
Using 3 digit codes from ISO 3166-1, plus BAS for Basque Country.
"-1"
=#
isoDict = Dict([
"Australia" => AUS,
"Basque Country" => BAS,
"Brazil" => BRA,
"France" => FRA,
"French Polynesia" => PYT,
"Hawaii" => HAW,
"Portugal" => PRT,
"United States" => USA,
"South Africa" => ZAF,
"Spain" => ESP,
"Fiji" => FJI,
"Indonesia"=>IDN,
"Italy"=>ITA,
"Japan"=>JPN,
"New Zealand"=>NZL
])
end
filter!(wave-> wave[2]["subScoOrigDefect"]==false,data);
WIDs= sort(collect(keys(data)));
EvtIds = unique(map(x->data[x]["evtId"],WIDs));
HeatIds = unique(map(x->data[x]["heatId"],WIDs));
maxRnd = Dict()
for id in EvtIds
thisEvt = filter(w->data[w]["evtId"]==id, WIDs)
maxRnd[id] = maximum(map(w->Base.parse(Int,data[w]["rnd"]), thisEvt))
end
WAVES = []
for wave in data
subScoOrig = map(x-> isoDict[x], wave[2]["subScoOrig"])
subSco = map(x->round(x,digits=1),wave[2]["subSco"])
athorig = isoDict[ wave[2]["athOrig"] ]
origs = unique(subScoOrig)
scos = Tuple(sort(unique(subSco)))
part_c = map(s-> subScoOrig[ findall(==(s),subSco) ], scos)
part_b = map(X-> X .== athorig ,part_c)
mult_c = map(c->count(==(c),subScoOrig), C)
mult_b = map(b->count(==(b),subScoOrig .== athorig ), (0,1) )
wv = (
id = wave[1],
szn = evtYearToSZN[ wave[2]["evtYear"] ],
evtOrig = isoDict[ wave[2]["evtOrig"] ],
evt = evtNameToEVT[ wave[2]["evtName"] ],
rnd = maxRnd[wave[2]["evtId"]]-Base.parse(Int,wave[2]["rnd"])+1,
ht = Base.parse(Int, wave[2]["heat"]),
athOrig = isoDict[ wave[2]["athOrig"] ],
athName = wave[2]["athName"],
I_match = athorig in subScoOrig,
m_c = mult_c,
m_b = mult_b,
λ_c = part_c,
λ_b = part_b,
s=scos
)
push!(WAVES,wv)
end
sort!(WAVES);
⊗(A::Array{T},B::Array{T}) where T<: Number = prod.(Base.product(A,B))
⊗(V::Vararg{Array{T}}) where T<: Number =reduce(⊗,V)
⊗(a::NTuple{T},b::NTuple{T}) where T = (a...,b...)
⊗(a::NTuple{N,T},b::NTuple{M,T}) where {T,N,M} = (a...,b...)
⊕(V::Vararg{Array{T,N} where N}) where T = [ sum(filter(x->size(x)==d,V)) for d in unique(size.(V)) ]
⊕(V::Vararg{Array{T,N}}) where {T,N} = [ sum(filter(x->size(x)==d,V)) for d in unique(size.(V)) ]
# maybe
⨁(A::Array{Array{T,N} where N,1}) where T <: Number = [ sum(filter(x->size(x)==d,A)) for d in sort(unique(size.(A))) ]
⨁(A::Array{Array{T},1}) where T<:Number = [ sum(filter(x->size(x)==d,A)) for d in sort(unique(size.(A))) ]
⨁(A::Array{Array{T,N},1}) where {T<:Number,N} = [ sum(filter(x->size(x)==d,A)) for d in sort(unique(size.(A))) ]
⨂(A::Array{Array{T,N} where N,1}) where T <: Number = reduce(⊗,A)
E(X::Array,i::K) where {K<:Integer} =dropdims( sum(X,dims=setdiff(1:ndims(X),i)),dims=tuple(setdiff(1:ndims(X),i)...) )
E(X::Array,I::NTuple) =dropdims(sum(X,dims=setdiff(1:ndims(X),I)),dims=tuple(setdiff(1:ndims(X),I)...))
μₐ(D::Array{Array{T,N} where N}) where T<:Number = map(x->sum(abs.(𝐒(x))),⨁(D))
μₛ(D::Array{Array{T,N} where N}) where T<:Number = map(x->sum(abs.(𝐀(x))),⨁(D))
#cov(X::Array,i::K,j::K) where {K<:Integer} = E(X,(i,j))-E(X,i)⊗E(X,j)
#cov(X::Array,I::NTuple,j::K) where {K<:Integer} = E(X,(I...,j))-E(X,I)⊗E(X,j)
#cov(X::Array,i::K,J::NTuple) where {K<:Integer} = E(X,(i,J...))-E(X,i)⊗E(X,J)
#cov(X::Array,I::NTuple,J::NTuple) =E(X,(I...,J...))-E(I)⊗E(J)
e_orig(c::ORIG) = vcat(zeros(Int(c)-1),1,zeros(length(instances(ORIG))-Int(c)))
e(c::ORIG) = vcat(zeros(Int(c)-1),1,zeros(length(C)-Int(c)))
e(b::Bool) = [ Int(b==0); Int(b==1)]
e(i::Integer,n::Integer) = vcat(zeros(i-1),1,zeros(n-i))
function embedd(t::Tuple{T,Vararg{T,N} where N} where T <:Union{Array{ORIG,1},BitArray{1}};strong=false, countinblock=false, minimal=false, normalize=false)
if minimal==true # then embedd in VS with as many dims are needed
orgs = unique(union(t...))
k = length(orgs)
idx = Dict([c => i for (i,c) in enumerate(sort(orgs)) ])
if countinblock==true
q = map(cls->[ count(==(c),cls)*e(idx[c],k) for c in unique(cls)],t)
else
q = map(cls->map(c->e(idx[c],k),cls),t)
end
else # embedd in VS with e_Int(label)
if countinblock==true
q = map(cls->[count(==(c),cls)*e(c) for c in unique(cls)],t)
else
q = map(cls->e.(cls),t)
end
end
if normalize==true
q = map(pod -> pod ./ sum(sum(pod)), q)
end
if strong==true # then do sum within pod
q = map(pod->sum(pod),q)
return ⊗(q...)
else
blocks = map(pod->𝐒(⊗(pod...)),q)
return ⊗(blocks...)
end
end
e_G(t) = embedd(t)
# varRng takes in element of field name s of a wave ∈ WAVES and returns sorted {wave.s | wave ∈ WAVES}
varRng(s::Symbol) = sort(unique(map(w->w[s],WAVES)))
varRng(S::NTuple{N,Symbol}) where N = sort(unique(NamedTuple{S}.(WAVES)))
function partitionBy(s::Symbol)
By = varRng(s)
partition = [b => [] for b in By]
for wave in WAVES
i = findfirst(==(wave[s]), By) # note: can call field of named tuple with []
push!(partition[i][2], wave )
end
return partition
end
function partitionBy(s::Symbol;val::Symbol)
By = varRng(s)
partition = [b => [] for b in By]
for wave in WAVES
i = findfirst(==(wave[s]), By) # note: can call field of named tuple with []
push!(partition[i][2], wave[val] )
end
return partition
end
function partitionBy(S::NTuple{N,Symbol}) where N
By = varRng(S)
partition = [b => [] for b in By]
for wave in WAVES
i = findfirst(==(NamedTuple{S}(wave)), By) # note: can call field of named tuple with []
push!(partition[i][2], wave )
end
return partition
end
function partitionBy(S::NTuple{N,Symbol};vals::NTuple{N,Symbol}) where N
By = varRng(S)
partition = [b => [] for b in By]
for wave in WAVES
i = findfirst(==(NamedTuple{S}(wave)), By) # note: can call field of named tuple with []
push!(partition[i][2], NamedTuple{vals}(wave) )
end
return partition
end