-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from john-sandall/csse-data
Add dataset: "coronavirus/CSSE"
- Loading branch information
Showing
13 changed files
with
471 additions
and
66 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,7 @@ maven.egg-info/ | |
|
||
# IDE ignores | ||
.vscode | ||
|
||
# Checklists | ||
DEPLOY | ||
REVIEW |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Coronavirus (COVID-19) datasets | ||
|
||
If you have any questions about these datasets please [contact me @John_Sandall](https://twitter.com/John_Sandall) on Twitter. | ||
|
||
|
||
## Sources | ||
We aim to source our data directly from the most authorative data provider, falling back to less authorative sources where a primary source isn't available. | ||
|
||
Global providers/aggregators: | ||
- [Johns Hopkins Center for Systems Science and Engineering](https://github.com/CSSEGISandData/COVID-19/). | ||
|
||
|
||
## Data dictionaries | ||
|
||
#### **`coronavirus/CSSE`** | ||
|
||
##### `CSSE_country_province.csv` | ||
| Column | Type | Description | Example | | ||
| -- | -- | -- | -- | | ||
| `date` | date | Date | `2020-03-13` | | ||
| `country_region` | str | Country/Region | `US` | | ||
| `province_state` | str | Province/State | `Washington` | | ||
| `lat` | float | Latitude | `47.4009` | | ||
| `lon` | float | Longitude | `-121.4905` | | ||
| `confirmed` | int | Confirmed cases | `568` | | ||
| `deaths` | int | Fatalities | `37` | | ||
| `recovered` | int | Recovered | `1` | | ||
|
||
##### `CSSE_country.csv` | ||
| Column | Type | Description | Example | | ||
| -- | -- | -- | -- | | ||
| `date` | date | Date | `2020-03-13` | | ||
| `country_region` | str | Country/Region | `US` | | ||
| `confirmed` | int | Confirmed cases | `2179` | | ||
| `deaths` | int | Fatalities | `47` | | ||
| `recovered` | int | Recovered | `12` | |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .csse import CSSE | ||
|
||
__all__ = [ | ||
"CSSE", | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
""" | ||
Coronavirus CSSE data from https://github.com/CSSEGISandData/COVID-19/ | ||
Usage: | ||
>>> import maven | ||
>>> maven.get('coronavirus/CSSE', data_directory='./data/') | ||
Sources: | ||
- https://github.com/CSSEGISandData/COVID-19/ | ||
""" | ||
import os | ||
from pathlib import Path | ||
|
||
import pandas as pd | ||
|
||
from maven import utils | ||
|
||
|
||
class CSSE(utils.Pipeline): | ||
"""Handle CSSE data from https://github.com/CSSEGISandData/COVID-19/""" | ||
|
||
def __init__(self, directory=Path("data/coronavirus/CSSE")): | ||
# inherit base __init__ but override default directory | ||
super(CSSE, self).__init__(directory=directory) | ||
# Source & targets | ||
base_url = ( | ||
"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/" | ||
"csse_covid_19_data/csse_covid_19_time_series/" | ||
) | ||
self.sources = [ | ||
# url, filename, checksum | ||
(base_url, "time_series_19-covid-Confirmed.csv", "09b6dfc1ee244ba652b8639f0aa2f093"), | ||
(base_url, "time_series_19-covid-Deaths.csv", "69a9dfa8a901c8f0bbe0f6499db8641c"), | ||
(base_url, "time_series_19-covid-Recovered.csv", "4d1c1d4f1c45514e3562cb42ef2729c7"), | ||
] | ||
self.targets = [ | ||
# filename, checksum( | ||
("CSSE_country_province.csv", "bfce6bf16571fbb3004f9e5eee7b9e30"), | ||
("CSSE_country.csv", "b5b3ed6fc75f323593fd7710a4262e1b"), | ||
] | ||
# Config | ||
self.rename_source = False | ||
self.retrieve_all = True | ||
self.cache = True | ||
self.verbose = False | ||
self.verbose_name = "CSSE" | ||
|
||
def process(self): | ||
"""Process CSSE data.""" | ||
target_dir = self.directory / "processed" | ||
os.makedirs(target_dir, exist_ok=True) # create directory if it doesn't exist | ||
|
||
def process_and_export(): | ||
"""Either caching disabled or file not yet processed; process regardless.""" | ||
data = {} | ||
for metric in ["Confirmed", "Deaths", "Recovered"]: | ||
df = pd.read_csv(self.directory / "raw" / f"time_series_19-covid-{metric}.csv") | ||
# Pivot all to long | ||
id_vars = ["Province/State", "Country/Region", "Lat", "Long"] | ||
value_vars = list(set(df.columns) - set(id_vars)) | ||
df = df.melt( | ||
id_vars=id_vars, value_vars=value_vars, var_name="date", value_name=metric | ||
) | ||
df["date"] = pd.to_datetime(df.date, format="%m/%d/%y") | ||
data[metric] = df.copy() | ||
|
||
# Merge together | ||
df_country_province = pd.merge( | ||
data["Confirmed"], | ||
data["Deaths"], | ||
how="outer", | ||
on=["Province/State", "Country/Region", "Lat", "Long", "date"], | ||
).merge( | ||
data["Recovered"], | ||
how="outer", | ||
on=["Province/State", "Country/Region", "Lat", "Long", "date"], | ||
) | ||
|
||
# Clean | ||
df_country_province.columns = utils.sanitise( | ||
df_country_province.columns, replace={"long": "lon"} | ||
) | ||
df_country_province = df_country_province[ | ||
[ | ||
"date", | ||
"country_region", | ||
"province_state", | ||
"lat", | ||
"lon", | ||
"confirmed", | ||
"deaths", | ||
"recovered", | ||
] | ||
].sort_values(["date", "country_region", "province_state"]) | ||
|
||
# Country-level data | ||
df_country = ( | ||
df_country_province.groupby(["date", "country_region"])[ | ||
["confirmed", "deaths", "recovered"] | ||
] | ||
.sum() | ||
.reset_index() | ||
) | ||
|
||
# Export | ||
print(f"Exporting dataset to {target_dir.resolve()}") | ||
df_country_province.to_csv(target_dir / "CSSE_country_province.csv", index=False) | ||
df_country.to_csv(target_dir / "CSSE_country.csv", index=False) | ||
|
||
for filename, checksum in self.targets: | ||
utils.retrieve_from_cache_if_exists( | ||
filename=filename, | ||
target_dir=target_dir, | ||
processing_fn=process_and_export, | ||
md5_checksum=checksum, | ||
caching_enabled=self.cache, | ||
verbose=self.verbose, | ||
) |
Oops, something went wrong.