Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class pr curves #6

Merged
merged 7 commits into from
Dec 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update docs, recreate py files
roshankern committed Dec 9, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit f7637b14c20fdadf853d299c5d002364449c25e7
5 changes: 4 additions & 1 deletion 3.evaluate_model/README.md
Original file line number Diff line number Diff line change
@@ -7,9 +7,12 @@ These predictions are saved in [model_predictions.tsv](evaluations/model_predict

We evaluate these 4 sets of predictions with a confusion matrix to see the true/false positives and negatives (see [sklearn.metrics.confusion_matrix](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html) for more details).

We also evaluate these 6 sets of predictions with [sklearn.metrics.f1_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html) to determine the final/shuffled baseline model's predictive performance on each subset.
We also evaluate these 4 sets of predictions with [sklearn.metrics.f1_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html) to determine the final/shuffled baseline model's predictive performance on each subset.
F1 score measures the models precision and recall performance for each phenotypic class.

In [class_PR_curves.ipynb](class_PR_curves.ipynb), we use [sklearn.metrics.precision_recall_curve](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_curve.html) to derive the precision-recall curves for final and shuffled baseline models on the training and testing data subsets.
The precision recall curves and their data are saved to [class_precision_recall_curves](class_precision_recall_curves/).

## Step 1: Evaluate Model

Use the commands below to evaluate the ML models:
4 changes: 2 additions & 2 deletions 3.evaluate_model/evaluate_model.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Convert notebook to python file and execute
# Convert notebooks to python file and execute
jupyter nbconvert --to python \
--FilesWriter.build_directory=scripts/nbconverted \
--execute evaluate_model.ipynb
--execute *.ipynb
80 changes: 80 additions & 0 deletions 3.evaluate_model/scripts/nbconverted/class_PR_curves.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python
# coding: utf-8

# ### Import Libraries

# In[1]:


import pandas as pd
import pathlib
from joblib import load

import sys
sys.path.append("../utils")
from split_utils import get_features_data
from train_utils import get_dataset
from evaluate_utils import class_PR_curves


# ### Load models and datasets

# In[2]:


model_dir = pathlib.Path("../2.train_model/models/")
log_reg_model_path = pathlib.Path(f"{model_dir}/log_reg_model.joblib")
log_reg_model = load(log_reg_model_path)
shuffled_baseline_log_reg_model_path = pathlib.Path(f"{model_dir}/shuffled_baseline_log_reg_model.joblib")
shuffled_baseline_log_reg_model = load(shuffled_baseline_log_reg_model_path)

# load features data from indexes and features dataframe
data_split_path = pathlib.Path("../1.split_data/indexes/data_split_indexes.tsv")
data_split_indexes = pd.read_csv(data_split_path, sep="\t", index_col=0)
features_dataframe_path = pathlib.Path("../0.download_data/data/training_data.csv.gz")
features_dataframe = get_features_data(features_dataframe_path)

train_data = get_dataset(features_dataframe, data_split_indexes, "train")
test_data = get_dataset(features_dataframe, data_split_indexes, "test")

save_dir = pathlib.Path("class_precision_recall_curves/")
save_dir.mkdir(parents=True, exist_ok=True)


# ### PR Curves for combinations of models and data subsets

# In[3]:


fig, PR_data = class_PR_curves(train_data, log_reg_model)
fig.suptitle("Precision Recall Curves for Final Model on Training Data")
fig.savefig(f"{save_dir}/final_model_train_data.png")
PR_data.to_csv(f"{save_dir}/final_model_train_data.tsv", sep="\t")


# In[4]:


fig, PR_data = class_PR_curves(train_data, shuffled_baseline_log_reg_model)
fig.suptitle("Precision Recall Curves for Shuffled Baseline Model on Training Data")
fig.savefig(f"{save_dir}/shuffled_baseline_model_train_data.png")
PR_data.to_csv(f"{save_dir}/shuffled_baseline_model_train_data.tsv", sep="\t")


# In[5]:


fig, PR_data = class_PR_curves(test_data, log_reg_model)
fig.suptitle("Precision Recall Curves for Final Model on Testing Data")
fig.savefig(f"{save_dir}/final_model_test_data.png")
PR_data.to_csv(f"{save_dir}/final_model_test_data.tsv", sep="\t")


# In[6]:


fig, PR_data = class_PR_curves(test_data, shuffled_baseline_log_reg_model)
fig.suptitle("Precision Recall Curves for Shuffled Baseline Model on Testing Data")
fig.savefig(f"{save_dir}/shuffled_baseline_model_test_data.png")
PR_data.to_csv(f"{save_dir}/shuffled_baseline_model_test_data.tsv", sep="\t")

2 changes: 1 addition & 1 deletion 3.evaluate_model/scripts/nbconverted/evaluate_model.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
import sys
sys.path.append("../utils")
from split_utils import get_features_data
from train_utils import get_dataset, get_X_y_data
from train_utils import get_dataset
from evaluate_utils import evaluate_model_cm, evaluate_model_score