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
|
const fs = require('fs')
const parse = require('csv-parse')
const stringify = require('csv-stringify')
const parklandInput = fs.createReadStream('./data/media_01_parkland_shooting.csv')
const averageInput = fs.createReadStream('./data/media_02_average_of_seven_shootings.csv')
const parklandParser = parse()
const averageParser = parse()
const stringifier = stringify()
const output = fs.createWriteStream('./data/media.csv')
let finishCount = 0
let parkland = [], average = []
function finish() {
finishCount += 1
console.log(finishCount)
if (finishCount < 2) { return }
console.log('finishing')
let a, b, nn
for (let n = 0; n < 12 * 8; n += 1) {
nn = n / 8
a = find(nn, parkland)
b = find(nn, average)
stringifier.write([n, a, b])
}
stringifier.end()
}
/*
Given the sequence A of pairs (i,j) describing coordinates and an offset n,
find i where A[i][0] <= n and n < A[i+1][0]
and use these to interpolate the value of A(n) -> j or whatever you call it.
*/
function find(n, a) {
let head, tail, dist
for (let i = 1, len = a.length; i < len; i++) {
if (a[i][0] > n) {
head = a[i-1]
tail = a[i]
dist = (n - head[0]) / (tail[0] - head[0])
console.log(n, head, tail)
return lerp(dist, head[1], tail[1])
}
}
}
function lerp(n, a, b) {
return (b-a) * n + a
}
parklandInput.on('readable', function() {
let buf
while ((buf = parklandInput.read()) !== null) {
parklandParser.write(buf)
}
})
parklandInput.on('error', function(err){
console.error('parklandInput error', err.message)
})
parklandInput.on('finish', function(){
parklandParser.end()
})
averageInput.on('readable', function() {
let buf
while ((buf = averageInput.read()) !== null) {
averageParser.write(buf)
}
})
averageInput.on('error', function(err){
console.error('averageInput error', err.message)
})
averageInput.on('finish', function(){
console.log('enddd')
averageParser.end()
})
parklandParser.on('readable', function(){
let row
while (row = parklandParser.read()) {
parkland.push(row.map(n => parseFloat(n)))
}
// console.log('p', parkland.length)
if (parkland.length == 108) finish()
})
parklandParser.on('error', function(err){
console.error('parklandParser error', err.message)
})
parklandParser.on('end', function(){
finish()
})
averageParser.on('readable', function(){
let row
while (row = averageParser.read()) {
average.push(row.map(n => parseFloat(n)))
}
if (average.length == 89) finish()
})
averageParser.on('error', function(err){
console.error('averageParser error', err.message)
})
averageParser.on('end', function(){
finish()
})
stringifier.on('readable', function(){
let row
while (row = stringifier.read()){
output.write(row)
}
})
stringifier.on('error', function(err){
console.error('stringifier error', err.message)
})
stringifier.on('finish', function(){
output.end()
})
|