Last active
November 2, 2020 19:19
-
-
Save jul/ea76268857634cfc95896362db4a01ed to your computer and use it in GitHub Desktop.
moving median
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
#!/usr/bin/env gnuplot | |
set timefmt '%Y-%m-%d' | |
set xdata time | |
set yrange [1<*:] | |
set format x '%Y-%m-%d' | |
set xrange ["2020-03-01"<*:] | |
set datafile sep ',' | |
set logscale y 10 | |
set terminal png size 800,600; set output 'orig.png'; | |
plot 'this.csv' u 1:2 w l title 'orig'~ | |
set terminal png size 1800,1000; set output 'mm.png'; | |
#plot 'this.csv' u 1:3 w l title 'mm' | |
plot 'this.csv' u 1:4 w l title 'mm 60%ile' ,'' u 1:3 w l title 'mm 40%ile', '' u 1:5 w l title "ma" | |
set terminal png size 1800,1000; set output 'ma.png'; | |
#plot 'this.csv' u 1:4 w l title 'ma' | |
set title "Cas de COVID 2019 en France en nouveau cas testés positifs par millions d'habitants (échelle log10) (jul)\nsource https://ourworldindata.org/coronavirus-data-explorer\nNB: Les tests sous évaluent la réalité" | |
show title | |
plot 'this.csv' u 1:4 w l title 'mediane mouvante à 7jours'# , '' u 1:3 w l title "70% ile 7jours" |
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
#!/usr/bin/env python3 | |
# curl https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv > info.csv | |
import csv | |
res = dict() | |
from json import dumps | |
pp = lambda x:print(dumps(x, indent=4)) | |
with open('info.csv', newline='') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in filter(lambda r:r["iso_code"]=="FRA", reader): | |
res[row["date"]]=float(row["new_cases_per_million"]) | |
def generate_and_set_moving_function(compute_window, name): | |
def filtre_moving(data, win_size=7, **kw): | |
window = [] | |
res=[] | |
for d in data: | |
window += [d] | |
res+= [compute_window(window, **kw)] | |
#print(window) | |
if len(window) == win_size: | |
window.pop(0) | |
assert len(data)==len(res) | |
return res | |
# ugly workaround around python's unability to make you change the function | |
# name on the fly, or parametrize (macro) the function name | |
filtre_moving.__name__ = name | |
globals()[name]=filtre_moving | |
moving_function( | |
lambda window, **kw:sorted(window)[int(kw.get("ratiotile",.5)*len(window))], | |
"moving_ratiotile" | |
) | |
moving_function( | |
lambda window, **kw: sum(window)/(len(window) or 1), | |
"moving_average" | |
) | |
WS=7 | |
RT=.2 | |
print("date,new_cases_per_million,moving_%sile_new_cases_per_million,moving_%sile_new_cases_per_million, moving_average" % | |
(int(RT*100), int((1-RT)*100))) | |
for l in zip(res.keys(), res.values(), | |
moving_average(res.values(), win_size=WS, ratiotile=RT), | |
moving_ratiotile(res.values(), win_size=WS, ratiotile=1-RT), | |
moving_ratiotile(res.values(), win_size=WS), | |
): | |
print(",".join(map(str,l))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment