-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilteredData.js
563 lines (515 loc) · 14.7 KB
/
filteredData.js
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* This class is used to save the currentFilter state.
* Views should only access this data structure.
*/
class FilteredData {
constructor(model, previous) {
if (model) {
this.model = model;
}
const prev = previous || {};
// those are the properties meant for public access
this.regions;
this.regionMap;
this.geographicBounds = prev.geographicBounds || null;
this.finished = true;
this.transitions;
this.aggregatedTransitions;
this.reducedTransitions;
this.highlighted = {
regions: [],
transitions: []
};
this.transitionCount = {
all: 0,
reduced: 0
};
this.transitionsPerRegion = {
min: Infinity,
max: -Infinity,
avg: null
};
this.transitionWeights = {
min: Infinity,
max: -Infinity,
avg: null
};
this.states;
this.maxState;
this.iterationCount = 0;
this.timeRange = prev.timeRange || [];
this.communityCount = prev.communityCount || 1;
// size of the timeline bins in ms (may be overwritten by AGGREGATOR)
this.timeBinSize = prev.timeBinSize || lib.toMillis.day;
/**
* Selects the transition and both target and source nodes
* and sets their flags for highlighting.
*
* @param id
* the id of the transition being highlighted
*/
DISPATCH.on("startTransitionHighlight", id => {
const t = this.model.transitionMap.get(+id);
this.startHighlight(t);
this.highlighted = {
regions: [],
transitions: [t]
};
// use map lookup for faster access
[t.source, t.target].forEach(node => {
node.highlight.h = true;
this.highlighted.regions.push(node);
});
DISPATCH.onTransitionHighlight(id);
});
/**
* Selects multiple transitions and all target and source nodes
* and sets their flags for highlighting.
*
* @param ids
* the ids of the transitions being highlighted
*/
DISPATCH.on("startTransitionHighlightMulti", ids => {
this.highlightMode = true;
this.clearHighlight();
this.highlighted = {
regions: [],
transitions: []
};
ids.forEach(id => {
const t = this.model.transitionMap.get(+id);
t.highlight.h = true;
this.highlighted.transitions.push(t);
});
DISPATCH.onTransitionHighlight(ids[0]);
});
/**
* Selects the region and all incoming and outgoing transitions
* and sets their flags for highlighting.
*
* @param id
* the id of the region being highlighted
*/
DISPATCH.on("startRegionHighlight", id => {
// get region object from id
const region = this.regionMap.get(+id);
if (!region) {
console.error(`undefined region for id ${id}`);
return;
}
this.startHighlight(region);
this.highlighted = {
regions: [region],
transitions: []
};
// highlight all transitions of this region
[region.incTransitions, region.outTransitions].forEach(tr => {
tr.forEach(d => {
if (!d.highlight) {
return;
}
d.highlight.h = true;
this.highlighted.transitions.push(d);
});
});
DISPATCH.onRegionHighlight(id);
});
/**
* Ends highlighting and clears all highlighting flags on all Elements.
*/
DISPATCH.on("endHighlight", () => {
this.highlightMode = false;
this.clearHighlight();
this.highlighted = {
regions: [],
transitions: []
};
DISPATCH.onHighlightEnd();
});
}
/**
* Calculates the geographic bounds of all selected regions.
*/
updateGeoBounds() {
if (!this.model.properties.geographicNodes) {
return this;
}
let minX = 180;
let maxX = -180;
let minY = 90;
let maxY = -90;
this.regions.forEach(r => {
const bbox = r.properties.bbox;
if (bbox[0][0] < minX) { minX = bbox[0][0]; }
if (bbox[0][1] < minY) { minY = bbox[0][1]; }
if (bbox[1][0] > maxX) { maxX = bbox[1][0]; }
if (bbox[1][1] > maxY) { maxY = bbox[1][1]; }
});
// bounds are saved as lat lon
this.geographicBounds = [
[minY, minX],
[maxY, maxX]
];
return this;
}
/**
* Updates the current (time filtered) state for each node
* and stores it in states.
*/
updateStates() {
if (!(this.model.states && this.model.additionalData.allIterations)) {
return this;
}
const interval = this.timeRange;
const allStates = this.model.additionalData.allIterations;
// get first and last iteration
let first = 0;
let last = this.model.states.length - 1;
if (interval) {
first = Math.max(0, Math.floor(lib.daysBetween(lib.baseTimestamp, interval[0])));
last = Math.min(Math.floor(lib.daysBetween(lib.baseTimestamp, interval[1])), this.model.states.length) - 1;
}
if (first > last) {
const t = first;
first = last;
last = t;
}
if (first < 0 || last >= allStates.length) {
console.error(`invalid interval [${first}, ${last}] max. index is ${allStates.length - 1}`);
console.error(this.timeRange);
return this;
}
this.iterationCount = last - first + 1;
const currentStates = new Array(this.model.regions.length).fill(0);
// original edit by sax, refactored
// convert status update: consider only the status at the last time instant of the interval
if (this.model.additionalData.infectionModelDiscrete) {
return this._populateStateArrayLast(currentStates, last);
}
return this._populateAggregatedStateArray(currentStates, first, last);
}
/**
* This function caluclates the state-array using the average state
* for the selected timespan.
*
* Do not alter the variable declerations from var to let/const
* there is a bug in chrome preventing optimization, so the resulting
* code runs much slower!
*/
_populateAggregatedStateArray(currentStates, first, last) {
this.maxState = 0;
// use forEach now as allIterations contains all arrays
for (let i = first; i <= last; i++) {
const status = this.model.additionalData.allIterations[i].status;
if(typeof status === "Array") {
status.forEach((value, index) => {
currentStates[index] += value;
if (value > this.maxState) {
this.maxState = value;
}
});
} else {
// handle objects too
for(let index in status) {
const value = status[index];
currentStates[index] += value;
if (value > this.maxState) {
this.maxState = value;
}
}
}
}
// divide by number of iterations
if (this.iterationCount === 0) {
// do not divide by 0!
return;
}
// or rather multiply inverse
const multiplier = 1 / this.iterationCount;
currentStates.forEach((t, i, arr) => {
arr[i] *= multiplier;
});
this.states = currentStates;
return this;
}
/**
* Calculates the state array, only looking at the last state
* in the given time span.
*/
_populateStateArrayLast(currentStates, last) {
const status = this.model.additionalData.allIterations[last].status;
this.maxState = -1;
for (let j = 0; j < currentStates.length; j++) {
if (this.maxState < status[j]) {
this.maxState = status[j];
}
}
this.states = status;
return this;
}
/**
* Runs commnity detection and updates the community attribute of all nodes.
* Stores the number of detected communities in this.communityCount.
*/
updateCommunities() {
const nodes = this.regions;
const links = this.reducedTransitions;
// convert data-structure for jLouvain
const nodes2 = [];
const links2 = [];
const nodesRemap = [];
nodes.forEach((node, index) => {
nodes2[index] = index;
nodesRemap[node.properties.id] = index;
});
links.forEach((edge, index) => {
const sid = edge.source.properties.id;
const tid = edge.target.properties.id;
links2[index] = {
source: nodesRemap[sid],
target: nodesRemap[tid],
weight: Math.sqrt(edge.weight * 10) / 3.0
};
});
// only do this if community detection is enabled in config
// calculate communities
const community_assignment_result = CONFIG.filter.showCommunities ? jLouvain().nodes(nodes2).edges(links2)() : null;
// apply communities
let max = 0;
for (let i = 0; i < nodes2.length; i++) {
const community = community_assignment_result ? community_assignment_result[i] : 0;
nodes[i].community = community;
max = community > max ? community : max;
}
// save number of communities
// 0 is valid too
this.communityCount = max + 1;
}
/**
* Sets transitions and reduces them to one representing transtion per
* source - target pair.
*/
setTransitions(transitions) {
this.transitions = transitions;
// reduce transitions between the same nodes
this.reducedTransitions = this.reduceTransitions();
this.transitionCount = {
all: transitions.length,
reduced: this.reducedTransitions.length
};
return this;
};
/**
* Sets the Regions and calculates a Map for faster lookups.
*/
setRegions(regions) {
this.regions = regions;
// create map and reset values
const map = new Map();
const len = this.regions.length;
for (let i = 0; i < len; i++) {
regions[i].highlight = {
h: false
};
const index = +this.regions[i].properties.id;
map.set(index, this.regions[i]);
}
this.regionMap = map;
return this;
}
/**
* Updates the number of all transitions that are connected to a region,
* for all regions. Updates region objects directly.
*
* CONFIG.filter.transitionCountMode determines the mode:
* only get transitions to region, from region, or both.
*/
updateRegions() {
this.regions.forEach(r => {
r.properties.transitionNumber = 0;
r.outTransitions = [];
r.incTransitions = [];
});
this.transitions.forEach(tr => {
// go through all selected transitions and update region.transitionNumber attributes
// skip in or outgoing transitions dependent on CONFIG
let s;
if (CONFIG.filter.transitionCountMode !== "in") {
s = tr.source;
s && s.properties.transitionNumber++;
}
if (CONFIG.filter.transitionCountMode !== "out") {
const t = tr.target;
t && t !== s && t.properties.transitionNumber++;
}
});
this.reducedTransitions.forEach(tr => {
tr.source.outTransitions.push(tr);
tr.target.incTransitions.push(tr);
});
// set min max and average values
let min = Infinity;
let max = 0;
let sum = 0;
let num = 0;
this.regions.forEach(item => {
if (item.properties.transitionNumber > max) {
max = item.properties.transitionNumber;
}
if (item.properties.transitionNumber < min) {
min = item.properties.transitionNumber;
}
sum += item.properties.transitionNumber;
num++;
});
this.transitionsPerRegion = {
max: max,
min: min,
avg: num > 0 ? sum / num : 0
};
return this;
}
/**
* Updates the timeline data (this.states) when states are shown.
*
* this.states is an array that consists of objects {<string> date, <Array> amount}.
*/
updateTimelineData() {
if (!this.model.additionalData || !this.model.additionalData.allIterations) {
return;
}
// states are saved incrementally, so we need to keep the last known value
const nodesState = [];
const lastKnownState = [];
const nodes = this.model.regions.length;
const beginDate = this.model.timeRange[0];
this.model.additionalData.allIterations.forEach((iter, i) => {
//TODO: quick hack for cnr cognitive opinion dynamic
const n = !this.model.additionalData.infectionModelDiscrete && !this.model.additionalData.infectionModel ? [0, 0] : [0, 0, 0];
const iteration = iter.status;
// sum over all nodes
this.regions.forEach(region => {
// get state
const j = region.properties.id;
let state = iteration[j];
if (state === null || state === undefined) {
lastKnownState[j] = lastKnownState[j] || 0;
state = lastKnownState[j];
} else {
lastKnownState[j] = state;
}
// add state to sum
if (!this.model.additionalData.infectionModelDiscrete) {
// for continuous models like cognitive opinion dynamics,
// values just added up
n[1] += state;
} else {
// models with concrete integer values are sorted into arrays
n[state]++;
}
});
nodesState[i] = {
amount: n,
// set a pseudo date
date: lib.addDays(beginDate, i)
};
});
this.timeStates = nodesState;
}
/**
* Reduces transitions into a single transition per source-target pair.
*
* Reduced transitions are sorted by weight descending to allow easy splicing,
* if we want to limit the number of drawn transitions.
*
* If directed is true (default is false), edges are considered different if
* source and target are switched.
*
* this.model.properties.directedTransitions specifies if the network is
* directed or not. In case of directed transitions, they are not aggregated
* if they share the same regions but differ in direction.
* For undirected transitions only the regions are considered, is does not
* matter which one is the source region.
*/
reduceTransitions() {
const directed = this.model.properties.directedTransitions;
const transitions = this.transitions;
const map = new Map();
transitions.forEach(t => {
const sid = t.source.properties.id;
const tid = t.target.properties.id;
let key = `${sid} ${tid}`;
let contained = map.has(key);
// if transitions are undirected, also try the opposite pair of regions
if (!contained && !directed) {
key = `${tid} ${sid}`;
contained = map.has(key);
}
// if the pair of regions is not yet contained,
// create a new map item
if (!contained) {
// reset references to reduced transitions
t.transitions = [t];
// reset weight of inserted nodes
t.weight = t.oWeight;
map.set(key, t);
return;
} else {
// else update the existing item
const item = map.get(key);
// store reference to a transitions that are reduced into this one
item.transitions.push(t);
// update weight using original weights
item.weight += t.oWeight;
}
});
// write map to array and find min and max values
let maxWeightTemp = 0.0;
let minWeightTemp = Number.MAX_VALUE;
const result = [];
let sum = 0;
for (const value of map.values()) {
value.highlight = {
h: false
};
result.push(value);
// get max weight
const weight = +value.weight;
if (weight < minWeightTemp) {
minWeightTemp = weight;
}
if (weight > maxWeightTemp) {
maxWeightTemp = weight;
}
sum += weight;
};
// give filter min and max values
this.transitionWeights = {
min: minWeightTemp,
max: maxWeightTemp,
avg: sum / result.length
};
// sort results descending
return result.sort((a, b) => b.weight - a.weight);
}
/**
* Clears the previous highlight states and sets the highlight
* flag on the primary target.
*
* @param item
* a transition or region object from the current filter, the primary highlight target
*/
startHighlight(item) {
this.highlightMode = true;
this.clearHighlight();
item.highlight.h = true;
}
/**
* Clears the highlight flag on all currently highlighted elements
*/
clearHighlight() {
for (const x in this.highlighted) {
this.highlighted[x].forEach(d => d.highlight.h = false);
}
};
}