diff --git a/server/train-ai/DatasetFetcher.mjs b/server/train-ai/DatasetFetcher.mjs index 8fc79a4..afd0cce 100644 --- a/server/train-ai/DatasetFetcher.mjs +++ b/server/train-ai/DatasetFetcher.mjs @@ -30,19 +30,18 @@ class DatasetFetcher { let iterator = gateway_id == null ? this.repo_rssi.iterate() : this.repo_rssi.iterate_gateway(gateway_id); // Add the readings where we did get a signal for(let rssi of iterator) { - if(gateway_id == null) - gateway_location = this.repo_gateway.get_by_id(rssi.gateway_id); let item = { input: { latitude: rssi.latitude, - longitude: rssi.longitude, - distance: haversine(gateway_location, rssi) + longitude: rssi.longitude }, output: [ rssi.rssi ] }; + if(gateway_id !== null) + item.input.distance = haversine(gateway_location, rssi); if(extended) { item.ext = { gateway: rssi.gateway_id, @@ -57,11 +56,13 @@ class DatasetFetcher { let item = { input: { latitude: reading.latitude, - longitude: reading.longitude, - distance: haversine(gateway_location, reading) + longitude: reading.longitude }, output: [ -150 ] }; + if(gateway_id !== null) + item.input.distance = haversine(gateway_location, reading); + if(extended) { item.ext = { gateway: "(none)", @@ -89,15 +90,17 @@ class DatasetFetcher { for(let item of result) { item.input.latitude = normalise_lat(item.input.latitude); item.input.longitude = normalise_lng(item.input.longitude); - item.input.distance = normalise_gateway_distance(item.input.distance); + if(typeof item.input.distance == "number") + item.input.distance = normalise_gateway_distance(item.input.distance); item.output[0] = normalise_rssi(item.output[0]); } + // Scan the resulting dataset for invalid items + this.scan_for_corruption(result); + // Shuffle the dataset shuffle_fisher_yates(result); - // Scan the resulting dataset for invalid items - this.scan_for_corruption(result); return result; } @@ -125,7 +128,7 @@ class DatasetFetcher { next_item.input, comp_item.input ); - if(isNaN(distance)) + if(Number.isNaN(distance)) throw new Error(`Error: Got NaN when checking zapping distance.`); if(distance < max_distance_metres) { @@ -146,13 +149,9 @@ class DatasetFetcher { scan_for_corruption(dataset) { // Scan the input data to make sure it is't corrupt for(let row of dataset) { - if(isNaN(row.output[0]) || isNaN(row.input.latitude) || isNaN(row.input.longitude) || isNaN(row.input.distance)) { + if(Number.isNaN(row.output[0]) || Number.isNaN(row.input.latitude) || Number.isNaN(row.input.longitude) || Number.isNaN(row.input.distance)) { console.error(row); - throw new Error("Error: Found NaN in input data"); - } - if(typeof row.output[0] !== "number" || typeof row.input.latitude !== "number" || typeof row.input.longitude !== "number" || typeof row.input.distance !== "number") { - console.error(row); - throw new Error("Error: Found item with an invalid type in the input data"); + throw new Error("Error: Found invalid value in input data"); } }