Skip to content

Instantly share code, notes, and snippets.

@pitermarx
Created August 4, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitermarx/4989e3f36518f3e1b9ef19b02cbf8257 to your computer and use it in GitHub Desktop.
Save pitermarx/4989e3f36518f3e1b9ef19b02cbf8257 to your computer and use it in GitHub Desktop.
LinqPad script for seeing Covid Data
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;
}
@pitermarx
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment