-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvDir2AdjList.jl
348 lines (287 loc) · 14.4 KB
/
csvDir2AdjList.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#Doing things another way, even if not better, not over thinking this, which is not what a programming mindset is supposed to do, but jumping in.
#produce a key which counts the total number of significant counts for each pair across the windows were found
function dictTotalThresholdsAdjListWindowCount(winAggDict,startYr,endYr,windowSize)
#a new dictionary which is non-windows
#winDictTotal = Dict()
#create the structure of the adjacency list where we will add the counts found to for each pair
#produce an initialized country pairing adjacency list for the threshold passings, the window interval is not used, as we need the full country pairings, not the window subset
thresholdSignificantAdjListTOTAL = initCountryPairAdjList(startYr,endYr)
#loop through the keys of the Dict to add the significant pairs to the
yr = startYr
while( (yr+windowSize) <= endYr )
threshSigAdjListTMP = winAggDict["$(yr)-$(yr+windowSize)"]["thresholdSigAdjList"]
for rowIndWin in 1:size(threshSigAdjListTMP,1)#look through every pairing in this window
for rowIndTotal in 1:size(thresholdSignificantAdjListTOTAL,1)
if((thresholdSignificantAdjListTOTAL[rowIndTotal,1] == threshSigAdjListTMP[rowIndWin,1]) && (thresholdSignificantAdjListTOTAL[rowIndTotal,2] == threshSigAdjListTMP[rowIndWin,2]))
thresholdSignificantAdjListTOTAL[rowIndTotal,3] += threshSigAdjListTMP[rowIndWin,3]
end
end
end
yr = yr + windowSize
end
#add the accumulated significance total to the dictionary
winAggDict["thresholdSignificantAdjListTOTAL"] = thresholdSignificantAdjListTOTAL
#also add the full total country pairing name list for the window set
winAggDict["countriesNamesTotal"] = subsetCountryNamesArray(startYr,endYr)
return winAggDict
end
#use the windowConf dictionary which has the threshold values for significance of the average values to determine for each window which country pairings were significant in a new window key 'thresholdSignificantAdjList'
function windowsDictThresholdsAdjList(windowConf, startYr = 1980, endYr = 1990, windowSize = 5)
#from the csv data build the dictionary for the basic information on the country pair lists, aggregate scores and average of the aggregate scores
winAggDict = windowsDictScoreAdjList(startYr, endYr, windowSize)
#println(keys(winAggDict["1980-1985"]))#["1980-1985"]["countries"])
#println(winAggDict["1980-1985"]["avgScoreAggregateAdjList"])
#windowConf is a dictionary with the window keys as winDicts here, and each window has a particular threshold
#extract the threshold, extract the average score matrix, produce an init country pairing adjacency matrix and then put it as a threshold passings component to the dictionary
yr = startYr
while( (yr+windowSize) <= endYr )
#get the threshold value (float)
thresholdTmp = windowConf["$(yr)-$(yr+windowSize)"]
#extract the average of the aggregate (adjacency list of country pairs)
avgAggAdjList = winAggDict["$(yr)-$(yr+windowSize)"]["avgScoreAggregateAdjList"]
#produce an initialized country pairing adjacency list for the threshold passings
thresholdSignificantAdjList = initCountryPairAdjList(yr,yr+windowSize)#(startYr,endYr)#!!!!!XXXX
#now set the pairs of country rows for each significant pair to 1 from 0
#HERE we do the THRESHOLD COMPARISON
if(windowConf["tailSide"]=="upper" || windowConf["tailSide"]=="right")
#println("upper")
for rowInd in 1:size(avgAggAdjList,1)
if(avgAggAdjList[rowInd,3] >= thresholdTmp)
thresholdSignificantAdjList[rowInd,3] = 1
end
end
else
#println("lower")
for rowInd in 1:size(avgAggAdjList,1)
if(avgAggAdjList[rowInd,3] <= thresholdTmp)
thresholdSignificantAdjList[rowInd,3] = 1
end
end
end
#=
for rowInd in 1:size(avgAggAdjList,1)
if(avgAggAdjList[rowInd,3] >= thresholdTmp)
thresholdSignificantAdjList[rowInd,3] = 1
end
end
=#
winAggDict["$(yr)-$(yr+windowSize)"]["thresholdSigAdjList"] = thresholdSignificantAdjList
yr = yr + windowSize
end
return winAggDict
end
#Given the total time interval, and windowSize, produce a dictionary for each window
#Returned is a dictionary for the time windows
#"countries" key is the unique list of country names for that window
#"scoreAggregateAdjList" key is the aggregate for the country pairs in each window
function windowsDictScoreAdjList(startYr = 1980, endYr = 1990, windowSize = 5)
winDict = Dict()
yr = startYr
while( (yr+windowSize) <= endYr )
winDict["$(yr)-$(yr+windowSize)"] = Dict()
winDict["$(yr)-$(yr+windowSize)"]["countries"] = subsetCountryNamesArray(yr,yr+windowSize)#(startYr,endYr)
aggAdjList = aggregateAdjList(yr,yr+windowSize)#(startYr,endYr)
winDict["$(yr)-$(yr+windowSize)"]["scoreAggregateAdjList"] = aggAdjList
#compute the average over the window for the scores
avgAggAdjList = initCountryPairAdjList(yr,yr+windowSize)#(startYr,endYr)
avgAggAdjList[:,3] = aggAdjList[:,3].*(1/(windowSize+1))
winDict["$(yr)-$(yr+windowSize)"]["avgScoreAggregateAdjList"] = avgAggAdjList
yr = yr + windowSize
end
return winDict
end
#create and return an adjacency country pair list with initially zero for the scores
function initCountryPairAdjList(startYr = 1980, endYr = 1990)
adjList = yearsScoreAdjList(startYr, endYr)
cntryNames = subsetCountryNamesArray(startYr,endYr)
countryNum = length(cntryNames)
aggregateAdjList = Array{Any}(undef,countryNum^2 - countryNum, 3)
#INIT: fill the names and initial aggregate scores
tmpRow = 1
for ii in 1:length(cntryNames) #over each first cycle of countries
for jj in 1:length(cntryNames) #over second cycle of countries
if(ii != jj)
aggregateAdjList[tmpRow,:] = [cntryNames[ii] cntryNames[jj] 0]
tmpRow = tmpRow + 1
end
end
end
return aggregateAdjList
end
#produce the aggregate score for the 'to-and-from' from the adjacency list so that we have the unique pairings and total scores
function aggregateAdjList(startYr = 1980, endYr = 1990)
adjList = yearsScoreAdjList(startYr, endYr)
cntryNames = subsetCountryNamesArray(startYr,endYr)
countryNum = length(cntryNames)
aggregateAdjList = Array{Any}(undef,countryNum^2 - countryNum, 3)
#INIT: fill the names and initial aggregate scores
tmpRow = 1
for ii in 1:length(cntryNames) #over each first cycle of countries
for jj in 1:length(cntryNames) #over second cycle of countries
if(ii != jj)
aggregateAdjList[tmpRow,:] = [cntryNames[ii] cntryNames[jj] 0]
tmpRow = tmpRow + 1
end
end
end
#FILL: the scores for the AdjList
adjListTotal = yearsScoreAdjList(startYr, endYr)
for ii in 1:size(aggregateAdjList,1)#look at every country pair
for rowInd in 1:size(adjListTotal,1)
if((aggregateAdjList[ii,1]==adjListTotal[rowInd,1]) && (aggregateAdjList[ii,2]==adjListTotal[rowInd,2]))
aggregateAdjList[ii,3] = aggregateAdjList[ii,3] + adjListTotal[rowInd,4]
end
end
end
return aggregateAdjList
end
#look at the directory and load every year in the range of the .csv files, then produce a final matrix(adjacency list) which is every year in the countryFrom|countryTo|year|score for this 'subset'
function yearsScoreAdjList(startYr = 1980, endYr = 1990)
dirFiles = readdir("./dataTables/")
adjMatScore = []
for dF in dirFiles
yrTmp = parse(Int,((split(dF,"."))[1]))
if(yrTmp < startYr || yrTmp > endYr)
continue
end
fileTmp = open(string("./dataTables/",dF))
linesTmp = readlines(fileTmp)#read each file lines
origColNames = split(linesTmp[1],r",|\n",keepempty=false)#get line1 into components
for ii=2:length(linesTmp)#get the names on the rows
rowTmp = split(linesTmp[ii],r",|\n",keepempty=false)
for jj=2:length(origColNames)
if(ii!=jj)
#print("$(rowTmp[1])->$(origColNames[jj])=$(rowTmp[jj]),")
if(isempty(adjMatScore) == true)
adjMatScore = Array{Any}(undef,1,4)
adjMatScore[1,:] = [rowTmp[1],origColNames[jj],yrTmp,parse(Int,rowTmp[jj])]
else
#println([rowTmp[1],origColNames[jj],yrTmp,parse(Int,rowTmp[jj])])
adjMatScore = vcat(adjMatScore , [rowTmp[1] origColNames[jj] yrTmp parse(Int,rowTmp[jj])])
end
end
end
end
end
adjMatScoreNoShows = missingCountryScoreInsert(adjMatScore)
return adjMatScoreNoShows
#return adjMatScore
end
#in this function we will pass the adjMatScore structure (mat)
#in each year, find the average score for all the countries per year
#find countries which are not in each year
#add countries and the average score for each year
function missingCountryScoreInsert(adjMatScore)
adjMatScoreTmp = adjMatScore#what is returned
countriesUnique = unique(adjMatScore[:,1])
yearsUnique = unique(adjMatScore[:,3])
for yy in yearsUnique
rowsYear = (adjMatScore[:,3] .== yy)
yrCountries = unique(adjMatScore[rowsYear,1])
#println(yrCountries)
ind = 0
for cc in countriesUnique
if( !(cc in yrCountries) )
ind+=1
end
end
sumTmp = sum(adjMatScore[rowsYear,4])
muTmp = round(sumTmp/(length(adjMatScore[rowsYear,4])+ind),digits=4)
#println(muTmp)
muTmp = convert(Int,round(muTmp))
#println(muTmp)
for cc in countriesUnique
if( !(cc in yrCountries) )
#println("$(cc)--$(yy)")
for cTmp in countriesUnique
#println([cc cTmp yy muTmp])
#println([cTmp cc yy muTmp])
adjMatScoreTmp = vcat(adjMatScoreTmp,[cc cTmp yy muTmp])
adjMatScoreTmp = vcat(adjMatScoreTmp,[cTmp cc yy muTmp])
end
end
end
end
return adjMatScoreTmp
end
#look at the directory and load every .csv file, then produce a final matrix which is the countryFrom|countryTo|year|score
function totalScoreAdjList()
dirFiles = readdir("./dataTables/")
adjMatScore = []
ind = 1
for dF in dirFiles
yrTmp = parse(Int,((split(dF,"."))[1]))
fileTmp = open(string("./dataTables/",dF))
linesTmp = readlines(fileTmp)#read each file lines
origColNames = split(linesTmp[1],r",|\n",keepempty=false)#get line1 into components
for ii=2:length(linesTmp)#get the names on the rows
rowTmp = split(linesTmp[ii],r",|\n",keepempty=false)
for jj=2:length(origColNames)
if(ii!=jj)
#print("$(rowTmp[1])->$(origColNames[jj])=$(rowTmp[jj]),")
if(isempty(adjMatScore) == true)
adjMatScore = Array{Any}(1,4)
adjMatScore[1,:] = [rowTmp[1],origColNames[jj],yrTmp,parse(Int,rowTmp[jj])]
else
#println([rowTmp[1],origColNames[jj],yrTmp,parse(Int,rowTmp[jj])])
adjMatScore = vcat(adjMatScore , [rowTmp[1] origColNames[jj] yrTmp parse(Int,rowTmp[jj])])
end
end
end
end
end
return adjMatScore
end
#return an array of unique country names for the time period
function subsetCountryNamesArray(stYr,endYr)
#get the dictionary of the year and names
countryNamesYrDict = subsetHeaderNames(stYr,endYr)
totalNames = []
for yearTmp in keys(countryNamesYrDict)
totalNamesTmp = countryNamesYrDict[yearTmp]
append!(totalNames,totalNamesTmp)
end
return sort(unique(totalNames))
end
#search the directory for the countries in the specific years listed; return dictionary of the year to names of countries
function subsetHeaderNames(stYr,endYr)
dirFiles = readdir("./dataTables/")
namesDict = Dict()
for dF in dirFiles
yrTmp = parse(Int,((split(dF,"."))[1]))
if(stYr <= yrTmp <= endYr)
fileTmp = open(string("./dataTables/",dF))
linesTmp = readlines(fileTmp)#read each file lines
origColNames = split(linesTmp[1],r",|\n",keepempty=false)#get line1 into components
splice!(origColNames,1)#get rid of topleft corner
rowNAMES = []#temp store
for ii=2:length(linesTmp)#get the names on the rows
rowNAMEStmp = split(linesTmp[ii],r",|\n",keepempty=false)
append!(rowNAMES, [rowNAMEStmp[1]])
end
totalNamesYr = sort(unique(append!(rowNAMES,origColNames)))#for yrTmp names
namesDict[yrTmp] = totalNamesYr#
end
end
return namesDict
end
#search the directory for the complete set of countries having appeared
function totalHeaderNames()
dirFiles = readdir("./dataTables/")
namesDict = Dict()
for dF in dirFiles
yrTmp = parse(Int,((split(dF,"."))[1]))
fileTmp = open(string("./dataTables/",dF))
linesTmp = readlines(fileTmp)#read each file lines
origColNames = split(linesTmp[1],r",|\n",keepempty=false)#get line1 into components
splice!(origColNames,1)#get rid of topleft corner
rowNAMES = []#temp store
for ii=2:length(linesTmp)#get the names on the rows
rowNAMEStmp = split(linesTmp[ii],r",|\n",keepempty=false)
append!(rowNAMES, [rowNAMEStmp[1]])
end
totalNamesYr = sort(unique(append!(rowNAMES,origColNames)))#for yrTmp names
namesDict[yrTmp] = totalNamesYr#
end
return namesDict
end