-
Notifications
You must be signed in to change notification settings - Fork 16
Doctor_visits patching code #1977
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9867c2e
add patching code
minhkhul 640b29c
add documentation
minhkhul 3b7be0a
linter
minhkhul d52af76
linter
minhkhul 1be6836
linter
minhkhul 518c7fa
linter
minhkhul 061719c
linter
minhkhul 7d95000
linter
minhkhul 03fac7d
linter
minhkhul 3bd458b
linter
minhkhul 67408e4
fix dir name
minhkhul b7f5ecb
Update get_latest_claims_name.py
minhkhul 1078eab
remove patch var, use only issue"
minhkhul 5c35211
issue -> issue_date for clarity
minhkhul f68009a
fix logger
minhkhul f12abce
unit test
minhkhul ca77d64
fix unit test
minhkhul 9a4c125
lint
minhkhul 88e14d2
Update doctor_visits/delphi_doctor_visits/patch.py
minhkhul c1d7c00
add download and get_latest_claims_name tests
minhkhul 09b0081
Update test_get_latest_claims_name.py test file name
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
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,71 @@ | ||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
This module is used for patching data in the delphi_doctor_visits package. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
To use this module, you need to specify the range of issue dates in params.json, like so: | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||
"common": { | ||||||||||||||||||||||||||||||||||
... | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
"validation": { | ||||||||||||||||||||||||||||||||||
... | ||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||
"patch": { | ||||||||||||||||||||||||||||||||||
"patch_dir": "/Users/minhkhuele/Desktop/delphi/covidcast-indicators/doctor_visits/AprilPatch", | ||||||||||||||||||||||||||||||||||
"start_issue": "2024-04-20", | ||||||||||||||||||||||||||||||||||
"end_issue": "2024-04-21" | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
It will generate data for that range of issue dates, and store them in batch issue format: | ||||||||||||||||||||||||||||||||||
[name-of-patch]/issue_[issue-date]/doctor-visits/actual_data_file.csv | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: add example output
Suggested change
|
||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
from datetime import datetime, timedelta | ||||||||||||||||||||||||||||||||||
from os import makedirs | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
from delphi_utils import get_structured_logger, read_params | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
from .run import run_module | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def patch(): | ||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
Run the doctor visits 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 | ||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
params = read_params() | ||||||||||||||||||||||||||||||||||
logger = get_structured_logger("delphi_doctor_visits.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")}""") | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
makedirs(params["patch"]["patch_dir"], exist_ok=True) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
current_issue = start_issue | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
while current_issue <= end_issue: | ||||||||||||||||||||||||||||||||||
logger.info(f"""Running issue {current_issue.strftime("%Y-%m-%d")}""") | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
params["patch"]["current_issue"] = current_issue.strftime("%Y-%m-%d") | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
current_issue_yyyymmdd = current_issue.strftime("%Y%m%d") | ||||||||||||||||||||||||||||||||||
current_issue_dir = f"""{params["patch"]["patch_dir"]}/issue_{current_issue_yyyymmdd}/doctor-visits""" | ||||||||||||||||||||||||||||||||||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from delphi_doctor_visits.download_claims_ftp_files import download | ||
|
||
class TestDownload(unittest.TestCase): | ||
@patch('delphi_doctor_visits.download_claims_ftp_files.paramiko.SSHClient') | ||
@patch('delphi_doctor_visits.download_claims_ftp_files.path.exists', return_value=False) | ||
def test_download(self, mock_exists, mock_sshclient): | ||
mock_sshclient_instance = MagicMock() | ||
mock_sshclient.return_value = mock_sshclient_instance | ||
mock_sftp = MagicMock() | ||
mock_sshclient_instance.open_sftp.return_value = mock_sftp | ||
mock_sftp.listdir_attr.return_value = [MagicMock(filename="SYNEDI_AGG_OUTPATIENT_20200207_1455CDT.csv.gz")] | ||
ftp_credentials = {"host": "test_host", "user": "test_user", "pass": "test_pass", "port": "test_port"} | ||
out_path = "./test_data/" | ||
logger = MagicMock() | ||
|
||
#case 1: download with issue_date that does not exist on ftp server | ||
download(ftp_credentials, out_path, logger, issue_date="2020-02-08") | ||
mock_sshclient_instance.connect.assert_called_once_with(ftp_credentials["host"], username=ftp_credentials["user"], password=ftp_credentials["pass"], port=ftp_credentials["port"]) | ||
mock_sftp.get.assert_not_called() | ||
|
||
# case 2: download with issue_date that exists on ftp server | ||
download(ftp_credentials, out_path, logger, issue_date="2020-02-07") | ||
mock_sftp.get.assert_called() | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,37 @@ | ||
import unittest | ||
from unittest.mock import patch as mock_patch, call | ||
from delphi_doctor_visits.patch import patch | ||
import os | ||
import shutil | ||
|
||
class TestPatchModule(unittest.TestCase): | ||
def test_patch(self): | ||
with mock_patch('delphi_doctor_visits.patch.run_module') as mock_run_module, \ | ||
mock_patch('delphi_doctor_visits.patch.get_structured_logger') as mock_get_structured_logger, \ | ||
mock_patch('delphi_doctor_visits.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" | ||
} | ||
} | ||
|
||
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_20210101/doctor-visits')) | ||
self.assertTrue(os.path.isdir('./patch_dir/issue_20210102/doctor-visits')) | ||
|
||
# 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (optional): since we're using this chunk in multiple places, consider factoring it out into a separate function. (2 duplicates is okay to leave, if we use this in a third place, we definitely should factor it out.)