-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcache.js
142 lines (128 loc) · 4.39 KB
/
cache.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
// Copyright (c) 2021, NVIDIA CORPORATION.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const fs = require('fs/promises');
const {DataFrame, Series, Uint32, Int16, Int8, Float32} = require('@rapidsai/cudf');
const {Field, Vector, List} = require('apache-arrow');
const path = require('path');
module.exports =
() => {
let timeout = null;
let datasets =
{uber: null, mortgage: null}
// let uberTracts = null;
function
clearCachedGPUData() {
datasets.uber = null;
datasets.mortgage = null;
}
return async function loadDataMiddleware(datasetName, req, res, next) {
if (timeout) { clearTimeout(timeout); }
// Set a 10-minute debounce to release server GPU memory
timeout = setTimeout(clearCachedGPUData, 10 * 60 * 1000);
req[datasetName] =
datasets[datasetName] || (datasets[datasetName] = await readDataset(datasets, datasetName));
next();
}
}
async function readDataset(datasets, datasetName) {
if (datasetName == 'uber') {
// clear mortgage dataset from mem
datasets.mortgage = null;
return readUberTrips();
}
if (datasetName == 'mortgage') {
// clear uber dataset from mem
datasets.uber = null;
return readMortgageData();
}
}
async function
readUberTrips() {
const trips = DataFrame.readCSV({
header: 0,
sourceType: 'files',
sources: [path.resolve('./public', 'data/san_fran_uber.csv')],
dataTypes: {
sourceid: new Int16,
dstid: new Int16,
month: new Int8,
day: new Int8,
start_hour: new Int8,
end_hour: new Int8,
travel_time: new Float32
}
});
return new DataFrame({
// TODO: do we want to add our own indices?
// id: Series.sequence({ type: new Uint32, init: 0, size: trips.numRows }),
sourceid: trips.get('sourceid'),
dstid: trips.get('dstid'),
month: trips.get('month'),
day: trips.get('day'),
start_hour: trips.get('start_hour'),
end_hour: trips.get('end_hour'),
travel_time: trips.get('travel_time'),
});
}
async function
readMortgageData() {
const mortgage = DataFrame.readCSV({
header: 0,
sourceType: 'files',
sources: [path.resolve('./public', 'data/mortgage.csv')],
dataTypes: {
index: new Int16,
zip: new Uint32,
dti: new Float32,
current_actual_upb: new Float32,
borrower_credit_score: new Int16,
load_id: new Uint32,
delinquency_12_prediction: new Float32,
seller_name: new Int16
}
});
return new DataFrame({
// TODO: do we want to add our own indices?
// id: Series.sequence({ type: new Uint32, init: 0, size: trips.numRows }),
zip: mortgage.get('zip'),
dti: mortgage.get('dti'),
current_actual_upb: mortgage.get('current_actual_upb'),
borrower_credit_score: mortgage.get('borrower_credit_score'),
load_id: mortgage.get('load_id'),
delinquency_12_prediction: mortgage.get('delinquency_12_prediction'),
seller_name: mortgage.get('seller_name'),
});
}
async function
readUberTracts() {
const {features} = JSON.parse(
await fs.readFile('public/data/san_francisco_censustracts.geojson', {encoding: 'utf8'}));
const polygons = features.filter((f) => f.geometry.type === 'MultiPolygon')
.reduce((x, {geometry}) => x.concat(geometry.coordinates), []);
return new DataFrame({
id: Series.sequence({type: new Uint32, init: 0, size: polygons.length}),
polygons: Series.new(featureToVector(polygons))
});
function featureToVector(coordinates) {
return Vector.from({
values: coordinates,
highWaterMark: Number.POSITIVE_INFINITY,
type: new List(Field.new({
name: 'rings',
type: new List(Field.new(
{name: 'coords', type: new List(Field.new({name: 'points', type: new Float32()}))}))
})),
});
}
}