Markdown Converter
Agent skill for markdown-converter
Use the M4 Python API to query clinical datasets (MIMIC-IV, eICU) programmatically. Triggers on "M4 API", "query MIMIC with Python", "clinical data analysis", "EHR data", "execute SQL on MIMIC", or when writing code to access clinical databases.
Sign in to like and favorite skills
The M4 Python API provides programmatic access to clinical datasets for code execution environments. It mirrors the MCP tools but returns native Python types (DataFrames, dicts) instead of formatted strings.
Use the Python API when:
Use MCP tools when:
You must follow this sequence:
set_dataset() - Select which dataset to query (REQUIRED FIRST)get_schema() / get_table_info() - Explore available tablesexecute_query() - Run SQL queriesfrom m4 import set_dataset, get_schema, get_table_info, execute_query # Step 1: Always set dataset first set_dataset("mimic-iv") # or "mimic-iv-demo", "eicu", "mimic-iv-note" # Step 2: Explore schema schema = get_schema() print(schema['tables']) # List of table names # Step 3: Inspect specific tables before querying info = get_table_info("hosp_patients") print(info['schema']) # DataFrame with column names, types print(info['sample']) # DataFrame with sample rows # Step 4: Execute queries df = execute_query("SELECT gender, COUNT(*) as n FROM hosp_patients GROUP BY gender") # Returns pd.DataFrame - use pandas operations freely
| Function | Returns | Description |
|---|---|---|
| | Available dataset names |
| | Set active dataset (confirmation message) |
| | Get current dataset name |
| Function | Returns | Description |
|---|---|---|
| | |
| | |
| | Query results as pandas DataFrame |
| Function | Returns | Description |
|---|---|---|
| | |
| | |
| | |
M4 uses a hierarchy of exceptions. Catch specific types to handle errors appropriately:
M4Error (base) ├── DatasetError # Dataset doesn't exist or not configured ├── QueryError # SQL syntax error, table not found, query failed └── ModalityError # Tool incompatible with dataset (e.g., notes on tabular-only)
Recovery patterns:
from m4 import execute_query, set_dataset, DatasetError, QueryError, ModalityError try: df = execute_query("SELECT * FROM hosp_patients") except DatasetError as e: # No dataset selected, or dataset not found # Recovery: call set_dataset() first, or check list_datasets() set_dataset("mimic-iv") df = execute_query("SELECT * FROM hosp_patients") except QueryError as e: # SQL error or table not found # Recovery: check table name with get_schema(), fix SQL syntax print(f"Query failed: {e}") except ModalityError as e: # Tried notes function on tabular-only dataset # Recovery: switch to dataset with NOTES modality set_dataset("mimic-iv-note")
Important: Dataset selection is module-level state that persists across function calls.
set_dataset("mimic-iv") df1 = execute_query("SELECT COUNT(*) FROM hosp_patients") # Uses mimic-iv set_dataset("eicu") df2 = execute_query("SELECT COUNT(*) FROM patient") # Uses eicu
The Python API mirrors MCP tools but with better return types:
| MCP Tool | Python Function | MCP Returns | Python Returns |
|---|---|---|---|
| | Formatted string | |
| | Formatted string | with list |
| | Formatted string | with / DataFrames |
Use the Python API when you need to: