Package: deepehrgraph
EHR Data Analysis and outcome prediction with Deep LearningDescriptionThis project aims at demonstring deep learning methodologies for EHR data.
The use case is to predict different outcomes for patients in the ICU.
The dataset is fromMIMIC-IV democontaining de-identified health-related data from 140 patients admitted to critical care units.InstallationInstall the latest package version with pippip3install-UdeepehrgraphCommandsDisplay available subcommands:python3-mdeepehrgraph.main--helpDatasetThe full dataset description can be foundmimic iv demo dataset.It contains de-identified health-related data from 140 patients admitted to critical care units. The data includes demographics, vital signs, laboratory tests, medications, and more. The data is stored in a relational database, and the schema is described in theMIMIC-IV documentation.Note that we only have access to thehospandicucompressed files
regarding this demo.Generate main dataset from compressed filespython-mdeepehrgraph.maindatasetThis step will download the archive files from physionet and generate the master dataset in thedatafolder by default as a csv file calledmimic_iv_demo_master_dataset.csv.Several pre-computation steps are done in order to generate this master dataset:CCI and ECI indexes are calculated and added to the dataset.Outcomes for patients are calculed and added to the date.These pre-computations have been adapted from thisrepositoryspecifically for the MIMIC-IV demo dataset.Categorical features are identified and encoded withLabelEncoder.In the context of medical studies, CCI (Charlson Comorbidity Index) and ECI (Elixhauser Comorbidity Index) are tools used to assess the burden of comorbidities in individuals.
Comorbidities refer to the presence of additional health conditions in a patient alongside the primary condition under investigation. Both CCI and ECI are designed to quantify and summarize the impact of comorbidities on patient health.These features seem to be good candidates for our prediction task.EDA : Features and outcomes analysisRun simple EDA with this command and you will get:basic information about dataset datatypesmissing values countcorrelation matrixoutcomes distributionpython-mdeepehrgraph.mainedaCorrelation MatrixHere are the 25 top correlated features:| Variable1        | Variable2        | Correlation |
|------------------|------------------|-------------|
| cci_Renal        | eci_Renal        | 1.000000    |
| cci_Rheumatic    | eci_Rheumatic    | 1.000000    |
| n_ed_365d        | n_icu_365d       | 1.000000    |
| cci_Cancer2      | eci_Tumor2       | 1.000000    |
| cci_Paralysis    | eci_Paralysis    | 1.000000    |
| n_ed_30d         | n_icu_30d        | 1.000000    |
| cci_PUD          | eci_PUD          | 1.000000    |
| n_ed_90d         | n_icu_90d        | 1.000000    |
| cci_Pulmonary    | eci_Pulmonary    | 1.000000    |
| cci_CHF          | eci_CHF          | 1.000000    |
| cci_Dementia     | cci_Paralysis    | 1.000000    |
| cci_PVD          | eci_PVD          | 1.000000    |
| cci_Dementia     | eci_Paralysis    | 1.000000    |
| cci_DM1          | eci_DM2          | 0.971825    |
| cci_Cancer1      | eci_Tumor1       | 0.949788    |
| cci_DM2          | eci_DM1          | 0.931891    |
| n_icu_90d        | n_icu_365d       | 0.927516    |
| n_ed_90d         | n_icu_365d       | 0.927516    |
| n_ed_365d        | n_icu_90d        | 0.927516    |
| n_ed_90d         | n_ed_365d        | 0.927516    |
| cci_Liver1       | eci_Liver        | 0.875261    |
| eci_HTN1         | eci_Renal        | 0.815725    |
| cci_Renal        | eci_HTN1         | 0.815725    |
| n_hosp_30d       | n_hosp_90d       | 0.807012    |
| n_ed_30d         | n_ed_90d         | 0.795026    |Some features are highly correlated which could lead to poor model performance:instability that make difficult to interpret the individual impact of each variable on the target.model instability, increased sensitivity to small changes in the dataoverfittingWe will try to address this situation by using features selection technique.Outcomes RepartitionBased on these first results we will try to predict the following outcome:in-hospital mortality.
Note that we are facing an outcome class imbalance problem which can result in poor results while trying to predict this outcome, we will need to add a pre-processing for that on the traning set.PCA for Collinear Feature ReductionThis command will run the features selection code:display cumulative explained variancedisplay correlation matrix of the PCA result featuresreturn number of features to keeppython-mdeepehrgraph.mainfeat_selectOverviewPrincipal Component Analysis (PCA) is a technique for reducing the dimensionality of your dataset while retaining most of its original information. It's particularly useful for handling collinear features and improving model performance.Understanding Cumulative VarianceExplained Variance Ratio:Each principal component explains a certain proportion of the total variance. The cumulative explained variance is the sum of these individual variances, representing the overall information retained.Choosing Components:Decide on the number of components based on the desired cumulative explained variance (e.g., 95%). This choice balances dimensionality reduction with information preservation.Cumulative Explained VarianceExpectations for Final Correlation MatrixCorrelation Among Features:The final correlation matrix of the PCA result features ideally shows reduced correlations between features. Principal components are designed to be orthogonal, minimizing multicollinearity.Near-Zero Correlations:Aim for near-zero correlations in the PCA result features, indicating that each component captures unique information.Correlation Matrix PCA37 features will be selected for training the model.This number is based on the cumulative explained variance of 95%.Model architectureTheBinaryClassificationModelis a simple neural network designed for binary classification tasks. It utilizes the PyTorch framework for building and training neural networks.InitializationThe model is initialized with the following parameters:input_size: Number of features in the input data.hidden_size: Number of neurons in the hidden layer.output_size: Number of neurons in the output layer (typically 1 for binary classification).LayersLayer 1 (self.layer1):Type: Linear (Fully Connected) LayerInput:input_sizeOutput:hidden_sizeActivation Function: None (linear transformation)ReLU Activation (self.relu):Type: Rectified Linear Unit (ReLU)Activation Function: ReLUIntroduces non-linearity to the model.Layer 2 (self.layer2):Type: Linear LayerInput:hidden_sizeOutput:output_sizeActivation Function: None (linear transformation)Sigmoid Activation (self.sigmoid):Type: Sigmoid ActivationActivation Function: SigmoidConverts the output to a probability between 0 and 1, suitable for binary classification.input_sizewill correspond to the number of previsouly selected PCA features.TrainingPytorch Lightningis used as a framework for training the model. It provides a high-level interface for PyTorch that abstracts away the boilerplate code needed for training and validation. Here are some of the features that Pytorch Lightning provides and that are used in this code:Automatic GPU/CPU device placementAutomatic training and validation loopAutomatic checkpointingAutomatic loggingIn order to run the training, you can use the following command:python-mdeepehrgraph.maintrain--max-epochs30Models are saved in:./models/checkpointsfor checkpoints./models/productionfor the best model according to validation loss metric as torchscript fileAfter the training the logs are available via tensorboard wtih the following command:tensorboard--logdir=./tb_logsHere is the implemented methodology:Features SelectionWe are using the PCA technique to reduce the number of features and address the collinearity problem.
We are keeping the 37 first components that explain 95% of the variance.Data Split and OversamplingThe dataset is split into train and validation sets with a 80/20 ratio.
The train set is oversampled to address the class imbalance problem for theoutcome_inhospital_mortality.Loss FunctionWe are using the Binary Cross Entropy Loss function for this binary classification task.OptimizerWe are using the Adam optimizer for this task with standard parameter.MetricsWe are using the following metrics to evaluate the model performance for both train and validation sets:LossAccuracyPrecisionRecallF1 ScoreResourceshttps://physionet.org/content/mimic-iv-demo/2.2/https://mimic.mit.edu/docs/iv/modules/hosp/https://mimic.mit.edu/docs/iv/modules/icu/https://www.sciencedirect.com/science/article/pii/S2352914823001089https://github.com/healthylaife/MIMIC-IV-Data-Pipeline#How-to-use-the-pipelinehttps://scikit-learn.org/stable/common_pitfalls.htmlContributingInstall dependenciespip3installpoetry
poetryinstallPre-commit hookspoetryrunpre-commitinstallRun pre-commit hooks on all filespoetryrunpre-commitrun--all-filesRun testsTox is using pre-commit hooks to run tests and linting.cddeep-ehr-graph
tox.