Download agent/data_loader.py from Artur-Melnyk/Medical-Insight-Explorer-Agent: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/Artur-Melnyk/Medical-Insight-Explorer-Agent/resolve/main/agent/data_loader.py
- Command line
-
hf download hf://spaces/Artur-Melnyk/Medical-Insight-Explorer-Agent/agent/data_loader.py
-
curl -L -o data_loader.py https://huggingface.co/spaces/Artur-Melnyk/Medical-Insight-Explorer-Agent/resolve/main/agent/data_loader.py
2.92 kB
| from pathlib import Path | |
| from typing import Dict | |
| import pandas as pd | |
| class HealthcareDataLoader: | |
| """ | |
| Loads cleaned relational Parquet tables produced by the upstream | |
| Healthcare-Data-Cleaning project. | |
| The full Parquet files are expected locally under data/processed/ | |
| and are intentionally excluded from GitHub. | |
| """ | |
| REQUIRED_TABLES = { | |
| "train_beneficiary": "train_beneficiary_clean.parquet", | |
| "test_beneficiary": "test_beneficiary_clean.parquet", | |
| "train_inpatient": "train_inpatient_clean.parquet", | |
| "test_inpatient": "test_inpatient_clean.parquet", | |
| "train_outpatient": "train_outpatient_clean.parquet", | |
| "test_outpatient": "test_outpatient_clean.parquet", | |
| "train_labels": "train_labels_clean.parquet", | |
| "test_labels": "test_labels_clean.parquet", | |
| } | |
| def __init__(self, data_dir: str = "data/processed") -> None: | |
| self.data_dir = Path(data_dir) | |
| def validate_files_exist(self) -> None: | |
| """ | |
| Checks that all required Parquet files exist before loading. | |
| Raises FileNotFoundError with a clear message if any file is missing. | |
| """ | |
| missing_files = [] | |
| for filename in self.REQUIRED_TABLES.values(): | |
| path = self.data_dir / filename | |
| if not path.exists(): | |
| missing_files.append(str(path)) | |
| if missing_files: | |
| missing_text = "\n".join(missing_files) | |
| raise FileNotFoundError( | |
| "Missing required Parquet file(s):\n" | |
| f"{missing_text}\n\n" | |
| "Place the cleaned Parquet files in data/processed/. " | |
| "They should be generated by the upstream Healthcare-Data-Cleaning project." | |
| ) | |
| def load_tables(self) -> Dict[str, pd.DataFrame]: | |
| """ | |
| Loads all required cleaned Parquet tables into a dictionary. | |
| Returns: | |
| Dictionary where keys are logical table names and values are pandas DataFrames. | |
| """ | |
| self.validate_files_exist() | |
| loaded_tables = {} | |
| for table_name, filename in self.REQUIRED_TABLES.items(): | |
| path = self.data_dir / filename | |
| loaded_tables[table_name] = pd.read_parquet(path) | |
| return loaded_tables | |
| def get_table_shapes(self) -> pd.DataFrame: | |
| """ | |
| Loads tables and returns a compact table with row and column counts. | |
| Useful for validation and documentation. | |
| """ | |
| tables = self.load_tables() | |
| shape_rows = [] | |
| for table_name, dataframe in tables.items(): | |
| shape_rows.append( | |
| { | |
| "table_name": table_name, | |
| "rows": dataframe.shape[0], | |
| "columns": dataframe.shape[1], | |
| } | |
| ) | |
| return pd.DataFrame(shape_rows).sort_values("table_name").reset_index(drop=True) |