Files
synthea-alldiseases/scripts/test_module_exists.py

123 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Simple script to check if a module exists for a given disease and generate it if not.
"""
import os
import sys
import json
import subprocess
import re
# Constants
DISEASE_NAME = sys.argv[1] if len(sys.argv) > 1 else "Excessive frequent and irregular menstruation"
MODULES_DIR = "src/main/resources/modules"
RUN_MODULE_GENERATOR_PATH = "src/main/python/run_module_generator.py"
DISEASE_LIST_PATH = "src/main/resources/disease_list.json"
# Function to normalize disease name for filenames
def normalize_filename(name):
"""Convert disease name to normalized filename format."""
filename = name.lower()
filename = re.sub(r'[^a-zA-Z0-9]', '_', filename)
filename = re.sub(r'_+', '_', filename)
filename = filename.strip('_')
return filename
# Main function
def main():
print(f"Checking if module exists for: {DISEASE_NAME}")
# Normalize disease name for filename
normalized_name = normalize_filename(DISEASE_NAME)
module_path = os.path.join(MODULES_DIR, f"{normalized_name}.json")
# Check if module already exists
if os.path.exists(module_path):
print(f"✅ Module already exists at: {module_path}")
return
print(f"❌ Module not found at: {module_path}")
print(f"Creating disease list entry for {DISEASE_NAME}...")
# Create temporary disease list with just this disease
create_disease_list(DISEASE_NAME)
# Run the module generator
print(f"Running module generator for {DISEASE_NAME}...")
try:
result = subprocess.run(
[sys.executable, RUN_MODULE_GENERATOR_PATH, "--batch-size", "1"],
capture_output=True,
text=True,
check=True
)
print("Module generator output:")
print(result.stdout)
# Check if module was created
if os.path.exists(module_path):
print(f"✅ Module created successfully at: {module_path}")
else:
print(f"❌ Module generation failed, file not found at: {module_path}")
print("Error output:")
print(result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error running module generator: {e}")
print("STDOUT:")
print(e.stdout)
print("STDERR:")
print(e.stderr)
def create_disease_list(disease_name):
"""Create a temporary disease list with just the requested disease."""
disease_list = []
# Try to read original disease list if it exists
if os.path.exists(DISEASE_LIST_PATH):
try:
with open(DISEASE_LIST_PATH, 'r') as f:
original_diseases = json.load(f)
# Check if our disease already exists
name_lower = disease_name.lower()
for disease in original_diseases:
if disease.get("disease_name", "").lower() == name_lower:
disease_list.append(disease)
print(f"Found existing disease entry for '{disease_name}'")
break
else:
# Disease not found in list
disease_list.append({
"id": "", # ICD-10 code (empty as we don't have it)
"disease_name": disease_name,
"ICD-10_name": disease_name
})
print(f"Created new disease entry for '{disease_name}'")
except Exception as e:
print(f"Error reading disease list: {e}")
# Create new entry
disease_list.append({
"id": "",
"disease_name": disease_name,
"ICD-10_name": disease_name
})
else:
# Create new entry
disease_list.append({
"id": "",
"disease_name": disease_name,
"ICD-10_name": disease_name
})
print(f"Created new disease list with '{disease_name}'")
# Save disease list
os.makedirs(os.path.dirname(DISEASE_LIST_PATH), exist_ok=True)
with open(DISEASE_LIST_PATH, 'w') as f:
json.dump(disease_list, f, indent=2)
print(f"Saved disease list with {len(disease_list)} entries.")
if __name__ == "__main__":
main()