def beat_loc_and_signal(address, begin, end): file_beat = json.load(open(address + '/beat_loc.json'))[begin:end] # raw = CsvLoader.load_dummy(1) file_raw = CsvLoader.load(address + '/record.csv')[begin:end] return file_beat, file_raw
import CsvLoader import matplotlib.pyplot as plt import numpy as np from scipy.signal import lfilter, convolve data_source = CsvLoader.load()[:600] ''' LOW PASS FILTERING ''' b=[1,0,0,0,0,0,-2,0,0,0,0,0,1] a=[1,-2,1] lowed = lfilter(b,a, data_source) ''' HIGH PASS FILTERING ''' b = [-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,32,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] a = [1,-1] highed = lfilter(b,a, lowed) ''' DERRIVIATE ''' h = [x/8. for x in [-1, -2, 0, 2, 1]] derr = convolve(highed, h)[2:] # cancel delay abss = [abs(x) for x in derr] max_abs = max(abss) derr = [x/max_abs for x in derr] '''
import CsvLoader source = CsvLoader.load() """ Delay data: 12 second (2400 sample) Sample Rate: 200/second (5ms) Using PanTom 1. import PanTom.py 2. instantiate Detector object 3. call function add_data(raw) return: check the returned[0] if ['filling']: function return 'filling', detection step is skipped, algorithm still filling buffer [Filtered Data, PVC, PAC, BUNDLE_BRANCH, AtrialTachycardia, VentricularTachycardia, BundleBranchBlock] : [0] is filtered data, send to chart via MQTT [1..6] is classification """ from PanTom import * detector = Detector() for raw in source: # detector.add_data(raw, True) # view plot detector.add_data(raw) # deployment # dat = detector.add_data(raw) # print test, return # if dat[0] != 'filling': # print dat[0], dat[1]
# Test sample # numbers = [100] # normal numbers = [106] # many PVC INDEX_LIMIT = 1000 def build_message(index, sample): next_index = 1 if index + 1 > INDEX_LIMIT else index + 1 return next_index, str(index) + ':' + str(sample) if __name__ == '__main__': client = mqtt.Client(client_id=CLIENT_ID) client.connect("localhost", 1883, 60) for number in numbers: number = 'TA-Data/MIT_BIH/' + str(number) index = 0 print(number) # raw = CsvLoader.load(number + '/record.csv')[:2935] # 8 second # raw = CsvLoader.load(number + '/record.csv')[:12000] # 1 minute raw = CsvLoader.load(number + '/record.csv')[9360:] # cut up # raw = CsvLoader.load(number + '/record.csv') # till end for data in raw: index, message = build_message(index, data) client.publish(CLIENT_ID + "/sensor", message) sleep(0.005)
# # for ecg in ecg_h: # # print ecg # # # # ax(3) = subplot(323) # # fig, (ax_orig, ax_filter) = plt.subplots(2, 1, sharex=True) # # ax_orig.plot(data_source) # # ax_orig.set_title('Original pulse') # # ax_filter.plot(ecg_h) # # ax_filter.set_title('Band Pass Filtered') # # fig.tight_layout() # # plt.show() import matplotlib.pyplot as plt import CsvLoader from PanTom import * detector = Detector() source = CsvLoader.load() for data in source: dat = detector.add_data(data, True) if dat[0]!='filling': print dat[0], dat[1] # fig, (ax_orig, ax_filter) = plt.subplots(2, 1, sharex=True) # ax_orig.plot(data_source[:200]) # ax_filter.plot(res) # plt.tight_layout() # plt.show()
import paho.mqtt.client as mqtt import CsvLoader import time from PanTom import Detector detector = Detector() CLIENT_ID = '02WXO01' data_csv = CsvLoader.load() client = mqtt.Client(client_id=CLIENT_ID) client.connect("localhost", 1883, 60) # device_id = msg.topic.split('/')[1] device_id = "02WXO01" for raw in data_csv: data = detector.add_data(float(raw)) if data[0] != 'filling': # send bpm for the first time bpm = round(float(sum(data[7])) / len(data[7]), 2) # client.publish("bpm/"+device_id, bpm, qos=2) # ini bikin errorr client.publish("bpm/" + device_id, bpm) for filtered_data in data[0]: print "forwarding data to dashboard", filtered_data # client.publish("visual/"+device_id, filtered_data, qos=2) client.publish("visual/" + device_id, filtered_data) time.sleep(0.010)