forked from monsooncollective/yatayat
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataquality.js
299 lines (268 loc) · 10.5 KB
/
dataquality.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
// Data Quality: headless sanity checks for Yatayat System data.
var DQ = DQ || {};
var _ = _ || require("underscore");
// nearestStops returns squared-distance;
// regardless, this is a magic number.
var SAME_STOP_DIST = Math.pow(0.0005,2);
DQ.sanityChecks = {
// name of test -> {
// run: function(route, [system]),
// print: function(run_output, route, system)
// }
"nearby different stops": {
run: function(route, system) {
// Returns: stop1.id -> stop2, when stop1 and stop2 are nearby
// and different
stopClosest = {};
route.stops.forEach(function(stop) {
var stops = system.nearestStops([stop.lat, stop.lng], 2, SAME_STOP_DIST)
.filter(function(s) { return s.id !== stop.id; });
if(stops.length > 0 && stop.name && stops[0].name && stops[0].name !== stop.name) {
stopClosest[stop.id] = stops[0];
}
});
return stopClosest;
},
print: function(run_output, route, system) {
var pairs = [];
for(var stopid in run_output) {
pairs.push([route.stopDict[stopid], run_output[stopid]]);
}
if(pairs.length > 0) {
var out = "" + pairs.length + " nearby different stops:\n";
pairs.forEach(function(pair) {
out += " " + pair[0].name + "(" + pair[0].id + ") - " + pair[1].name + "(" + pair[1].id + ")\n";
});
out += "\n";
return out;
}
},
kind: 'WARNING'
},
"first segment doesn't end at a stop": {
run: function(route) {
if(route.stops.length === 0 || route.segments.length === 0) {
return true;
}
var firstStop = route.stops[0];
var firstSegmentEnd = route.segments[0].listOfLatLng[0];
if (!firstStop) return true;
return firstStop.lat == firstSegmentEnd[0] && firstStop.lng == firstSegmentEnd[1];
},
print: function(run_output, route) {
debugger;
if (run_output) {
if(route.stops.length === 0 || route.segments.length === 0) {
return "Without stops or segments, it's silly to talk about whether the first segment will end at a stop.\n\n";
}
return "First segment of route (id:" + route.segments[0].id +
") doesn't start at a properly key-ed stop.\n";
}
},
kind: 'WARNING'
},
"unnamed stops": {
run: function(route) {
// Returns: stop.id -> true, when stop is unnamed
unnamedStops = {};
route.stops.forEach(function(stop) {
if(!stop.name) {
unnamedStops[stop.id] = true;
}
});
return unnamedStops;
},
print: function(run_output, route, system) {
var stops = [];
for(var stopid in run_output) {
stops.push(route.stopDict[stopid]);
}
if(stops.length > 0) {
var out = "" + stops.length + " unnamed stops:\n";
stops.forEach(function(stop) {
out += " " + stop.id + "\n";
});
out += "\n";
return out;
}
},
kind: 'WARNING'
},
"no terminus": {
run: function(route) {
// Returns true if route has no terminus, false otherwise
return false || route._noTerminus;
},
print: function(run_output, route, system) {
if(run_output) {
return "No terminus\n\n";
}
},
kind: 'ERROR'
},
"unconnected segments": {
run: function(route) {
// Returns seg.id -> true, when Segment is unconnected
unconnected = {};
route._unconnectedSegments.forEach(function(seg) {
unconnected[seg.id] = true;
});
return unconnected;
},
print: function(run_output, route, system) {
var segs = [];
for(var segid in run_output) {
segs.push(segid);
}
if(segs.length > 0) {
var out = "" + segs.length + " unconnected segments:\n";
segs.forEach(function(segid) {
out += " " + segid + "\n";
});
out += "\n";
return out;
}
},
kind: 'ERROR'
},
"similar names": {
run: function(route, system) {
// Returns stop1.id -> stop2, when stop1 and stop2 have
// similar names.
// thanks! http://thinkphp.ro/apps/js-hacks/String.levenshtein/String.levenshtein.html
var levenshtein = function(stringa, stringb) {
var cost = new Array(),
str1 = stringa,
str2 = stringb,
n = str1.length,
m = str2.length,
i, j;
var minimum = function(a,b,c) {
var min = a;
if(b < min) {
min = b;
}
if(c < min) {
min = c;
}
return min;
}
if(n == 0 || m == 0) {
return;
}
for(var i=0;i<=n;i++) {
cost[i] = new Array();
}
for(i=0;i<=n;i++) {
cost[i][0] = i;
}
for(j=0;j<=m;j++) {
cost[0][j] = j;
}
for(i=1;i<=n;i++) {
var x = str1.charAt(i-1);
for(j=1;j<=m;j++) {
var y = str2.charAt(j-1);
if(x == y) {
cost[i][j] = cost[i-1][j-1];
} else {
cost[i][j] = 1 + minimum(cost[i-1][j-1], cost[i][j-1], cost[i-1][j]);
}
}
}
return cost[n][m];
};
var preprocess = function(str) {
return str.toLowerCase().replace(' ', '');
};
similarNames = {};
// n^2 algorithm that compares string-distance between every two pairs of stop
route.stops.forEach(function(stop) {
system.routes.forEach(function(route2) {
route.stops.forEach(function(stop2) {
if(stop.name && stop2.name && levenshtein(stop.name, stop2.name) < 3) {
var physicalDistance = Math.pow(stop.lat-stop2.lat,2) + Math.pow(stop.lng-stop2.lng,2);
if(physicalDistance > SAME_STOP_DIST) {
// console.log(stop.name, stop2.name, physicalDistance);
similarNames[stop.id] = stop2;
}
else if(stop.id != stop2.id) {
//console.log(stop.name, stop2.name, 'nearby');
}
}
});
});
});
return similarNames;
},
print: function(run_output, route, system) {
// XXX: *very* similar code to nearby different stops
var pairs = [];
for(var stopid in run_output) {
pairs.push([route.stopDict[stopid], run_output[stopid]]);
}
if(pairs.length > 0) {
var out = "" + pairs.length + " stops with similar names:\n";
pairs.forEach(function(pair) {
out += " " + pair[0].name + "(" + pair[0].id + ") - " + pair[1].name + "(" + pair[1].id + ")\n";
});
out += "\n";
return out;
}
},
kind: 'WARNING'
}
};
// For easier integration with data_quality.html, make aliases
DQ.nearbyDifferentStops = DQ.sanityChecks["nearby different stops"].run;
DQ.unnamedStops = DQ.sanityChecks["unnamed stops"].run;
DQ.similarNames = DQ.sanityChecks["similar names"].run;
DQ.noTerminus = DQ.sanityChecks["no terminus"].run;
DQ.unconnectedSegments = DQ.sanityChecks["unconnected segments"].run;
DQ.errorString = function(system, which) {
var which = which || 'ERROR'; // possible options: 'ERROR', 'WARNING'
var errForRoute = function(route) {
var errs = DQ.findErrorsAndWarnings(route, system)[which];
var prequel = "\n### Route: " + route.name + " (" + route.id + ")\n\n" +
"http://yatayat.monsooncollective.org/data_quality.html#" + route.id + "\n\n";
return errs ? prequel + errs : "";
};
return _.chain(system.routes)
.map(errForRoute)
.reduce(function(a, b) { return a+b; }, "")
.value();
};
DQ.findErrorsAndWarnings = function(route, system) {
var retval = {};
//TODO: If route is undefined
_(DQ.sanityChecks).each(function(v, testname) {
var test = DQ.sanityChecks[testname];
var res = test.run(route, system);
var errout = test.print(res, route, system);
if(errout) {
if(!(test.kind in retval)) // warning or error not added yet
retval[test.kind] = [errout];
else
retval[test.kind].push(errout);
}
});
return retval;
};
DQ.findAllErrorsAndWarnings = function(system) {
return system.routes.map(function(r) { return DQ.findErrorsAndWarnings(r, system); });
};
DQ.findCorrectRoutes = function(system) {
return system.routes.filter(function(route) {
for(var testname in DQ.sanityChecks) {
var res = DQ.sanityChecks[testname].run(route, system);
var errout = DQ.sanityChecks[testname].print(res, route, system);
if(errout) {
return false;
}
}
return true;
});
};
// export as a node module
var module = module || {};
module.exports = DQ;