-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2json.js
113 lines (88 loc) · 3.18 KB
/
csv2json.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
/*function parse(allText) {
var allCategories = allText.split('|');
var database = [];
for (var i = 0; i < allCategories.length; i++) {
var allLines = allCategories[i].split(/\r\n|\n/);
var lines = [];
for (var j = 0; j < allLines.length; j++) {
var allBits = allLines[j].split(',');
var bits = [];
for (var k = 0; k < allBits.length; k++) {
bits.push(allBits[k]);
}
lines.push(bits);
}
database.push(lines);
}
database.pop();
return database;
}
*/
function printo(database) {
for (category in database) {
console.log(category+":");
var catKeys = Object.keys(database[category]);
for (var j = 0; j < catKeys.length; j++) {
console.log(catKeys[j]+","+database[category][catKeys[j]].toString());
}
console.log("--");
}
}
function filtercut(past,filter) {
var database = {};
for (category in past) {
var lines = {};
var pastKeys = Object.keys(past[category]);
outerloop : for (var i = 0; i < pastKeys.length; i++) {
if (pastKeys[i] > filter.cutoff) {
break outerloop;
}
lines[pastKeys[i]] = past[category][pastKeys[i]];
}
database[category] = lines;
}
return database;
}
function generate(init,past,filter) {
var database = filtercut(past,filter);
var datekeys = Object.keys(init.dates);
for (var i = 0; i < datekeys.length; i++) {
var total = 0;
for (category in init.categories) {
if (datekeys[i] > filter.cutoff && datekeys[i] >= init.categories[category][0]) {
try {
var basekeys = Object.keys(database[category]);
var prevdate = basekeys[basekeys.length-1];
var prevstrength = database[category][basekeys[basekeys.length-1]][1];
}
catch {
database[category] = {};
var prevdate = Number.MIN_SAFE_INTEGER;
var prevstrength = 0;
}
//calculate c
database[category][datekeys[i]] = [(1-Math.exp((prevdate-datekeys[i])/(1+prevstrength)))*init.categories[category][1],prevstrength];
total += database[category][datekeys[i]][0];
}
}
if (total > 0) {
for (category in init.categories) {
console.log(category);
if (database[category][datekeys[i]] != null) {
var basekeys = Object.keys(database[category]);
var prevdate = basekeys.length > 1 ? basekeys[basekeys.length-2] : Number.MIN_SAFE_INTEGER;
var altdate = basekeys.length > 1 ? basekeys[basekeys.length-2] : datekeys[i];
var prevstrength = basekeys.length > 1 ? database[category][basekeys[basekeys.length-2]][1] : 0;
//calculate a
database[category][datekeys[i]][0] *= init.dates[datekeys[i]]/total;
//calculate m
database[category][datekeys[i]][1] += filter.rise*database[category][datekeys[i]][0]*(1-Math.exp((prevdate-datekeys[i])/(1+prevstrength))) - filter.decay*(datekeys[i]-altdate);
if (database[category][datekeys[i]][1] < 0 ) {
database[category][datekeys[i]][1] = 0;
}
}
}
}
}
return database;
}