-
Notifications
You must be signed in to change notification settings - Fork 16
nssp patching code #2000
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
+627
−27
Merged
nssp patching code #2000
Changes from 8 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
c78ae21
nssp patching code
minhkhul 7694c0a
lint
minhkhul a3ed4c2
add test
minhkhul 1628d34
add test
minhkhul 2536b94
Add patching how-to to readme
minhkhul e4d45e5
adjust current_issue_dir name for weekly data instead of daily.
minhkhul db906fc
lint
minhkhul 8f0bb32
adjust test for more cases
minhkhul 7f151f5
add custom_run flag
minhkhul c093349
handle custom flag on but bad config
minhkhul a967416
make patch config check readable
minhkhul c020da6
make good_patch_config check comprehensive
minhkhul 9a6130b
rewrite good_patch_config for clarity
minhkhul b8a2177
add unit tests for good_patch_config check
minhkhul a7d9443
add test_pull unit test for patching case + cleanup format
minhkhul e29e07e
split test cases + move to pytest
minhkhul 0a4bfb6
add test for multi-week patching
minhkhul 6c0abad
rename tests for clarity + restructure test_patch tests to clarify pu…
minhkhul 7078dd0
make expected issue dates explicit in test_patch_confirm_dir_structur…
minhkhul d435bf0
add log to distinguish grabbing records from socrata vs locally store…
minhkhul 8734daa
Update nssp/README.md
minhkhul 5a6f8b6
Update nssp/README.md
minhkhul 4356494
Add auto-download source backup data + update docs + test
minhkhul ca427a4
adjust custom_run flag to leave room for non-patch custom runs
minhkhul f58b068
move pull logic from run.py into pull.py
minhkhul 5e93175
logger to static
minhkhul 2d8670d
adjust unit tests
minhkhul f0335f6
more unit test adjustment
minhkhul 7e06f94
move get_source_data to pull.py + make get_source_data run when sourc…
minhkhul e678ce6
auto-remove source_dir content after finish patch run
minhkhul bc1d7a7
lint happy
minhkhul 84cba84
Update pull.py
minhkhul 742737b
Update pull.py - remove stat debug
minhkhul e13d3db
add progress log for source file download
minhkhul 9cec6ff
lint
minhkhul 5450d8b
lint
minhkhul 0a8da1e
Merge remote-tracking branch 'origin/main' into nssp_patching
minhkhul ab4b542
rewrite get_source_data to get source data from prod backup_dir + add…
minhkhul 14b4e6f
various bug fixes
minhkhul d9e1f74
remove source data from local after patch run
minhkhul ca5294b
fix tests in test_pull
minhkhul 49c4f67
Add new + fix current patch tests + fix duplicated data
minhkhul fb3fbf2
use set comprehension in get_patch_dates
minhkhul 1f5a4dc
lint
minhkhul 32f550e
make user param optional when no source data download needed
minhkhul 2214f87
linter
minhkhul 9ce6cab
Merge branch 'main' into nssp_patching
minhkhul 633399b
lint
minhkhul 83e5a8c
adjust readme
minhkhul 552d036
add test for pull source data + remove misc secondary trace
minhkhul c4dd3b2
darker linting
nmdefries a4c4aec
add tests for remote source download vs local
minhkhul 7c77be5
Merge branch 'nssp_patching' of https://github.com/cmu-delphi/covidca…
minhkhul 5f383a6
change source host to params + add if logger before use in create_bac…
minhkhul d70d6e1
lint
minhkhul 2a67c27
fix test
minhkhul 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
This module is used for patching data in the delphi_nssp package. | ||
|
||
To use this module, you need to specify the range of issue dates in params.json, like so: | ||
|
||
{ | ||
"common": { | ||
... | ||
}, | ||
"validation": { | ||
... | ||
}, | ||
"patch": { | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"patch_dir": "/Users/minhkhuele/Desktop/delphi/covidcast-indicators/nssp/AprilPatch", | ||
"start_issue": "2024-04-20", | ||
"end_issue": "2024-04-21", | ||
"source_dir": "/Users/minhkhuele/Desktop/delphi/covidcast-indicators/nssp/source_data" | ||
nmdefries marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
It will generate data for that range of issue dates, and store them in batch issue format: | ||
[name-of-patch]/issue_[issue-date]/nssp/actual_data_file.csv | ||
""" | ||
|
||
from datetime import datetime, timedelta | ||
from os import makedirs, path | ||
|
||
from delphi_utils import get_structured_logger, read_params | ||
from epiweeks import Week | ||
|
||
from .run import run_module | ||
|
||
|
||
def patch(): | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Run the nssp indicator for a range of issue dates. | ||
|
||
The range of issue dates is specified in params.json using the following keys: | ||
- "patch": Only used for patching data | ||
- "start_date": str, YYYY-MM-DD format, first issue date | ||
- "end_date": str, YYYY-MM-DD format, last issue date | ||
- "patch_dir": str, directory to write all issues output | ||
- "source_dir": str, directory to read source data from. | ||
""" | ||
params = read_params() | ||
logger = get_structured_logger("delphi_nssp.patch", filename=params["common"]["log_filename"]) | ||
|
||
start_issue = datetime.strptime(params["patch"]["start_issue"], "%Y-%m-%d") | ||
end_issue = datetime.strptime(params["patch"]["end_issue"], "%Y-%m-%d") | ||
|
||
logger.info(f"""Start patching {params["patch"]["patch_dir"]}""") | ||
logger.info(f"""Start issue: {start_issue.strftime("%Y-%m-%d")}""") | ||
logger.info(f"""End issue: {end_issue.strftime("%Y-%m-%d")}""") | ||
logger.info(f"""Source from: {params["patch"]["source_dir"]}""") | ||
logger.info(f"""Output to: {params["patch"]["patch_dir"]}""") | ||
|
||
makedirs(params["patch"]["patch_dir"], exist_ok=True) | ||
|
||
current_issue = start_issue | ||
while current_issue <= end_issue: | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.info(f"""Running issue {current_issue.strftime("%Y-%m-%d")}""") | ||
|
||
current_issue_source_csv = ( | ||
aysim319 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f"""{params.get("patch", {}).get("source_dir")}/{current_issue.strftime("%Y-%m-%d")}.csv""" | ||
) | ||
if not path.isfile(current_issue_source_csv): | ||
logger.info(f"No source data at {current_issue_source_csv}") | ||
current_issue += timedelta(days=1) | ||
continue | ||
|
||
params["patch"]["current_issue"] = current_issue.strftime("%Y-%m-%d") | ||
|
||
# current_issue_date can be different from params["patch"]["current_issue"] | ||
# due to weekly cadence of nssp data. For weekly sources, issue dates in our | ||
# db matches with first date of epiweek that the reporting date falls in, | ||
# rather than reporting date itself. | ||
current_issue_date = Week.fromdate(current_issue).startdate() | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
current_issue_dir = f"""{params["patch"]["patch_dir"]}/issue_{current_issue_date.strftime("%Y%m%d")}/nssp""" | ||
makedirs(f"{current_issue_dir}", exist_ok=True) | ||
params["common"]["export_dir"] = f"""{current_issue_dir}""" | ||
|
||
run_module(params, logger) | ||
current_issue += timedelta(days=1) | ||
|
||
|
||
if __name__ == "__main__": | ||
patch() |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
week_end,geography,county,percent_visits_combined,percent_visits_covid,percent_visits_influenza,percent_visits_rsv,percent_visits_smoothed,percent_visits_smoothed_covid,percent_visits_smoothed_1,percent_visits_smoothed_rsv,ed_trends_covid,ed_trends_influenza,ed_trends_rsv,hsa,hsa_counties,hsa_nci_id,fips,trend_source | ||
2022-10-01T00:00:00.000,United States,All,2.84,1.84,0.48,0.55,2.83,2.07,0.34,0.44,Decreasing,Increasing,Increasing,All,All,All,0,United States |
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,47 @@ | ||
import unittest | ||
from unittest.mock import patch as mock_patch, call | ||
from delphi_nssp.patch import patch | ||
import os | ||
import shutil | ||
from datetime import datetime, timedelta | ||
|
||
class TestPatchModule(unittest.TestCase): | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_patch(self): | ||
minhkhul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with mock_patch('delphi_nssp.patch.run_module') as mock_run_module, \ | ||
mock_patch('delphi_nssp.patch.get_structured_logger') as mock_get_structured_logger, \ | ||
mock_patch('delphi_nssp.patch.read_params') as mock_read_params: | ||
|
||
mock_read_params.return_value = { | ||
"common": { | ||
"log_filename": "test.log" | ||
}, | ||
"patch": { | ||
"start_issue": "2021-01-01", | ||
"end_issue": "2021-01-02", | ||
"patch_dir": "./patch_dir", | ||
"source_dir": "./source_dir" | ||
} | ||
} | ||
|
||
patch() | ||
|
||
self.assertIn('current_issue', mock_read_params.return_value['patch']) | ||
self.assertEqual(mock_read_params.return_value['patch']['current_issue'], '2021-01-02') | ||
|
||
self.assertTrue(os.path.isdir('./patch_dir')) | ||
self.assertTrue(os.path.isdir('./patch_dir/issue_20201227/nssp')) | ||
|
||
start_date = datetime(2020, 12, 28) | ||
end_date = datetime(2021, 1, 3) | ||
date = start_date | ||
|
||
while date <= end_date: | ||
date_str = date.strftime("%Y%m%d") | ||
self.assertFalse(os.path.isdir(f'./patch_dir/issue_{date_str}/nssp')) | ||
date += timedelta(days=1) | ||
|
||
# Clean up the created directories after the test | ||
shutil.rmtree(mock_read_params.return_value["patch"]["patch_dir"]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.