diff options
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/grep.js | 6 | ||||
| -rw-r--r-- | bin/media.js | 117 |
2 files changed, 120 insertions, 3 deletions
diff --git a/bin/grep.js b/bin/grep.js index b4eb3bd..ee85ff3 100644 --- a/bin/grep.js +++ b/bin/grep.js @@ -75,14 +75,14 @@ const fields = ("incident_id,date,state,city_or_county," return a }, {}) -const search = 'good-samaritans' -const regexp = new RegExp(search, 'i') +const search = 'school-shootings' +// const regexp = new RegExp(search, 'i') function test(row){ return ( row // && row[fields.incident_characteristics].indexOf('Non-Shooting Incident') === -1 - && row[fields.incident_characteristics].indexOf('Samaritan') !== -1 + && row[fields.incident_characteristics].indexOf('School Shooting') !== -1 // // && row[fields.participant_age_group].indexOf('Teen') !== -1 // && row[fields.participant_age_group].indexOf('Child') !== -1 // && row[fields.gun_type].indexOf('AR-15') !== -1 diff --git a/bin/media.js b/bin/media.js new file mode 100644 index 0000000..b8742ac --- /dev/null +++ b/bin/media.js @@ -0,0 +1,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() +}) |
