FEMR is a Python package for manipulating longitudinal EHR data for machine learning, with a focus on supporting the creation of foundation models and verifying their presumed benefits in healthcare. Such a framework is needed given the current state of large language models in healthcare and the need for better evaluation frameworks.
The currently supported foundation models are CLMBR and MOTOR.
FEMR works with data that has been converted to the MEDS schema, a simple schema that supports a wide variety of EHR / claims datasets. Please see the MEDS documentation, and in particular its provided ETLs for help converting your data to MEDS.
FEMR helps users:
- Use ontologies to better understand / featurize medical codes
- Algorithmically label patient records based on structured data
- Generate tabular features from patient timelines for use with traditional gradient boosted tree models
- Train and finetune CLMBR-derived models for binary classification and prediction tasks.
- Train and finetune MOTOR-derived models for binary classification and prediction tasks.
We recommend users start with our tutorial folder
pip install femr
# If you are using deep learning, you also need to install xformers
#
# Note that xformers has some known issues with MacOS.
# If you are using MacOS you might also need to install llvm. See https://stackoverflow.com/questions/60005176/how-to-deal-with-clang-error-unsupported-option-fopenmp-on-travis
pip install xformers
The first step of using FEMR is to convert your patient data into MEDS, the standard input format expected by FEMR codebase.
Note: FEMR currently only supports MEDS v1, so you will need to install MEDS v1 versions of packages. Aka pip install meds-etl==0.1.1
The best way to do this is with the ETLs provided by MEDS.
If you have OMOP CDM formated data, follow these instructions:
- Download your OMOP dataset to
[PATH_TO_SOURCE_OMOP]
. - Convert OMOP => MEDS using the following:
# Convert OMOP => MEDS data format
meds_etl_omop [PATH_TO_SOURCE_OMOP] [PATH_TO_OUTPUT_MEDS]
- Use HuggingFace's Datasets library to load our dataset in Python
import datasets
dataset = datasets.Dataset.from_parquet(PATH_TO_OUTPUT_MEDS + 'data/*')
# Print dataset stats
print(dataset)
>>> Dataset({
>>> features: ['patient_id', 'events'],
>>> num_rows: 6732
>>> })
# Print number of events in first patient in dataset
print(len(dataset[0]['events']))
>>> 2287
If you are using the STARR-OMOP dataset from Stanford (which uses the OMOP CDM), we add an initial Stanford-specific preprocessing step. Otherwise this should be identical to the OMOP Data section. Follow these instructions:
- Download your STARR-OMOP dataset to
[PATH_TO_SOURCE_OMOP]
. - Convert STARR-OMOP => MEDS using the following:
# Convert OMOP => MEDS data format
meds_etl_omop [PATH_TO_SOURCE_OMOP] [PATH_TO_OUTPUT_MEDS]_raw
# Apply Stanford fixes
femr_stanford_omop_fixer [PATH_TO_OUTPUT_MEDS]_raw [PATH_TO_OUTPUT_MEDS]
- Use HuggingFace's Datasets library to load our dataset in Python
import datasets
dataset = datasets.Dataset.from_parquet(PATH_TO_OUTPUT_MEDS + 'data/*')
# Print dataset stats
print(dataset)
>>> Dataset({
>>> features: ['patient_id', 'events'],
>>> num_rows: 6732
>>> })
# Print number of events in first patient in dataset
print(len(dataset[0]['events']))
>>> 2287
The following guides are for developers who want to contribute to FEMR.
Before committing, please run the following commands to ensure that your code is formatted correctly and passes all tests.
conda install pre-commit pytest -y
pre-commit install
pytest tests
pre-commit run --all-files