| |
| """Simple serial synthetic image generation - 0001 style only.""" |
|
|
| import os |
| import sys |
| import subprocess |
| from pathlib import Path |
| from tqdm import tqdm |
|
|
| |
| ECGKIT_DIR = '/home/azureuser/ecg-digitization/ecg-image-kit/codes/ecg-image-generator' |
| PTBXL_DIR = '/data/ecg-digitization/ptbxl/physionet.org/files/ptb-xl/1.0.3/records500' |
| OUTPUT_DIR = '/data/ecg-digitization/synthetic_0001' |
|
|
| |
| os.makedirs(f'{OUTPUT_DIR}/raw', exist_ok=True) |
| os.makedirs(f'{OUTPUT_DIR}/gt', exist_ok=True) |
|
|
| |
| os.environ['MPLBACKEND'] = 'Agg' |
| os.environ['MPLCONFIGDIR'] = f'{OUTPUT_DIR}/mplconfig' |
| os.makedirs(os.environ['MPLCONFIGDIR'], exist_ok=True) |
|
|
| |
| hea_files = sorted(Path(PTBXL_DIR).rglob('*.hea')) |
| print(f"Found {len(hea_files)} records") |
|
|
| |
| existing = set(p.stem.replace('syn_', '').replace('-0001', '') for p in Path(f'{OUTPUT_DIR}/raw').glob('*.png')) |
| print(f"Already generated: {len(existing)}") |
|
|
| |
| for hea_path in tqdm(hea_files): |
| record_id = hea_path.stem |
| |
| |
| if record_id in existing: |
| continue |
| |
| input_file = str(hea_path.with_suffix('')) |
| output_file = f'{OUTPUT_DIR}/raw/syn_{record_id}-0001.png' |
| |
| |
| cmd = [ |
| 'python', f'{ECGKIT_DIR}/gen_ecg_image_from_data.py', |
| '-i', input_file, |
| '-o', output_file, |
| '--input_directory', '', |
| '--output_directory', '', |
| '-r', '200', |
| '--pad_inches', '0', |
| '--print_header', 'False', |
| '--standard_grid_color', '5', |
| '-se', '10', |
| '--store_config', '2', |
| ] |
| |
| try: |
| result = subprocess.run( |
| cmd, |
| cwd=ECGKIT_DIR, |
| capture_output=True, |
| text=True, |
| timeout=120, |
| env={**os.environ, 'MPLBACKEND': 'Agg'} |
| ) |
| if result.returncode != 0: |
| print(f"Error {record_id}: {result.stderr[:200]}") |
| except subprocess.TimeoutExpired: |
| print(f"Timeout {record_id}") |
| except Exception as e: |
| print(f"Exception {record_id}: {e}") |
|
|
| print("Done!") |
|
|