Download validate_datasets.py from Fanoni/medical-ehr-imaging: direct link, hf CLI and curl.
- Browser
- Download file 9.13 kB
-
https://huggingface.co/datasets/Fanoni/medical-ehr-imaging/resolve/main/validate_datasets.py
- Command line
-
hf download hf://datasets/Fanoni/medical-ehr-imaging/validate_datasets.py
-
curl -L -o validate_datasets.py https://huggingface.co/datasets/Fanoni/medical-ehr-imaging/resolve/main/validate_datasets.py
9.13 kB
| #!/usr/bin/env python3 | |
| """ | |
| Validate downloaded medical imaging datasets. | |
| Checks file integrity, formats, and metadata. | |
| """ | |
| import os | |
| import json | |
| from pathlib import Path | |
| from datetime import datetime | |
| from collections import defaultdict | |
| import hashlib | |
| # Try to import optional libraries | |
| try: | |
| from PIL import Image | |
| PIL_AVAILABLE = True | |
| except ImportError: | |
| PIL_AVAILABLE = False | |
| try: | |
| import pydicom | |
| PYDICOM_AVAILABLE = True | |
| except ImportError: | |
| PYDICOM_AVAILABLE = False | |
| BASE_DIR = Path("/Users/dafesmith/Documents/repo/NeMo-agent/medical_ehr/data/imaging") | |
| def validate_image(filepath): | |
| """Validate an image file.""" | |
| if not PIL_AVAILABLE: | |
| return {"valid": None, "error": "PIL not available"} | |
| try: | |
| with Image.open(filepath) as img: | |
| img.verify() | |
| # Re-open to get details (verify() closes the file) | |
| with Image.open(filepath) as img: | |
| return { | |
| "valid": True, | |
| "format": img.format, | |
| "mode": img.mode, | |
| "size": img.size, | |
| "file_size": os.path.getsize(filepath) | |
| } | |
| except Exception as e: | |
| return {"valid": False, "error": str(e)} | |
| def validate_dicom(filepath): | |
| """Validate a DICOM file.""" | |
| if not PYDICOM_AVAILABLE: | |
| # Basic validation without pydicom | |
| try: | |
| with open(filepath, 'rb') as f: | |
| # Check DICOM preamble | |
| f.seek(128) | |
| magic = f.read(4) | |
| if magic == b'DICM': | |
| return { | |
| "valid": True, | |
| "method": "magic_number", | |
| "file_size": os.path.getsize(filepath) | |
| } | |
| else: | |
| # Some DICOM files don't have the preamble | |
| return { | |
| "valid": None, | |
| "method": "no_magic", | |
| "file_size": os.path.getsize(filepath), | |
| "note": "No DICM magic number (may still be valid)" | |
| } | |
| except Exception as e: | |
| return {"valid": False, "error": str(e)} | |
| try: | |
| ds = pydicom.dcmread(filepath) | |
| info = { | |
| "valid": True, | |
| "modality": str(ds.get("Modality", "Unknown")), | |
| "patient_id": "ANONYMIZED" if ds.get("PatientID") else "None", | |
| "study_date": str(ds.get("StudyDate", "Unknown")), | |
| "file_size": os.path.getsize(filepath) | |
| } | |
| if hasattr(ds, 'pixel_array'): | |
| info["has_pixel_data"] = True | |
| info["pixel_shape"] = ds.pixel_array.shape | |
| return info | |
| except Exception as e: | |
| return {"valid": False, "error": str(e)} | |
| def get_file_hash(filepath, chunk_size=8192): | |
| """Calculate MD5 hash of a file.""" | |
| hash_md5 = hashlib.md5() | |
| try: | |
| with open(filepath, "rb") as f: | |
| for chunk in iter(lambda: f.read(chunk_size), b""): | |
| hash_md5.update(chunk) | |
| return hash_md5.hexdigest() | |
| except Exception: | |
| return None | |
| def validate_directory(directory, file_types=None): | |
| """Validate all files in a directory.""" | |
| results = { | |
| "directory": str(directory), | |
| "total_files": 0, | |
| "valid_files": 0, | |
| "invalid_files": 0, | |
| "skipped_files": 0, | |
| "file_types": defaultdict(int), | |
| "total_size_bytes": 0, | |
| "errors": [], | |
| "sample_files": [] | |
| } | |
| if not directory.exists(): | |
| results["error"] = "Directory does not exist" | |
| return results | |
| files = list(directory.rglob("*")) | |
| files = [f for f in files if f.is_file()] | |
| for filepath in files: | |
| results["total_files"] += 1 | |
| suffix = filepath.suffix.lower() | |
| results["file_types"][suffix] += 1 | |
| try: | |
| file_size = os.path.getsize(filepath) | |
| results["total_size_bytes"] += file_size | |
| # Validate based on file type | |
| if suffix in ['.png', '.jpg', '.jpeg']: | |
| validation = validate_image(filepath) | |
| elif suffix in ['.dcm', '.dicom']: | |
| validation = validate_dicom(filepath) | |
| elif suffix in ['.json', '.csv']: | |
| # Metadata files - just check they're readable | |
| validation = {"valid": True, "type": "metadata"} | |
| else: | |
| validation = {"valid": None, "type": "unknown"} | |
| results["skipped_files"] += 1 | |
| continue | |
| if validation.get("valid") is True: | |
| results["valid_files"] += 1 | |
| elif validation.get("valid") is False: | |
| results["invalid_files"] += 1 | |
| results["errors"].append({ | |
| "file": str(filepath), | |
| "error": validation.get("error", "Unknown error") | |
| }) | |
| # Store sample file info (first 5 of each type) | |
| if len([s for s in results["sample_files"] if s.get("suffix") == suffix]) < 5: | |
| results["sample_files"].append({ | |
| "filename": filepath.name, | |
| "suffix": suffix, | |
| "size": file_size, | |
| **{k: v for k, v in validation.items() if k != "valid"} | |
| }) | |
| except Exception as e: | |
| results["invalid_files"] += 1 | |
| results["errors"].append({ | |
| "file": str(filepath), | |
| "error": str(e) | |
| }) | |
| results["file_types"] = dict(results["file_types"]) | |
| return results | |
| def main(): | |
| """Run validation on all datasets.""" | |
| print("="*60) | |
| print("MEDICAL IMAGING DATASET VALIDATION") | |
| print(f"Base directory: {BASE_DIR}") | |
| print(f"Date: {datetime.now().isoformat()}") | |
| print("="*60) | |
| # Define datasets to validate | |
| datasets = [ | |
| { | |
| "name": "Chest X-ray (Pneumonia)", | |
| "path": BASE_DIR / "chest_xray" / "pneumonia", | |
| "expected_types": [".png"] | |
| }, | |
| { | |
| "name": "Brain MRI (Alzheimer)", | |
| "path": BASE_DIR / "brain_mri" / "alzheimer", | |
| "expected_types": [".png"] | |
| }, | |
| { | |
| "name": "Dermatology (Skin Cancer)", | |
| "path": BASE_DIR / "dermatology" / "skin_cancer", | |
| "expected_types": [".png"] | |
| }, | |
| { | |
| "name": "DICOM Samples", | |
| "path": BASE_DIR / "dicom_samples", | |
| "expected_types": [".dcm"] | |
| } | |
| ] | |
| all_results = [] | |
| total_files = 0 | |
| total_valid = 0 | |
| total_invalid = 0 | |
| total_size = 0 | |
| for dataset in datasets: | |
| print(f"\n{'='*60}") | |
| print(f"Validating: {dataset['name']}") | |
| print(f"Path: {dataset['path']}") | |
| print("-"*60) | |
| results = validate_directory(dataset["path"]) | |
| results["dataset_name"] = dataset["name"] | |
| all_results.append(results) | |
| total_files += results["total_files"] | |
| total_valid += results["valid_files"] | |
| total_invalid += results["invalid_files"] | |
| total_size += results["total_size_bytes"] | |
| # Print summary | |
| print(f"Total files: {results['total_files']}") | |
| print(f"Valid files: {results['valid_files']}") | |
| print(f"Invalid files: {results['invalid_files']}") | |
| print(f"Skipped files: {results['skipped_files']}") | |
| print(f"File types: {results['file_types']}") | |
| print(f"Total size: {results['total_size_bytes'] / (1024*1024):.2f} MB") | |
| if results["errors"]: | |
| print(f"\nErrors ({len(results['errors'])}):") | |
| for err in results["errors"][:5]: | |
| print(f" - {err['file']}: {err['error']}") | |
| if len(results["errors"]) > 5: | |
| print(f" ... and {len(results['errors']) - 5} more") | |
| # Print overall summary | |
| print(f"\n{'='*60}") | |
| print("OVERALL VALIDATION SUMMARY") | |
| print("="*60) | |
| print(f"Total datasets: {len(datasets)}") | |
| print(f"Total files: {total_files}") | |
| print(f"Valid files: {total_valid}") | |
| print(f"Invalid files: {total_invalid}") | |
| print(f"Total size: {total_size / (1024*1024):.2f} MB") | |
| print(f"Validation rate: {(total_valid / max(total_files, 1)) * 100:.1f}%") | |
| # Save validation report | |
| report = { | |
| "validation_date": datetime.now().isoformat(), | |
| "base_directory": str(BASE_DIR), | |
| "pil_available": PIL_AVAILABLE, | |
| "pydicom_available": PYDICOM_AVAILABLE, | |
| "summary": { | |
| "total_datasets": len(datasets), | |
| "total_files": total_files, | |
| "valid_files": total_valid, | |
| "invalid_files": total_invalid, | |
| "total_size_bytes": total_size, | |
| "validation_rate_percent": round((total_valid / max(total_files, 1)) * 100, 2) | |
| }, | |
| "datasets": all_results | |
| } | |
| report_path = BASE_DIR / "validation_results.json" | |
| with open(report_path, 'w') as f: | |
| json.dump(report, f, indent=2) | |
| print(f"\nDetailed report saved to: {report_path}") | |
| return report | |
| if __name__ == "__main__": | |
| main() | |