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

Healthwatcher fit #31

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions spo2evaluation/evaluation/blant_altman.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def BlandAltmanPlot(Xdata, Ydata, fig_num=None):

if fig_num != None:

plt.figure(fig_num)

plt.figure()

##Set axis limits, this will account for the maximum average difference and the upper and lower
##limits of agreement, and all data points.
Expand All @@ -151,12 +152,14 @@ def BlandAltmanPlot(Xdata, Ydata, fig_num=None):
ms="8",
lw="2",
)
# Add the mean, upper and lower levels of agreement to the Bland-Altman plot.
# Add the mean, upper and lower levels of agrespo2evaluation/evaluation/blant_altman.pyement to the Bland-Altman plot.
plt.axhline(y=mean_difference, lw="2", color="k", ls="dashed")
plt.axhline(y=upper_limit, lw="2", color="k", ls="dashed")
plt.axhline(y=lower_limit, lw="2", color="k", ls="dashed")
# Horizontal axis label
plt.xlabel("Average difference", fontsize="16")
plt.xlabel("Original SPO2 value", fontsize="16")
# Vertical axis label
plt.ylabel("Difference", fontsize="16")
plt.xlim(min(Average), max(Average))
plt.show()
return (mean_difference, std_difference, upper_limit, lower_limit)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import math

import utils
from . import utils


class Camera_specs(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import math

import utils
from . import utils
import matplotlib.pyplot as plt


Expand Down
2 changes: 1 addition & 1 deletion spo2evaluation/modelling/wang_2017/wang_2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import math

import utils
from . import utils
import matplotlib.pyplot as plt
import scipy

Expand Down
4 changes: 2 additions & 2 deletions spo2evaluation/preprocessing/data_loader_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, data_path):

self.meta = pd.DataFrame([data_sample_label_id, meta], ["sample_id", "fps"]).T

self.number_of_videos = self.sample_data['sample_id'].max()
self.number_of_videos = len(self.meta.index)

# self.data = self.data.T
print(self.sample_data)
Expand Down Expand Up @@ -222,7 +222,7 @@ def load_pickle(self):
self.ground_truths_sample = pd.read_pickle(self.path_gt)
self.meta = pd.read_pickle(self.path_meta)
# print("yo\n", self.sample_data.loc[self.sample_data['sample_id'] == 1])
self.number_of_videos = self.sample_data['sample_id'].max()
self.number_of_videos = len(self.meta.index)
print("Loaded", self.number_of_videos)

def is_pickled(self) -> bool:
Expand Down
11 changes: 8 additions & 3 deletions src/evaluation/Spo2_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
sys.path.append('../..')
from spo2evaluation.evaluation import blant_altman
from argparse import ArgumentParser
import pandas as pd
import matplotlib.pyplot as plt


def main():
Expand All @@ -21,18 +23,21 @@ def main():
args = parser.parse_args()
print("Launching the evaluatio for", args.data_file)

results_df = pd.read_csv(args.data_file)


# Extract Camera data
Data_camera = []
Data_camera = results_df['spo2_pred']

# Extract GT data
Data_gt = []
Data_gt = results_df['spo2_gt']

# Do statistical estimation
least_square_slope_1, least_square_y_intercept_1, R_and_P_1 = blant_altman.Correlation(
Data_camera, Data_gt
)
mean_difference_1, std_difference_1, upper_limit_1, lower_limit_1 = blant_altman.BlandAltmanPlot(
Data_camera, Data_gt
Data_camera, Data_gt, fig_num=1
)


Expand Down
54 changes: 54 additions & 0 deletions src/fit_healthwatcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas as pd
import os

DATA_DIR = '../data/0430/community'

gts = pd.read_pickle(os.path.join(DATA_DIR, 'ground_truths_sample'))
meta = pd.read_pickle(os.path.join(DATA_DIR, 'meta'))
df = pd.read_pickle(os.path.join(DATA_DIR, 'sample_data'))

df[['mean_red', 'std_red', 'mean_green', 'std_green', 'mean_blue', 'std_blue']] = df[['mean_red', 'std_red', 'mean_green', 'std_green', 'mean_blue', 'std_blue']].apply(pd.to_numeric)

df = df.groupby(['sample_id']).mean()

y = gts['SpO2'].to_numpy().reshape(-1,1)

mean_red = df['mean_red'].to_numpy()
std_red = df['std_red'].to_numpy()
mean_blue = df['mean_blue'].to_numpy()
std_blue = df['std_blue'].to_numpy()
mean_green = df['mean_green'].to_numpy()
std_green = df['std_green'].to_numpy()

X = np.array([mean_red, std_red, mean_blue, std_blue, mean_green]).T # <-- 'new' method, linear regression on all features
# X = (std_red / mean_red) / (std_blue / mean_blue).reshape(-1,1) <-- healthwatcher's method, aggregates all into 1

split=int(len(X)*0.75)

X_train=X[:split]
X_test=X[split:]

y_train=y[:split]
y_test=y[split:]

reg = LinearRegression().fit(X_train, y_train)
print(reg.score(X, y))
print(reg.coef_)
print(reg.intercept_)

y_pred = reg.predict(X_test)

import matplotlib.pyplot as plt

plt.scatter(x=y_pred, y=y_test)
plt.xlabel('predictions')
plt.ylabel('ground truth values')
plt.show()
import time

pd.DataFrame(data={'spo2_gt': y_test.reshape(1,-1)[0], 'spo2_pred': y_pred.reshape(1,-1)[0]}).to_csv(f'../results/hw-fit-{int(time.time())}')



22 changes: 21 additions & 1 deletion src/healthwatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import spo2evaluation
from spo2evaluation.modelling.healthwatcher import healthwatcher
from spo2evaluation.preprocessing import data_loader_pandas
import pandas as pd
import time


if __name__ == "__main__":
Expand Down Expand Up @@ -35,6 +37,12 @@
print("Picling the data")
dataset.pickle_data()

predictions = {'id': [],
'spo2_gt': [],
'spo2_pred': [],
}


for i in range(dataset.number_of_videos):
sample, gt, meta = dataset.get_video(i)

Expand All @@ -48,4 +56,16 @@

spo2 = healthwatcher.health_watcher(blue, blue_std, red, red_std, fps,
smooth=False)
print(spo2)

print(gt['path'])
survey_id = gt['path'].iloc[0].split('/')[-1].split('=')[-1]


predictions['id'].append(survey_id)
predictions['spo2_gt'].append(gt['SpO2'].iloc[0])
predictions['spo2_pred'].append(spo2)

results_df = pd.DataFrame(predictions)

results_df.to_csv(f'../results/healthwatcher_{int(time.time())}.csv')

4 changes: 3 additions & 1 deletion src/wang.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cv2
from spo2evaluation.modelling import wang_2017
import sys
sys.path.append('..') # expected to be run from inside src/
from spo2evaluation.modelling.wang_2017 import wang_2017


def main():
Expand Down