Skip to content

Instantly share code, notes, and snippets.

@samth
Last active March 2, 2022 16:31
Show Gist options
  • Save samth/802a28a0b5d4f10f05fb99e15d3991c7 to your computer and use it in GitHub Desktop.
Save samth/802a28a0b5d4f10f05fb99e15d3991c7 to your computer and use it in GitHub Desktop.
#lang racket
(require sawzall data-frame json graphite racket/runtime-path)
(require pict)
(require (only-in plot date-ticks) gregor threading)
(define-runtime-path here ".")
(define fips-codes (delay (~> (df-read/csv (build-path here "state_and_county_fips_master.csv"))
(where (state) (equal? state "IN")))))
;; this file is at https://www.coronavirus.in.gov/map/covid-19-indiana-universal-report-current-public.json
(define v (delay (read-json (open-input-file (build-path here "covid-19-indiana-universal-report-current-public.json")))))
(define (hash-ref* v . rest)
(for/fold ([v v])
([k (in-list rest)])
(hash-ref v k)))
(define data (delay (hash-ref* (force v) 'metrics 'data)))
(define iso8601->posix (compose ->posix iso8601->date))
(define df
(delay
(let ([data (force data)])
(for/data-frame (date icu-covid icu-not-covid icu-supply icu-available district hospital-beds hospital-pts hospital-pui)
([date (hash-ref data 'date)]
[ic (hash-ref data 'm2b_hospitalized_icu_occupied_covid)]
[inc (hash-ref data 'm2b_hospitalized_icu_occupied_non_covid)]
[is (hash-ref data 'm2b_hospitalized_icu_supply)]
[ia (hash-ref data 'm2b_hospitalized_icu_available)]
[beds (hash-ref data 'm1a_beds_all_occupied_beds_covid_19_smoothed)]
[pts (hash-ref data 'm1a_beds_all_occupied_beds_covid_19_pts_smoothed)]
[pui (hash-ref data 'm1a_beds_all_occupied_beds_covid_19_pui_smoothed)]
[district (hash-ref data 'district)]
#;#;
#:when (< (string->number district) 20)
#;#;
#:when (> inc 10))
(values date ic inc is ia district beds pts pui)))))
(define (->district d*)
(cond [(not d*) "0"]
[(number? d*) (number->string d*)]
[(string->number d*) d*]
[else (number->string (lookup-county-code d*))]))
(define (icu-district [d* #f])
(define d (->district d*))
(graph #:data (~> (force df)
(where (district) (equal? district d))
(where (icu-supply) (>= icu-supply 1))
(create [total-icu (icu-covid icu-not-covid) (+ icu-covid icu-not-covid)]))
#:x-transform (only-ticks (date-ticks))
#:x-conv iso8601->posix
#:mapping (aes #:x "date")
#:title (format "ICU occupancy, ~a" (district->name d))
#:width 800
#:legend-anchor 'top-right
#:y-label "ICU Beds"
#:x-label ""
(lines #:color 1 #:mapping (aes #:y "icu-covid") #:label "COVID ICU patients")
(lines #:color 2 #:mapping (aes #:y "total-icu") #:label "Total ICU patients")
(lines #:color 3 #:mapping (aes #:y "icu-supply") #:label "Total ICU beds")))
(define (district->name d)
(define d* (string->number d))
(cond [(= 0 d*) "Indiana statewide"]
[(< d* 20) (format "District ~a" d)]
[else (lookup-county-name d*)]))
(define (hospital-district d*)
(define d (->district d*))
(graph #:data (~> (force df)
(where (district hospital-beds) (and (equal? district d) (> hospital-beds 0))))
#:x-transform (only-ticks (date-ticks))
#:x-conv iso8601->posix
#:mapping (aes #:x "date")
#:title
(format "Hospitalization, ~a" (district->name d))
#:width 800
#:legend-anchor 'top-left
#:x-label "Date"
#:y-label ""
(lines #:color 1 #:mapping (aes #:y "hospital-beds") #:label "All COVID Hospitalizations")
#;
(lines #:color 2 #:mapping (aes #:y "hospital-pts"))
(lines #:color 3 #:mapping (aes #:y "hospital-pui" ) #:label "COVID PUI Hospitalizations")))
(define (hospital-county n)
(hospital-district (format "181~a" n)))
(define (lookup-county-code pat)
(for/first ([(f n) (in-data-frame (force fips-codes) "fips" "name")]
#:when (regexp-match pat n))
f))
(define (lookup-county-name n)
(for/first ([(f name) (in-data-frame (force fips-codes) "fips" "name")]
#:when (= n f))
name))
(define (add-margin n p)
(define bg (filled-rectangle #:color "white" #:draw-border? #f (+ n (pict-width p)) (+ n (pict-height p))))
(cc-superimpose bg p))
(require simple-xlsx)
(define (write-fips-xlsx path)
(let ([xlsx (new xlsx%)])
(send xlsx add-data-sheet
#:sheet_name "FIPS Codes"
#:sheet_data
(cons '("fips" "name" "state")
(for/list ([(f name state) (in-data-frame (force fips-codes) "fips" "name" "state")]) (list f name state))))
(write-xlsx-file xlsx path)))
; use as
;(write-fips-xlsx "/home/samth/out.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment