nuts-data.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const fs = require('fs');
  2. const csv = require('csv-parse');
  3. const stringify = require('csv-stringify');
  4. /* Helper method to load the datasets from CSV and store it in server object */
  5. module.exports.loadDatasets = async function(filePath) {
  6. //console.log('Datasets structure loading.')
  7. let datasets = undefined
  8. return new Promise((resolve, reject) => {
  9. const stream = fs.createReadStream(filePath).pipe(csv({to_line: 1}))
  10. stream
  11. .on('data', (row) => {
  12. if (!datasets) {
  13. datasets = row
  14. }
  15. })
  16. .on('end', () => {
  17. datasets = datasets.slice(2) //TODO: FIXME: unify this number (only one ID column in the beginning)
  18. console.log('Datasets structure loaded.')
  19. resolve(datasets)
  20. })
  21. .on('error', reject)
  22. })
  23. }
  24. /* Load all the attractivness data from CSV into a server object.
  25. The data don't need to be loaded for each request then. */
  26. module.exports.loadRuralData = async function (filePath) {
  27. //console.log('Reading rural data file processing started.')
  28. let ruralData = []
  29. let columns
  30. return new Promise((resolve, reject) => {
  31. fs.createReadStream(filePath)
  32. .pipe(csv())
  33. .on('data', (row) => {
  34. if (!columns) {
  35. columns = row
  36. return
  37. }
  38. let item = {
  39. values: {}
  40. }
  41. for (let i = 0; i < columns.length; i++) {
  42. let colName = columns[i].toLowerCase()
  43. if (colName == "nuts_id") // ID of the NUTS region (EU)
  44. item.nuts = row[i]
  45. // else if (colName == "datasets") // empty datasets count
  46. // item.availableDS = datasets.length - row[i];
  47. // else if (colName == "quality")
  48. // item.quality = row[i];
  49. else if (colName == "lau2") // ID of the municipality (CZ)
  50. item.lau2 = row[i]
  51. else if (colName == "district_code") // ID of the district (Kenya+Uganda)
  52. item.district = row[i]
  53. else if (colName == "eurostat_code" || colName == "name")
  54. continue
  55. else {
  56. item.values[colName] = Number(row[i])
  57. }
  58. }
  59. ruralData.push(item)
  60. })
  61. .on('end', () => {
  62. console.log('Rural data file processing finished.');
  63. resolve(ruralData);
  64. })
  65. .on('error', reject);
  66. })
  67. }
  68. module.exports.loadOntology = async function(filePath) {
  69. return new Promise((resolve, reject) => {
  70. fs.readFile(filePath, (err, data) => {
  71. if (err) reject(err)
  72. const ontology = JSON.parse(data)
  73. resolve(ontology)
  74. })
  75. })
  76. }
  77. /**
  78. * Resolves with an array representing rows of CSV file
  79. * @param {string} inputFileName path to the CSV file with input data for clustering calculation
  80. */
  81. module.exports.loadClusteringInput = async function (inputFileName) {
  82. const clusteringData = [];
  83. /*
  84. * The parsed CSV array keeps the native csv-parser structure
  85. * for future easier serialization back to CSV file
  86. */
  87. return new Promise((resolve, reject) => {
  88. fs.createReadStream(inputFileName)
  89. .pipe(csv())
  90. .on('data', (row) => {
  91. row = [row[0], ...row.slice(2)]
  92. clusteringData.push(row);
  93. })
  94. .on('end', () => {
  95. resolve(clusteringData);
  96. })
  97. .on('error', reject);
  98. });
  99. }
  100. /**
  101. * Resolves once the modified CSV file is written to fs
  102. */
  103. module.exports.modifyClusteringData = async function ({datasets, data, params, idString, outputFileName}) {
  104. // regional ID must be copied to the output as well
  105. const allowedDatasets = [idString, ...params.datasets.map(ds => ds.id)]
  106. const factorMultipliers = data[0].map((dataset) => {
  107. if (dataset === idString) return 1
  108. if (!allowedDatasets.includes(dataset)) {
  109. return 0
  110. } else {
  111. return params.datasets.find(ds => ds.id === dataset).weight
  112. }
  113. })
  114. /* The actual modification logic resides here */
  115. const modifiedData = data.map((row, idx) => {
  116. return row.map((value, i) => {
  117. if (idx == 0) {
  118. /* These are the headers */
  119. /* Have to check for both allowed datasets and zero multiplications */
  120. return allowedDatasets.includes(value) && factorMultipliers[i] !== 0 ? value : null;
  121. } else if (isNaN(value)) {
  122. /* This is the NUTS ID record at the beginning of each line */
  123. return value;
  124. }
  125. return factorMultipliers[i] === 0 ? null : value*factorMultipliers[i];
  126. }).filter(val => val !== null);
  127. });
  128. //console.log(modifiedData);
  129. if (modifiedData[0].length <= 1) {
  130. throw new Error('All datasets turned off. No data to create clusters.');
  131. }
  132. return new Promise((resolve, reject) => {
  133. stringify(modifiedData, (err, output) => {
  134. if (err) return reject(err);
  135. fs.writeFile(outputFileName, output, (err) => {
  136. if (err) reject(err);
  137. else resolve();
  138. console.log('Data modification finished.');
  139. })
  140. })
  141. });
  142. }
  143. /**
  144. * Reads the out_file.csv created by R script and saves it into an object
  145. */
  146. module.exports.loadClusters = function (filePath, idString, dataLoadedCallback) {
  147. //console.log('Reading clustering data file processing started.');
  148. let clusters = [];
  149. let columns = undefined;
  150. fs.createReadStream(filePath)
  151. .pipe(csv())
  152. .on('data', (row) => {
  153. if (!columns) {
  154. columns = row;
  155. }
  156. else {
  157. let item = {};
  158. for (let i = 0; i < columns.length; i++) {
  159. const colName = columns[i].length > 0 ? columns[i].toLowerCase() : idString;
  160. item[colName] = row[i];
  161. }
  162. clusters.push(item);
  163. }
  164. })
  165. .on('end', () => {
  166. console.log('Cluster data file processing finished.');
  167. dataLoadedCallback(clusters);
  168. });
  169. }
  170. module.exports.getFactorIndex = function (region, factor) {
  171. //console.log('getFactorIndex');
  172. //console.log('region: ' + JSON.stringify(region, null, 4));
  173. //console.log('factor: ' + JSON.stringify(factor, null, 4));
  174. let sumValue = 0;
  175. let count = 0;
  176. factor.datasets.forEach(ds => {
  177. const dataset = ds.split('/').slice(-1).pop()
  178. //console.log('factor: ' + factor.factor);
  179. const value = region.values[dataset];
  180. if (value) {
  181. sumValue += value;
  182. count++;
  183. }
  184. });
  185. return { index: sumValue / count, sumValue: sumValue, sumWeight: count * factor.weight };
  186. }
  187. /* Unused */
  188. function getDatasetFactor(datasets, colName) {
  189. for (let i = 0; i < datasets.length; i++) {
  190. if (datasets[i].Name.toLowerCase() == colName)
  191. return datasets[i].Factor;
  192. }
  193. return undefined;
  194. }