bjornsing's picture
add functionality for apple data
3c0932f
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
import os
import wfdb
import shutil
import gradio as gr
from models.inception import *
SAMPLE_FREQUENCY = 512
TIME = 10
NEW_SAMPLE_FREQUENCY = 100
def load_age_model(sample_frequency,recording_time, num_leads):
cwd = os.getcwd()
weights = f"{cwd}/models/weights/model_weights_leadI_age.h5"
model = build_age_model((sample_frequency * recording_time, num_leads), 1)
model.load_weights(weights)
return model
def load_gender_model(sample_frequency,recording_time, num_leads):
cwd = os.getcwd()
weights = f"{cwd}/models/weights/model_weights_leadI_gender.h5"
model = build_gender_model((sample_frequency * recording_time, num_leads), 1)
model.load_weights(weights)
return model
age_model = load_age_model(100,10,1)
gender_model = load_gender_model(100,10,1)
ecg_path = "../../apple_health_export/electrocardiograms/"
for i in os.listdir(ecg_path):
df= pd.read_csv(os.path.join(ecg_path,i), skiprows=12, sep=";", header=None, decimal=',')
ecg = np.asarray(df[0].str.replace(',', '.').str.replace('−', '-').astype(float))
new_ecg = signal.resample(ecg[(TIME*SAMPLE_FREQUENCY):(TIME*SAMPLE_FREQUENCY*2)],TIME * NEW_SAMPLE_FREQUENCY)
# from uV to mV
new_ecg = new_ecg/1000
age_estimate = age_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
gender_prediction = gender_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
print("Estimated age: ", age_estimate)
print("Predicted gender: Male = {}, Female = {}".format((1- gender_prediction),(gender_prediction)))