Skip to content

Commit 5c98e02

Browse files
committed
Backing up mongodb test data db, making multiple fixes to keypress learner, adding running commentary and producing detection data json file.
1 parent 7a361e3 commit 5c98e02

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
#Block IntelliJ's project folder
22
.idea
3+
#Ignore db-dump (probably shouldn't have that on GitHub)
4+
db-dump
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"downUpRange":"[0,0]","oBetaPrePress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.1256914830569554,"weights":{"0":-0.06358266426250339,"1":-0.009532214697029797,"2":0.13106300167711632,"3":0.08863383905026659,"4":0.07553907567606725}},"1":{"bias":0.21309025755029123,"weights":{"0":-0.08871578462421895,"1":-0.165580867856356,"2":-0.1496587090499989,"3":0.15831075156823726,"4":0.0010332044369733974}},"2":{"bias":0.3033092508277044,"weights":{"0":-0.14283886114135386,"1":-0.1598442644181718,"2":0.1723971067997329,"3":0.056598242686826526,"4":0.2002734589879477}}},{"keypress":{"bias":1.4516290619149166,"weights":{"0":0.9220337238228323,"1":0.7694263896236155,"2":0.9840821743619522}}}],"outputLookup":true,"inputLookup":false},"oBetaPostPress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.055917392641217735,"weights":{"0":-0.06874729366973042,"1":0.0505662785360404,"2":0.03937816601083393,"3":0.10330143181566885,"4":0.0806595615573737}},"1":{"bias":0.29204936200022047,"weights":{"0":0.029510296881198883,"1":-0.12558105142663795,"2":0.17349022098677797,"3":-0.13585084317829335,"4":-0.1807786492051441}},"2":{"bias":0.1059359074337274,"weights":{"0":-0.1995158834382892,"1":-0.0385570577320713,"2":0.15107839328794587,"3":-0.2037769963940173,"4":0.17210478387880396}}},{"keypress":{"bias":1.4505154022291424,"weights":{"0":0.7326406724975131,"1":0.863046735431264,"2":1.0067905029186066}}}],"outputLookup":true,"inputLookup":false},"oGammaPrePress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.16151020142813946,"weights":{"0":-0.07049975991249086,"1":-0.09021790808773669,"2":0.13173741893288987,"3":0.0006780008038537575,"4":-0.12672522638923078}},"1":{"bias":-0.012581007233702762,"weights":{"0":0.04117718739435078,"1":0.16339184778845428,"2":0.0070890632060140535,"3":0.1517638683640279,"4":0.10104397535924912}},"2":{"bias":0.17917370435550545,"weights":{"0":0.03450935026630758,"1":-0.13206392237387585,"2":-0.046457173018761996,"3":-0.01782414434658423,"4":0.18410482762520658}}},{"keypress":{"bias":1.595328520936466,"weights":{"0":0.8865454388100987,"1":0.7735254059406078,"2":0.8172296495399652}}}],"outputLookup":true,"inputLookup":false},"oGammaPostPress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.048589511941195984,"weights":{"0":0.12622114932164552,"1":-0.07656021899400745,"2":-0.19534879101313077,"3":0.0801013352672635,"4":0.13880616673712018}},"1":{"bias":0.36899119686329074,"weights":{"0":0.028285488486289978,"1":0.18993392575638687,"2":0.03548205603831068,"3":-0.012268746332317278,"4":0.13772216352873362}},"2":{"bias":0.04446693168264923,"weights":{"0":-0.0005559963174164295,"1":0.11435359504137624,"2":-0.10184560500528066,"3":-0.04169708686211824,"4":0.009171560548342759}}},{"keypress":{"bias":1.5192220643260217,"weights":{"0":0.8279261349363487,"1":0.9824561253676277,"2":0.6582095488177209}}}],"outputLookup":true,"inputLookup":false},"aZPrePress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.1375655056844021,"weights":{"0":-0.02129532173275947,"1":-0.004958287605773014,"2":-0.04319891898451145,"3":-0.09092423957197915,"4":0.2663988377676829}},"1":{"bias":0.3397177815275383,"weights":{"0":-0.19841566365212204,"1":-0.012222357969204059,"2":-0.18302680481542546,"3":0.24565321969852563,"4":-0.041687375897298005}},"2":{"bias":0.35325399673084007,"weights":{"0":-0.1399271983653307,"1":-0.08628228306392242,"2":0.07589382470872612,"3":0.15574709272173903,"4":-0.013745613860532954}}},{"keypress":{"bias":1.3379146551266912,"weights":{"0":0.8428534712654087,"1":0.9770759728218615,"2":0.9505539587447983}}}],"outputLookup":true,"inputLookup":false},"aZPostPress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":-0.05017425499301192,"weights":{"0":0.010821026936173428,"1":-0.025416510420785753,"2":0.0568622934318785,"3":0.1499016917978516,"4":0.12715469190961431}},"1":{"bias":-0.053507257730120474,"weights":{"0":-0.09439805382862687,"1":0.023208990046823166,"2":0.1662473080111419,"3":0.022855126105643206,"4":-0.044068012323224964}},"2":{"bias":0.30811975879193926,"weights":{"0":-0.0048634494654834215,"1":-0.09889362514585875,"2":0.15016433869706114,"3":-0.17106333851722272,"4":-0.031520984405790396}}},{"keypress":{"bias":1.5945612070213433,"weights":{"0":0.6705356037740744,"1":0.6353494116520215,"2":1.0445199039328756}}}],"outputLookup":true,"inputLookup":false},"aYPrePress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.22308718305167347,"weights":{"0":0.10199671527370813,"1":-0.08460493262831216,"2":-0.15355698494353742,"3":0.012871502078054865,"4":-0.004471681078860413}},"1":{"bias":0.1632989729252583,"weights":{"0":0.199649921618402,"1":0.06775324396285395,"2":0.1937803407759596,"3":0.14036822999658463,"4":0.07448342929868289}},"2":{"bias":0.0025967994501912865,"weights":{"0":-0.10062684835866094,"1":0.09202338550862019,"2":0.15428275770695568,"3":-0.07355404668037994,"4":-0.003674088259723404}}},{"keypress":{"bias":1.5724813562254762,"weights":{"0":0.8097502381391755,"1":0.9494134392570582,"2":0.7559047651376564}}}],"outputLookup":true,"inputLookup":false},"aYPostPress":{"layers":[{"0":{},"1":{},"2":{},"3":{},"4":{}},{"0":{"bias":0.1984649672392398,"weights":{"0":0.07294764146208765,"1":-0.06252032485701904,"2":0.12063376873240063,"3":-0.10747600326081366,"4":0.07218250267290585}},"1":{"bias":0.31164134587684866,"weights":{"0":-0.1659734234213829,"1":-0.09100685460314975,"2":-0.08223246010226329,"3":0.08813151591694292,"4":0.11939542016604913}},"2":{"bias":-0.09641908227608381,"weights":{"0":-0.14202064080163837,"1":-0.14535417621850133,"2":0.07574526770988664,"3":-0.09342784957773051,"4":0.012274710965628691}}},{"keypress":{"bias":1.58957102070062,"weights":{"0":0.7777596520734343,"1":0.925642261591671,"2":0.6304209521316956}}}],"outputLookup":true,"inputLookup":false}}

keypress-detector/learner.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//Shim for Promises
88
if (typeof Promise == "undefined") {Promise = require("promise");}
99

10+
var fs = require('fs'); // FileSystem
1011
var save = require('save'); //Database interface
1112
var saveMongodb = require('save-mongodb');
1213
var Db = require('mongodb').Db; //MongoDB Database object
@@ -29,15 +30,11 @@ var networks = {oBeta: {
2930
var downUpDiff = []; // Min and Max difference between keydown and keyup events.
3031
var promExtractor = function(resolve, reject) {this.resolve = resolve; this.reject = reject;};
3132

32-
var subjSaveDb = new this.Db('TestSubjects', new this.DbServer('localhost', 27017, {})) //Initialisation of the TestSubjects database
33+
var subjSaveDb = new Db('TestSubjects', new DbServer('localhost', 27017, {})) //Initialisation of the TestSubjects database
3334
, subjCol; //Initialising db interface object
34-
var resuSaveDb = new this.Db('keyPressSystem', new this.DbServer('localhost', 27017, {})) //Initialisation of the keyPressSystem database
35-
, resuCol; //Initialising db interface object
3635

3736
var testProm = {};
3837
var testDone = new Promise(promExtractor.bind(testProm));
39-
var resuProm = {};
40-
var resuDone = new Promise(promExtractor.bind(resuProm));
4138

4239
subjSaveDb.open(function (error, connection) {
4340
if (error) {
@@ -56,24 +53,7 @@ subjSaveDb.open(function (error, connection) {
5653
});
5754
});
5855

59-
resuSaveDb.open(function (error, connection) {
60-
if (error) {
61-
resuProm.reject("Error opening keyPressSystem database! " + JSON.stringify(error.errors));
62-
return false;
63-
}
64-
65-
connection.collection('data', function (error, collection) {
66-
if (error) {
67-
resuProm.reject("Error choosing keyPressSystem data collection! " + JSON.stringify(error.errors));
68-
return false;
69-
}
70-
console.log("Chosen keyPressSystem data collection, successful connection...");
71-
resuCol = save('keyPressData', {engine: saveMongodb(collection)});
72-
resuProm.resolve();
73-
});
74-
});
75-
76-
Promise.all([testDone, resuDone]).then(finishSetup, function() {
56+
Promise.all([testDone]).then(finishSetup, function() {
7757
console.log("ERROR: An error occurred opening the databases.");
7858
throw new Error("An error occurred opening the databases.");
7959
});
@@ -82,39 +62,38 @@ function finishSetup() {
8262
subjCol.count({}, function(cnterr, data) {
8363
if (cnterr) {return new Error("There was an error counting the data items.");}
8464
if (data > currentRecord) {
85-
subjCol.find().skip(currentRecord).limit(1, function(findErr, data) {
65+
subjCol.find({}, function(findErr, data) {
8666
if (findErr) {return new Error("There was an error choosing a data item.");}
8767
try {
88-
var finalData = JSON.parse(data.data);
68+
console.log("Attempting to select record for learning. Trying record: "+currentRecord);
69+
var finalData = data[currentRecord].data;
70+
console.log("Selection and parsing successful. Sending for prep.");
8971
currentRecord++;
9072
learn(finalData);
9173
} catch (e) {
9274
return new Error("There was an error parsing the test data.");
9375
}
9476
});
9577
} else {
96-
saveValToDb("downUpRange", JSON.stringify(downUpDiff));
97-
saveValToDb("oBetaPrePress", networks.oBeta.pre.toJSON());
98-
saveValToDb("oBetaPostPress", networks.oBeta.post.toJSON());
99-
saveValToDb("oGammaPrePress", networks.oGamma.pre.toJSON());
100-
saveValToDb("oGammaPostPress", networks.oGamma.post.toJSON());
101-
saveValToDb("aZPrePress", networks.aZ.pre.toJSON());
102-
saveValToDb("aZPostPress", networks.aZ.post.toJSON());
103-
saveValToDb("aYPrePress", networks.aY.pre.toJSON());
104-
saveValToDb("aYPostPress", networks.aY.post.toJSON());
105-
}
106-
});
107-
}
108-
109-
function saveValToDb(key, data) {
110-
resuCol.findOne({"key": key}, function(findErr, foundObj) {
111-
if (findErr) {
112-
resuCol.create({"key": key, "data": data}, function(createErr, newObj) {
113-
if (createErr) {return new Error("There was an error creating the "+key+" value in the db.");}
114-
});
115-
} else {
116-
resuCol.update({"key": key, "data": data}, function(updateErr, updatedObj) {
117-
if (updateErr) {return new Error("There was an error updating the "+key+" value in the db.");}
78+
var dataObj = {
79+
downUpRange: JSON.stringify(downUpDiff),
80+
oBetaPrePress: networks.oBeta.pre.toJSON(),
81+
oBetaPostPress: networks.oBeta.post.toJSON(),
82+
oGammaPrePress: networks.oGamma.pre.toJSON(),
83+
oGammaPostPress: networks.oGamma.post.toJSON(),
84+
aZPrePress: networks.aZ.pre.toJSON(),
85+
aZPostPress: networks.aZ.post.toJSON(),
86+
aYPrePress: networks.aY.pre.toJSON(),
87+
aYPostPress: networks.aY.post.toJSON()
88+
};
89+
fs.writeFile("./keypress_detection_data.json", JSON.stringify(dataObj), function(err) {
90+
if (err) {
91+
console.log("An issue occurred saving the detection data.");
92+
} else {
93+
console.log("Detection data saved.");
94+
}
95+
console.log("Finished learning!");
96+
process.exit(0);
11897
});
11998
}
12099
});
@@ -151,9 +130,12 @@ function arrToBrainFormat(outputVal, val) {
151130
}
152131

153132
function learn(dataArr) {
154-
var stream = require("./clouded-sky.js")(dataArr);
133+
console.log("Beginning learning...");
134+
var stream = require("./clouded-sky.js").init(dataArr);
155135
var datapoint;
156136
var exit = false;
137+
var hasPress = false;
138+
var firstResponseThreshold = 1250; //Wait a second and a quarter before assuming a dud dataset.
157139
var trailLen = 4;
158140
var trailCountDown;
159141
var trailAcceptanceThreshold = 0.001; // The smallest range that should be exhibited in a trail for it to be included.
@@ -169,13 +151,22 @@ function learn(dataArr) {
169151
// prePress - Contains arrays of [trailLen] pings before each keydown (keydown is included too, making [trailLen+1] values)
170152
// postPress - Contains arrays of [trailLen] pings after each keyup (keyup is included too, making [trailLen+1] values)
171153
var downTime;
172-
while(datapoint = stream.pick(0) && !exit) {
154+
var count = 0;
155+
var strmLen = stream.store.length;
156+
while((datapoint = stream.pick(0)[0]) && !exit) {
157+
count++;
158+
if (Boolean(process.stdout.isTTY)) {
159+
process.stdout.write("Prepping datapoint "+count+" of "+strmLen+": Type: "+datapoint.data.event);
160+
} else {
161+
console.log("Prepping datapoint "+count+" of "+strmLen+": Type: "+datapoint.data.event);
162+
}
173163
switch(datapoint.data.event) {
174164
case "ping":
175-
if (!datapoint.data.datapoints.orientation.x || !datapoint.data.datapoints.orientation.y
176-
|| !datapoint.data.datapoints.acceleration.z || !datapoint.data.datapoints.acceleration.y) {
165+
if (datapoint.time > firstResponseThreshold && (!datapoint.data.datapoints.orientation.x || !datapoint.data.datapoints.orientation.y
166+
|| !datapoint.data.datapoints.acceleration.z || !datapoint.data.datapoints.acceleration.y)) {
177167
exit = true;
178-
} else {
168+
} else if ((datapoint.data.datapoints.orientation.x && datapoint.data.datapoints.orientation.y
169+
&& datapoint.data.datapoints.acceleration.z && datapoint.data.datapoints.acceleration.y)) {
179170
oBeta.pings.push(datapoint.data.datapoints.orientation.x);
180171
oGamma.pings.push(datapoint.data.datapoints.orientation.y);
181172
aZ.pings.push(datapoint.data.datapoints.acceleration.z);
@@ -200,6 +191,7 @@ function learn(dataArr) {
200191
break;
201192
case "keydown":
202193
if (oBeta.pings.length >= 6) {
194+
hasPress = true;
203195
//Only add data if it has a change above the trail acceptance threshold.
204196
var oBetaTrail = normaliseArrData(oBeta.pings.slice(oBeta.pings.length-trailLen, oBeta.pings.length).concat(datapoint.data.datapoints.orientation.x), ranges.oBeta);
205197
if (findRange(oBetaTrail) != trailAcceptanceThreshold) {oBeta.prePress.push(oBetaTrail);}
@@ -224,9 +216,17 @@ function learn(dataArr) {
224216
}
225217
break;
226218
}
219+
if (!stream.isEmpty()) {
220+
process.stdout.clearLine(); // clear current text
221+
process.stdout.cursorTo(0); // return cursor
222+
} else {
223+
process.stdout.write("\n");
224+
}
227225
}
228-
if (!exit) {
229-
var data;
226+
if (!exit && hasPress) {
227+
console.log("Prep complete!");
228+
var data = {oBeta: {}, oGamma: {}, aZ: {}, aY: {}};
229+
console.log("Formatting");
230230
data.oBeta.prePress = oBeta.prePress.map(arrToBrainFormat.bind(this, {keypress: 1}));
231231
data.oBeta.postPress = oBeta.postPress.map(arrToBrainFormat.bind(this, {keypress: 1}));
232232
data.oGamma.prePress = oGamma.prePress.map(arrToBrainFormat.bind(this, {keypress: 1}));

0 commit comments

Comments
 (0)