bjornsing commited on
Commit
3c0932f
1 Parent(s): a53ae2a

add functionality for apple data

Browse files
app.py CHANGED
@@ -5,14 +5,24 @@ import numpy as np
5
  import gradio as gr
6
  from models.inception import *
7
  from scipy.signal import resample
 
8
 
9
 
 
 
 
 
 
 
 
10
  def load_data(sample_data):
11
  ecg, meta_data = wfdb.rdsamp(sample_data)
12
  lead_I = ecg[:,0]
13
  sample_frequency = meta_data["fs"]
14
  return lead_I, sample_frequency
15
 
 
 
16
  def preprocess_ecg(ecg,fs):
17
  if fs != 100:
18
  ecg = resample(ecg, int(len(ecg)*(100/fs)))
@@ -43,13 +53,23 @@ def run(header_file, data_file):
43
  SAMPLE_FREQUENCY = 100
44
  TIME = 10
45
  NUM_LEADS = 1
 
46
  demo_dir = f"{CWD}/sample_data"
47
- _, hdr_basename = os.path.split(header_file.name)
48
- _, data_basename = os.path.split(data_file.name)
49
- shutil.copyfile(data_file.name, f"{demo_dir}/{data_basename}")
50
- shutil.copyfile(header_file.name, f"{demo_dir}/{hdr_basename}")
51
- data, fs = load_data(f"{demo_dir}/{hdr_basename.split('.')[0]}")
52
- ecg = preprocess_ecg(data,fs)
 
 
 
 
 
 
 
 
 
53
  age_model = load_age_model(sample_frequency=SAMPLE_FREQUENCY,recording_time=TIME,num_leads=NUM_LEADS)
54
  gender_model = load_gender_model(sample_frequency=SAMPLE_FREQUENCY,recording_time=TIME,num_leads=NUM_LEADS)
55
  age_estimate = age_model.predict(np.expand_dims(ecg,0)).ravel()[0]
@@ -66,7 +86,7 @@ with gr.Blocks() as demo:
66
  with gr.Row():
67
  with gr.Column(scale=1):
68
  header_file = gr.File(label = "header_file", file_types=[".hea"],)
69
- data_file = gr.File(label = "data_file", file_types=[".dat"])
70
  with gr.Column(scale=1):
71
  output_age = gr.Textbox(label = "Estimated age")
72
  output_gender = gr.Label( label = "Predicted gender")
 
5
  import gradio as gr
6
  from models.inception import *
7
  from scipy.signal import resample
8
+ import pandas as pd
9
 
10
 
11
+
12
+ def load_apple_data():
13
+ return None
14
+
15
+
16
+
17
+ # load wfdb data
18
  def load_data(sample_data):
19
  ecg, meta_data = wfdb.rdsamp(sample_data)
20
  lead_I = ecg[:,0]
21
  sample_frequency = meta_data["fs"]
22
  return lead_I, sample_frequency
23
 
24
+
25
+
26
  def preprocess_ecg(ecg,fs):
27
  if fs != 100:
28
  ecg = resample(ecg, int(len(ecg)*(100/fs)))
 
53
  SAMPLE_FREQUENCY = 100
54
  TIME = 10
55
  NUM_LEADS = 1
56
+ NEW_SAMPLE_FREQUENCY = 100
57
  demo_dir = f"{CWD}/sample_data"
58
+
59
+ if data_file.name.endswith(".csv"):
60
+ _, data_basename = os.path.split(data_file.name)
61
+ shutil.copyfile(data_file.name, f"{demo_dir}/{data_basename}")
62
+ df= pd.read_csv(f"{demo_dir}/{data_basename}", skiprows=12, sep=";", header=None, decimal=',')
63
+ ecg = np.asarray(df[0].str.replace(',', '.').str.replace('−', '-').astype(float))
64
+ ecg = resample(ecg[(TIME*SAMPLE_FREQUENCY):(TIME*SAMPLE_FREQUENCY*2)],TIME * NEW_SAMPLE_FREQUENCY)
65
+ ecg = ecg/1000
66
+ else:
67
+ _, hdr_basename = os.path.split(header_file.name)
68
+ _, data_basename = os.path.split(data_file.name)
69
+ shutil.copyfile(data_file.name, f"{demo_dir}/{data_basename}")
70
+ shutil.copyfile(header_file.name, f"{demo_dir}/{hdr_basename}")
71
+ data, fs = load_data(f"{demo_dir}/{hdr_basename.split('.')[0]}")
72
+ ecg = preprocess_ecg(data,fs)
73
  age_model = load_age_model(sample_frequency=SAMPLE_FREQUENCY,recording_time=TIME,num_leads=NUM_LEADS)
74
  gender_model = load_gender_model(sample_frequency=SAMPLE_FREQUENCY,recording_time=TIME,num_leads=NUM_LEADS)
75
  age_estimate = age_model.predict(np.expand_dims(ecg,0)).ravel()[0]
 
86
  with gr.Row():
87
  with gr.Column(scale=1):
88
  header_file = gr.File(label = "header_file", file_types=[".hea"],)
89
+ data_file = gr.File(label = "data_file", file_types=[".dat",".csv"])
90
  with gr.Column(scale=1):
91
  output_age = gr.Textbox(label = "Estimated age")
92
  output_gender = gr.Label( label = "Predicted gender")
apple_test.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ from scipy import signal
5
+ import os
6
+ import wfdb
7
+ import shutil
8
+ import gradio as gr
9
+ from models.inception import *
10
+
11
+ SAMPLE_FREQUENCY = 512
12
+ TIME = 10
13
+ NEW_SAMPLE_FREQUENCY = 100
14
+
15
+ def load_age_model(sample_frequency,recording_time, num_leads):
16
+ cwd = os.getcwd()
17
+ weights = f"{cwd}/models/weights/model_weights_leadI_age.h5"
18
+ model = build_age_model((sample_frequency * recording_time, num_leads), 1)
19
+ model.load_weights(weights)
20
+ return model
21
+
22
+ def load_gender_model(sample_frequency,recording_time, num_leads):
23
+ cwd = os.getcwd()
24
+ weights = f"{cwd}/models/weights/model_weights_leadI_gender.h5"
25
+ model = build_gender_model((sample_frequency * recording_time, num_leads), 1)
26
+ model.load_weights(weights)
27
+ return model
28
+
29
+ age_model = load_age_model(100,10,1)
30
+ gender_model = load_gender_model(100,10,1)
31
+ ecg_path = "../../apple_health_export/electrocardiograms/"
32
+ for i in os.listdir(ecg_path):
33
+ df= pd.read_csv(os.path.join(ecg_path,i), skiprows=12, sep=";", header=None, decimal=',')
34
+ ecg = np.asarray(df[0].str.replace(',', '.').str.replace('−', '-').astype(float))
35
+ new_ecg = signal.resample(ecg[(TIME*SAMPLE_FREQUENCY):(TIME*SAMPLE_FREQUENCY*2)],TIME * NEW_SAMPLE_FREQUENCY)
36
+ # from uV to mV
37
+ new_ecg = new_ecg/1000
38
+ age_estimate = age_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
39
+ gender_prediction = gender_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
40
+ print("Estimated age: ", age_estimate)
41
+ print("Predicted gender: Male = {}, Female = {}".format((1- gender_prediction),(gender_prediction)))
import_apple_data.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
models/__pycache__/inception.cpython-38.pyc CHANGED
Binary files a/models/__pycache__/inception.cpython-38.pyc and b/models/__pycache__/inception.cpython-38.pyc differ
 
sample_data/ecg_2021-10-16.csv ADDED
The diff for this file is too large to render. See raw diff
 
sample_data/ecg_2021-10-17_1.csv ADDED
The diff for this file is too large to render. See raw diff