Last active
June 18, 2021 17:17
-
-
Save mekanics/b6ecb764d5b5048fd623767454ddca6b to your computer and use it in GitHub Desktop.
COVID-19 Vaccination predication in Switzerland – Widget Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: red; icon-glyph: plus-square; | |
/** | |
* @akosma wrote this nice little script (in python) to try to figure out when the | |
* Swiss population would be fully vaccinated (with both doses of | |
* the COVID-19 vaccination). The script would print out the result to the | |
* console. This is the JS version, which displays the result in a widget. | |
* | |
* | |
* You'll find the original code source here: | |
* https://gitlab.com/akosma/covid-19-vaccination-in-switzerland/ | |
* Thank you @akosma | |
*/ | |
async function getPredictedDate() { | |
try { | |
// Download list of all latest files available for download | |
const filesUrl = 'https://www.covid19.admin.ch/api/data/context' | |
const filesResp = new Request(filesUrl) | |
const filesJson = await filesResp.loadJSON() | |
// Download the JSON stats for fully vaccinated people | |
const fullyVaccUrl = filesJson['sources']['individual']['json']['fullyVaccPersons'] | |
const fullyVaccResp = new Request(fullyVaccUrl) | |
const fullyVaccJson = await fullyVaccResp.loadJSON() | |
// Filter JSON data for the whole country | |
// Schema docs: | |
// https://www.covid19.admin.ch/api/data/documentation/models/sources-definitions-vaccinationincomingdata.md | |
chEntries = fullyVaccJson.filter((entry) => entry.geoRegion === 'CH') | |
population = chEntries[0]['pop'] | |
// Aggregate for regression | |
aggregates = chEntries.map((entry) => [new Date(entry.date).getTime() / 1000, entry.sumTotal]) | |
// Linear regression | |
const result = linear(aggregates, { order: 2, precision: 20, period: null }) | |
const gradient = result.equation[0] | |
const yIntercept = result.equation[1] | |
// Predict the date where everyone will be vaccinated at the current rate | |
const prediction = (population - yIntercept) / gradient | |
const predictedDate = new Date(prediction * 1000) | |
return predictedDate | |
} catch (err) { | |
console.error(err) | |
} | |
} | |
async function createWidget() { | |
const predictedDate = await getPredictedDate() | |
// Widget | |
const w = new ListWidget() | |
w.backgroundColor = new Color('#edf2f4') | |
// Logo 💉 | |
const logo = w.addText('💉') | |
logo.font = Font.title1() | |
w.addSpacer(5) | |
// Description | |
const txt = w.addText('All of 🇨🇭 vaccinated on:') | |
txt.font = Font.subheadline() | |
txt.textColor = new Color('#8d99ae') | |
// Prediction | |
const prediction = w.addText(`${predictedDate ? predictedDate.toLocaleDateString('en-CH') : '¯\\_(ツ)_/¯'}`) | |
prediction.font = Font.headline() | |
prediction.textColor = new Color('#2b2d42') | |
w.addSpacer() | |
return w | |
} | |
const widget = await createWidget() | |
if (config.runsInWidget) { | |
Script.setWidget(widget) | |
Script.complete() | |
} else { | |
widget.presentSmall() | |
} | |
// ============================================================================= | |
// part of regression-js | |
// https://github.com/Tom-Alexander/regression-js | |
/** | |
The MIT License (MIT) | |
Copyright (c) Tom Alexander <me@tomalexander.co.nz> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
/** | |
* Round a number to a precision, specificed in number of decimal places | |
* | |
* @param {number} number - The number to round | |
* @param {number} precision - The number of decimal places to round to: | |
* > 0 means decimals, < 0 means powers of 10 | |
* | |
* | |
* @return {numbr} - The number, rounded | |
*/ | |
function round(number, precision) { | |
const factor = 10 ** precision | |
return Math.round(number * factor) / factor | |
} | |
function linear(data, options) { | |
const sum = [0, 0, 0, 0, 0] | |
let len = 0 | |
for (let n = 0; n < data.length; n++) { | |
if (data[n][1] !== null) { | |
len++ | |
sum[0] += data[n][0] | |
sum[1] += data[n][1] | |
sum[2] += data[n][0] * data[n][0] | |
sum[3] += data[n][0] * data[n][1] | |
sum[4] += data[n][1] * data[n][1] | |
} | |
} | |
const run = len * sum[2] - sum[0] * sum[0] | |
const rise = len * sum[3] - sum[0] * sum[1] | |
const gradient = run === 0 ? 0 : round(rise / run, options.precision) | |
const intercept = round(sum[1] / len - (gradient * sum[0]) / len, options.precision) | |
const predict = (x) => [round(x, options.precision), round(gradient * x + intercept, options.precision)] | |
const points = data.map((point) => predict(point[0])) | |
return { | |
points, | |
predict, | |
equation: [gradient, intercept], | |
// r2: round(determinationCoefficient(data, points), options.precision), | |
string: intercept === 0 ? `y = ${gradient}x` : `y = ${gradient}x + ${intercept}`, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment