Created
August 4, 2020 09:02
-
-
Save pitermarx/4989e3f36518f3e1b9ef19b02cbf8257 to your computer and use it in GitHub Desktop.
LinqPad script for seeing Covid Data
This file contains hidden or 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
var data = Parse(@"https://raw.githubusercontent.com/dssg-pt/covid19pt-data/master/data.csv", | |
v => new | |
{ | |
data = DateTime.Parse(v["data"]), | |
novos_total = decimal.Parse(v["confirmados"]), | |
novos = decimal.Parse(v["confirmados_novos"]) | |
}); | |
var amostras = Parse(@"https://raw.githubusercontent.com/dssg-pt/covid19pt-data/master/amostras.csv", | |
v => new | |
{ | |
data = DateTime.Parse(v["data"]), | |
amostras_total = decimal.TryParse(v["amostras"].Split('.')[0], out var na) ? na : 0, | |
amostras = decimal.TryParse(v["amostras_novas"].Split('.')[0], out var n) ? n : 0 | |
}); | |
amostras | |
.Join(data, x => x.data, x => x.data, (x, y) => (x.data, x.amostras, y.novos, x.amostras_total, y.novos_total)) | |
.Chart (c => c.data) | |
.AddYSeries(c => c.amostras == 0 ? 0 : c.novos/c.amostras, Util.SeriesType.Column, "Novo/Amostras diário") | |
.AddYSeries(c => c.amostras_total == 0 ? 0 : c.novos_total/c.amostras_total, Util.SeriesType.Spline, "Total Novo/Amostras") | |
.DumpInline(); | |
IEnumerable<T> Parse<T>(string url, Func<Dictionary<string, string>, T> selector) | |
{ | |
var lines = ReadLines(url).Select(l => l.Split(',').Select((value,index) => (value, index))); | |
var headers = lines.First().ToDictionary(x => x.index, x => x.value); | |
foreach (var l in lines.Skip(1)) | |
{ | |
var d = l.ToDictionary(v => headers[v.index], v => v.value); | |
yield return selector(d); | |
} | |
} | |
IEnumerable<string> ReadLines(string url) { | |
var sr = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream()); | |
while(sr.ReadLine() is string l) yield return l; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Referenced in https://twitter.com/pitermarx/status/1290573973722365952