* Load and preprocess data. * * This method first tries to load the data from `LOCAL_JENA_WEATHER_CSV_PATH` * (a relative path) and, if that fails, will try to load it from a remote * URL (`JENA_WEATHER_CSV_PATH`).
()
| 50 | * URL (`JENA_WEATHER_CSV_PATH`). |
| 51 | */ |
| 52 | async load() { |
| 53 | let response; |
| 54 | try { |
| 55 | response = await fetch(LOCAL_JENA_WEATHER_CSV_PATH); |
| 56 | } catch (err) {} |
| 57 | |
| 58 | if (response != null && |
| 59 | (response.statusCode === 200 || response.statusCode === 304)) { |
| 60 | console.log('Loading data from local path'); |
| 61 | } else { |
| 62 | response = await fetch(REMOTE_JENA_WEATHER_CSV_PATH); |
| 63 | console.log( |
| 64 | `Loading data from remote path: ${REMOTE_JENA_WEATHER_CSV_PATH}`); |
| 65 | } |
| 66 | const csvData = await response.text(); |
| 67 | |
| 68 | // Parse CSV file. |
| 69 | const csvLines = csvData.split('\n'); |
| 70 | |
| 71 | // Parse header. |
| 72 | const columnNames = csvLines[0].split(','); |
| 73 | for (let i = 0; i < columnNames.length; ++i) { |
| 74 | // Discard the quotes around the column name. |
| 75 | columnNames[i] = columnNames[i].slice(1, columnNames[i].length - 1); |
| 76 | } |
| 77 | |
| 78 | this.dateTimeCol = columnNames.indexOf('Date Time'); |
| 79 | tf.util.assert(this.dateTimeCol === 0, `Unexpected date-time column index`); |
| 80 | |
| 81 | this.dataColumnNames = columnNames.slice(1); |
| 82 | this.tempCol = this.dataColumnNames.indexOf('T (degC)'); |
| 83 | tf.util.assert(this.tempCol >= 1, `Unexpected T (degC) column index`); |
| 84 | |
| 85 | this.dateTime = []; |
| 86 | this.data = []; // Unnormalized data. |
| 87 | // Day of the year data, normalized between 0 and 1. |
| 88 | this.normalizedDayOfYear = []; |
| 89 | // Time of the day, normalized between 0 and 1. |
| 90 | this.normalizedTimeOfDay = []; |
| 91 | for (let i = 1; i < csvLines.length; ++i) { |
| 92 | const line = csvLines[i].trim(); |
| 93 | if (line.length === 0) { |
| 94 | continue; |
| 95 | } |
| 96 | const items = line.split(','); |
| 97 | const parsed = this.parseDateTime_(items[0]); |
| 98 | const newDateTime = parsed.date; |
| 99 | if (this.dateTime.length > 0 && |
| 100 | newDateTime.getTime() <= |
| 101 | this.dateTime[this.dateTime.length - 1].getTime()) { |
| 102 | } |
| 103 | |
| 104 | this.dateTime.push(newDateTime); |
| 105 | this.data.push(items.slice(1).map(x => +x)); |
| 106 | this.normalizedDayOfYear.push(parsed.normalizedDayOfYear); |
| 107 | this.normalizedTimeOfDay.push(parsed.normalizedTimeOfDay); |
| 108 | } |
| 109 | this.numRows = this.data.length; |
no test coverage detected