-
-
Notifications
You must be signed in to change notification settings - Fork 314
Source NYT #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Source NYT #259
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8751f7e
Merge pull request #1 from ExpDev07/master
ibhuiyan17 e8cd213
Started adding documentation for NYT source
ibhuiyan17 9ffe9e6
Started nyt.py file in services.location module
ibhuiyan17 4903abb
Testing nischal pushing to branch
nischalshankar 6a744a6
Merge pull request #2 from ibhuiyan17/source-nyt
ibhuiyan17 a9c5f37
Merge remote-tracking branch 'upstream/master'
nischalshankar 736b90d
Merge branch 'master' of https://github.com/ibhuiyan17/coronavirus-tr…
nischalshankar ce841d3
Temp directory to test parsing nyt timeseries
ibhuiyan17 5f587a9
Locally working nyt source on v2
nischalshankar cf9694c
Deleted temporary folder so it hopefully passes build
ibhuiyan17 8a27080
Added mock data, unit tests, and regression tests for source NYT
ibhuiyan17 59437b3
Updated README to reflect new NYT source
nischalshankar 149de08
Fixed requested points from PR
ibhuiyan17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ class Sources(str, Enum): | |
|
|
||
| jhu = "jhu" | ||
| csbs = "csbs" | ||
| nyt = "nyt" | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """app.locations.nyt.py""" | ||
| from . import TimelinedLocation | ||
|
|
||
|
|
||
| class NYTLocation(TimelinedLocation): | ||
| """ | ||
| A NYT (county) Timelinedlocation. | ||
| """ | ||
|
|
||
| # pylint: disable=too-many-arguments,redefined-builtin | ||
| def __init__(self, id, state, county, coordinates, last_updated, timelines): | ||
| super().__init__(id, "US", state, coordinates, last_updated, timelines) | ||
|
|
||
| self.state = state | ||
| self.county = county | ||
|
|
||
| def serialize(self, timelines=False): # pylint: disable=arguments-differ,unused-argument | ||
| """ | ||
| Serializes the location into a dict. | ||
|
|
||
| :returns: The serialized location. | ||
| :rtype: dict | ||
| """ | ||
| serialized = super().serialize(timelines) | ||
|
|
||
| # Update with new fields. | ||
| serialized.update( | ||
| {"state": self.state, "county": self.county,} | ||
| ) | ||
|
|
||
| # Return the serialized location. | ||
| return serialized |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """app.services.location.nyt.py""" | ||
| import csv | ||
| from datetime import datetime | ||
|
|
||
| from asyncache import cached | ||
| from cachetools import TTLCache | ||
|
|
||
| from ...coordinates import Coordinates | ||
| from ...location.nyt import NYTLocation | ||
| from ...timeline import Timeline | ||
| from ...utils import httputils | ||
| from . import LocationService | ||
|
|
||
|
|
||
| class NYTLocationService(LocationService): | ||
| """ | ||
| Service for retrieving locations from New York Times (https://github.com/nytimes/covid-19-data). | ||
| """ | ||
|
|
||
| async def get_all(self): | ||
| # Get the locations. | ||
| locations = await get_locations() | ||
| return locations | ||
|
|
||
| async def get(self, loc_id): # pylint: disable=arguments-differ | ||
| # Get location at the index equal to provided id. | ||
| locations = await self.get_all() | ||
| return locations[loc_id] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------- | ||
|
|
||
|
|
||
| # Base URL for fetching category. | ||
| BASE_URL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv" | ||
|
|
||
|
|
||
| def get_grouped_locations_dict(data): | ||
| """ | ||
| Helper function to group history for locations into one dict. | ||
| :returns: The complete data for each unique US county | ||
| :rdata: dict | ||
| """ | ||
| grouped_locations = {} | ||
|
|
||
| # in increasing order of dates | ||
| for row in data: | ||
| county_state = (row["county"], row["state"]) | ||
| date = row["date"] | ||
| confirmed = row["cases"] | ||
| deaths = row["deaths"] | ||
|
|
||
| # initialize if not existing | ||
| if county_state not in grouped_locations: | ||
| grouped_locations[county_state] = {"confirmed": [], "deaths": []} | ||
|
|
||
| # append confirmed tuple to county_state (date, # confirmed) | ||
| grouped_locations[county_state]["confirmed"].append((date, confirmed)) | ||
| # append deaths tuple to county_state (date, # deaths) | ||
| grouped_locations[county_state]["deaths"].append((date, deaths)) | ||
|
|
||
| return grouped_locations | ||
|
|
||
|
|
||
| @cached(cache=TTLCache(maxsize=1024, ttl=3600)) | ||
| async def get_locations(): | ||
| """ | ||
| Returns a list containing parsed NYT data by US county. The data is cached for 1 hour. | ||
| :returns: The complete data for US Counties. | ||
| :rtype: dict | ||
| """ | ||
|
|
||
| # Request the data. | ||
| async with httputils.CLIENT_SESSION.get(BASE_URL) as response: | ||
| text = await response.text() | ||
|
|
||
| # Parse the CSV. | ||
| data = list(csv.DictReader(text.splitlines())) | ||
|
|
||
| # Group together locations (NYT data ordered by dates not location). | ||
| grouped_locations = get_grouped_locations_dict(data) | ||
|
|
||
| # The normalized locations. | ||
| locations = [] | ||
|
|
||
| idx = 0 | ||
| for county_state, histories in grouped_locations.items(): | ||
ibhuiyan17 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Make location history for confirmed and deaths from dates. | ||
| # List is tuples of (date, amount) in order of increasing dates. | ||
| confirmed_list = histories["confirmed"] | ||
| confirmed_history = {date: int(amount or 0) for date, amount in confirmed_list} | ||
|
|
||
| deaths_list = histories["deaths"] | ||
| deaths_history = {date: int(amount or 0) for date, amount in deaths_list} | ||
|
|
||
| # Normalize the item and append to locations. | ||
| locations.append( | ||
| NYTLocation( | ||
| id=idx, | ||
| state=county_state[1], | ||
| county=county_state[0], | ||
| coordinates=Coordinates(None, None), # NYT does not provide coordinates | ||
| last_updated=datetime.utcnow().isoformat() + "Z", # since last request | ||
| timelines={ | ||
| "confirmed": Timeline( | ||
| { | ||
| datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount | ||
| for date, amount in confirmed_history.items() | ||
| } | ||
| ), | ||
| "deaths": Timeline( | ||
| { | ||
| datetime.strptime(date, "%Y-%m-%d").isoformat() + "Z": amount | ||
| for date, amount in deaths_history.items() | ||
| } | ||
| ), | ||
| "recovered": Timeline({}), | ||
| }, | ||
| ) | ||
| ) | ||
|
|
||
| idx += 1 | ||
|
|
||
| return locations | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| date,county,state,fips,cases,deaths | ||
| 2020-01-21,Snohomish,Washington,53061,1,0 | ||
| 2020-01-22,Snohomish,Washington,53061,1,0 | ||
| 2020-01-23,Snohomish,Washington,53061,1,0 | ||
| 2020-01-24,Cook,Illinois,17031,1,0 | ||
| 2020-01-24,Snohomish,Washington,53061,1,0 | ||
| 2020-01-25,Orange,California,06059,1,0 | ||
| 2020-01-25,Cook,Illinois,17031,1,0 | ||
| 2020-01-25,Snohomish,Washington,53061,1,0 | ||
| 2020-01-26,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-26,Los Angeles,California,06037,1,0 | ||
| 2020-01-26,Orange,California,06059,1,0 | ||
| 2020-01-26,Cook,Illinois,17031,1,0 | ||
| 2020-01-26,Snohomish,Washington,53061,1,0 | ||
| 2020-01-27,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-27,Los Angeles,California,06037,1,0 | ||
| 2020-01-27,Orange,California,06059,1,0 | ||
| 2020-01-27,Cook,Illinois,17031,1,0 | ||
| 2020-01-27,Snohomish,Washington,53061,1,0 | ||
| 2020-01-28,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-28,Los Angeles,California,06037,1,0 | ||
| 2020-01-28,Orange,California,06059,1,0 | ||
| 2020-01-28,Cook,Illinois,17031,1,0 | ||
| 2020-01-28,Snohomish,Washington,53061,1,0 | ||
| 2020-01-29,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-29,Los Angeles,California,06037,1,0 | ||
| 2020-01-29,Orange,California,06059,1,0 | ||
| 2020-01-29,Cook,Illinois,17031,1,0 | ||
| 2020-01-29,Snohomish,Washington,53061,1,0 | ||
| 2020-01-30,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-30,Los Angeles,California,06037,1,0 | ||
| 2020-01-30,Orange,California,06059,1,0 | ||
| 2020-01-30,Cook,Illinois,17031,2,0 | ||
| 2020-01-30,Snohomish,Washington,53061,1,0 | ||
| 2020-01-31,Maricopa,Arizona,04013,1,0 | ||
| 2020-01-31,Los Angeles,California,06037,1,0 | ||
| 2020-01-31,Orange,California,06059,1,0 | ||
| 2020-01-31,Santa Clara,California,06085,1,0 | ||
| 2020-01-31,Cook,Illinois,17031,2,0 | ||
| 2020-01-31,Snohomish,Washington,53061,1,0 | ||
| 2020-02-28,Snohomish,Washington,53061,2,0 | ||
| 2020-03-10,Snohomish,Washington,53061,61,0 | ||
| 2020-03-11,Snohomish,Washington,53061,69,1 | ||
| 2020-03-12,Snohomish,Washington,53061,107,3 | ||
| 2020-03-15,Snohomish,Washington,53061,175,3 | ||
| 2020-03-17,Snohomish,Washington,53061,265,4 | ||
| 2020-03-18,Snohomish,Washington,53061,309,5 | ||
| 2020-03-19,Snohomish,Washington,53061,347,6 | ||
| 2020-03-20,Snohomish,Washington,53061,384,7 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.