-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Max Ratings API: Updates from eCFR for digestive systems. (#3049)
- Loading branch information
Showing
13 changed files
with
865 additions
and
750 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Ignore everything in the directory | ||
* | ||
|
||
# But don't ignore data_consolidator | ||
!dc_lookup_table_updater.py | ||
!README.md |
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,23 @@ | ||
# Diagnostic Code Lookup Table Updater | ||
|
||
## Overview | ||
|
||
The `dc_lookup_table_updater.py` script is used to update the Diagnostic Code Lookup Table in the database. The script reads a CSV file containing the new data | ||
and updates the database with the new data. The script also logs the changes made to the database. | ||
|
||
# Using the dc_lookup_table_updater.py script | ||
|
||
1. Prepare the new data CSV file and add it to this directory. The CSV file must have the following columns: | ||
* Diagnostic Code **(Required)** | ||
* Rated Issue Name | ||
* Max Rating | ||
* Body System | ||
* Category | ||
* Subcategory | ||
* CFR Reference | ||
Preparation includes removing any extra punctuation in the `Rated Issue Name` column. | ||
|
||
2. Run the script with the following command from the directory `domain-ee/ee-max-cfi-app/scripts` | ||
``` | ||
python dc_lookup_table_updater.py <new_data_csv_file>.csv | ||
``` |
77 changes: 77 additions & 0 deletions
77
domain-ee/ee-max-cfi-app/scripts/dc_lookup_table_updater.py
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,77 @@ | ||
import csv | ||
import sys | ||
from typing import Any | ||
|
||
TABLE_VERSION_PY = '../src/python_src/util/data/table_version.py' | ||
DATA_FILE = '../src/python_src/util/data/Diagnostic Code Lookup Table.csv' | ||
|
||
|
||
def consolidate_data_files(new_data_file: str) -> dict[int, tuple[Any, Any, Any, Any, Any, Any]] | None: | ||
original_data = import_file(DATA_FILE) | ||
data_map: dict[int, tuple[Any, int, Any, Any, Any, Any]] = original_data.copy() | ||
|
||
imported_data = import_file(new_data_file) | ||
updates = imported_data & data_map.keys() | ||
adds = updates ^ imported_data.keys() | ||
msg = f'Loading {len(imported_data)} ratings from "{new_data_file}" | Adds {len(adds)} | Potential Updates {len(updates)}' | ||
print(msg) | ||
|
||
data_map.update(imported_data) | ||
|
||
new_data = dict(sorted(data_map.items())) | ||
|
||
if new_data == original_data: | ||
print('No changes detected') | ||
return None | ||
|
||
return new_data | ||
|
||
|
||
def import_file(filename: str) -> dict[int, tuple[Any, Any, Any, Any, Any, Any]]: | ||
diagnostic_code_to_data: dict[int, tuple[Any, Any, Any, Any, Any, Any]] = {} | ||
with open(filename, 'r') as file: | ||
csv_reader = csv.reader(file) | ||
for index, csv_line in enumerate(csv_reader): | ||
if index == 0: | ||
continue | ||
|
||
if len(csv_line) != 7: | ||
raise ValueError(f'Invalid CSV line at index {index}') | ||
|
||
diagnostic_code_str, rated_issue_name, max_rating_str, body_system, category, subcategory, cfr_ref = csv_line | ||
|
||
try: | ||
diagnostic_code = int(diagnostic_code_str) | ||
except ValueError: | ||
raise ValueError(f'Invalid diagnostic code at index {index}: \n{csv_line}') | ||
|
||
diagnostic_code_to_data[diagnostic_code] = (rated_issue_name, max_rating_str, body_system, category, subcategory, cfr_ref) | ||
|
||
return diagnostic_code_to_data | ||
|
||
|
||
def export_data(data: dict[int, tuple[Any, int, Any, Any, Any, Any]]): | ||
with open(DATA_FILE, 'w') as file: | ||
csv_writer = csv.writer(file) | ||
csv_writer.writerow(['Diagnostic Code', 'Rated Issue Name', 'Max Rating', 'Body System', 'Category', 'Subcategory', 'CFR Reference']) | ||
for diagnostic_code, data_tuple in data.items(): | ||
csv_writer.writerow([diagnostic_code, *data_tuple]) | ||
|
||
|
||
def increment_table_version(): | ||
with open(TABLE_VERSION_PY, 'r') as file: | ||
version = int(file.readline().strip().split(' = ').pop()) | ||
with open(TABLE_VERSION_PY, 'w') as file: | ||
file.write(f'TABLE_VERSION = {version + 1}\n') | ||
file.write('# auto-generated by dc_lookup_table_updater.py\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
new_data_file = sys.argv[1] | ||
print(new_data_file) | ||
data = consolidate_data_files(new_data_file) | ||
if data: | ||
export_data(data) | ||
print('Data exported') | ||
increment_table_version() | ||
print('Table version incremented') |
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
Oops, something went wrong.