def test_detect_rhythm_tachy(): """.. function:: test_detect_rhythm_tachy() Test output of tachydetector when threshold is set to 140. """ bm_t = ECGMeasure(file_bool=True) bm_t.data = get_test_hr1(c=200) bm_t.detect_rhythm() output_messages = [ 'Healthy... for now', 'Healthy... for now', 'Tachycardia Detected' ] output_hr = { 'B/T': output_messages, 'time': [0, 5, 10], 'HeartRate': [60, 60, 200] } assert (bm_t.data['B/T'] == output_hr['B/T']).all()
def test_detect_rhythm_brady(): """.. function:: test_detect_rhythm_brady() Test output of bradydetector when threshold is set to 50. """ bm_b = ECGMeasure(file_bool=True) bm_b.data = get_test_hr1(a=20) bm_b.detect_rhythm() output_messages = [ 'Bradycardia Detected', 'Healthy... for now', 'Healthy... for now' ] output_hr = { 'B/T': output_messages, 'time': [0, 5, 10], 'HeartRate': [20, 60, 60] } assert (bm_b.data['B/T'] == output_hr['B/T']).all()
def average(): """.. function:: average() Provides average heart rate data """ global request_counter request_counter = request_counter + 1 try: averaging_period = request.json['averaging_period'] time = request.json['time'] voltages = request.json['voltage'] if len(time) == 0: raise Exception("No input data") if len(time) != len(voltages): raise Exception("Mismatched input data") hr_rawdata = pd.DataFrame.from_dict({ 'time': time, 'voltage': voltages }) hr_rawdata['time'] = pd.to_numeric(hr_rawdata['time'], errors='coerce') hr_rawdata['voltage'] = pd.to_numeric(hr_rawdata['voltage'], errors='coerce') hr_rawdata = hr_rawdata.dropna(axis=0, how="any") hr_rawdata = hr_rawdata[abs(hr_rawdata['voltage']) <= 300] hr_rawdata = hr_rawdata.reset_index() if len(hr_rawdata) == 0 or type(averaging_period) is not int: raise Exception("No valid input data") except Exception as e: abort(400, str(e)) try: hr = ECGMeasure(rawdata=hr_rawdata) hr.acquire_avgper(averaging_period=averaging_period) hr.hrdetector_avg() hr.detect_rhythm_avg() # as of writing, the above methods and below attribute avg_data have not been created return jsonify(averaging_period=averaging_period, time_interval=hr.avg_data['time'].tolist(), average_heart_rate=hr.avg_data['HeartRate'].tolist(), tachycardia_annotations=hr. avg_data['tachycardia_annotations'].tolist(), bradycardia_annotations=hr. avg_data['bradycardia_annotations'].tolist()) except Exception as e: abort(500)
def test_thresholdhr_unchanging(): """.. function:: test_thresholdhr_unchanging() Test that threshold is the same for all chunks of the raw data. """ thr = [] for x in range(0, 10): thr.append(0.9 * 25) thresholds = np.array(thr) chunk = 50 num_chunks = 10 biomeasure = ECGMeasure(file_bool=True, argument="test_hr.csv") # biomeasure.__hr_rawdata = get_raw_data() #print(biomeasure.__hr_rawdata) biomeasure.thresholdhr() [t, c, n] = biomeasure.data t_list = t.values.T.tolist()[0] assert (t_list == thresholds).all() assert c == chunk assert n == num_chunks
def test_hrdetector(): """.. function:: test_hrdetector() Test that hrdetector() correctly detects brady/tachycardia. """ biomeasure = ECGMeasure(file_bool=True, argument="test_hr.csv") # biomeasure.__raw_data = get_raw_data() test_hr1 = get_test_hr1() biomeasure.hrdetector() biomeasure.detect_rhythm() assert (biomeasure.data['HeartRate'] == test_hr1['HeartRate']).all() assert (biomeasure.data['B/T'] == test_hr1['B/T']).all()
def test_detect_rhythm_brady3(): """.. function:: test_detect_rhythm_brady3() Test bradydetector when threshold is set to 0. """ bm_b3 = ECGMeasure(file_bool=True) bm_b3.data = get_test_hr1() bm_b3.change_brady_threshold(brady_threshold=0) bm_b3.detect_rhythm() output_messages = [ 'Healthy... for now', 'Healthy... for now', 'Healthy... for now' ] output_hr = { 'B/T': output_messages, 'time': [0, 5, 10], 'HeartRate': [60, 60, 60] } assert (bm_b3.data['B/T'] == output_hr['B/T']).all()
def test_detect_rhythm_brady2(): """.. function:: test_detect_rhythm_brady2() Test bradydetector when threshold is set to 100. """ bm_b2 = ECGMeasure(file_bool=True) bm_b2.data = get_test_hr1() bm_b2.change_brady_threshold(brady_threshold=100) bm_b2.detect_rhythm() output_messages = [ 'Bradycardia Detected', 'Bradycardia Detected', 'Bradycardia Detected' ] output_hr = { 'B/T': output_messages, 'time': [0, 5, 10], 'HeartRate': [60, 60, 60] } assert (bm_b2.data['B/T'] == output_hr['B/T']).all()
def test_detect_rhythm_tachy3(): """.. function:: test_detect_rhythm_tachy3() Test tachydetector when threshold is set to 0. """ bm_t3 = ECGMeasure(file_bool=True) bm_t3.data = get_test_hr1() bm_t3.change_tachy_threshold(tachy_threshold=0) bm_t3.detect_rhythm() output_messages = [ 'Tachycardia Detected', 'Tachycardia Detected', 'Tachycardia Detected' ] output_hr = { 'B/T': output_messages, 'time': [0, 5, 10], 'HeartRate': [60, 60, 60] } assert (bm_t3.data['B/T'] == output_hr['B/T']).all()
def summary(): """.. function:: summary() Provides a summary of heart rate data """ global request_counter request_counter = request_counter + 1 try: time = request.json['time'] voltages = request.json['voltage'] if len(time) == 0: raise Exception("No input data") if len(time) != len(voltages): raise Exception("Mismatched input data") hr_rawdata = pd.DataFrame.from_dict({ 'time': time, 'voltage': voltages }) hr_rawdata['time'] = pd.to_numeric(hr_rawdata['time'], errors='coerce') hr_rawdata['voltage'] = pd.to_numeric(hr_rawdata['voltage'], errors='coerce') hr_rawdata = hr_rawdata.dropna(axis=0, how="any") hr_rawdata = hr_rawdata[abs(hr_rawdata['voltage']) <= 300] hr_rawdata = hr_rawdata.reset_index() if len(hr_rawdata) == 0: raise Exception("No valid input data") except Exception as e: abort(400, str(e)) try: hr = ECGMeasure(rawdata=hr_rawdata) hr.hrdetector() hr.detect_rhythm() return jsonify(time=hr.data['time'].tolist(), instantaneous_heart_rate=hr.data['HeartRate'].tolist(), tachycardia_annotations=hr. data['tachycardia_annotations'].tolist(), bradycardia_annotations=hr. data['bradycardia_annotations'].tolist()) except Exception as e: abort(500)