from hrv.filters import moving_median import numpy as np # def _moving_function(rri, order, func): # offset = int(order / 2) # filt_rri = np.array(rri.copy(), dtype=np.float64) # for i in range(offset, len(rri) - offset, 1): # filt_rri[i] = func(rri[i-offset:i+offset+1]) # return filt_rri # def moving_median(rri, order=3): # return _moving_function(rri, order, np.median) rri = open_rri('./data/test/extracted/Iris_kangxi_peyin_RRI.txt') filt_rri = moving_median(rri, order=3) results = time_domain(filt_rri) print(results) results = frequency_domain(rri=filt_rri, fs=4.0, method='welch', interp_method='cubic', detrend='linear') print(results) results = non_linear(filt_rri) print(results)
from hrv.classical import frequency_domain from hrv.utils import open_rri import csv for name in range(1, 106): rri = open_rri(str(name) + '_.txt') results = frequency_domain(rri=rri, fs=4.0, method='welch', interp_method='cubic', detrend='linear') print("For data sample: " + str(name)) print(results) output_csv_name = str(name) + '_hrvfd.csv' with open(output_csv_name, 'wb') as f: # Just use 'w' mode in 3.x w = csv.DictWriter(f, results.keys()) w.writeheader() w.writerow(results) print(str(name) + ' rri file has been written to csv')
from hrv.utils import open_rri # uses hrv package and converts dict output to csv format import csv # how to use: td specifies td analysis and nl specifies NL analysis. # change it depending on which one you want output specifier = 'td' # this script is GENERAL purpose. you need to change some of these to match the filenames for i in range(1, 55): # change range filename_header = 'nsr' + str(i).zfill(3) # change name possibly filename = filename_header + 'rri.txt' classifier = '' try: rri = open_rri(filename) except: continue if specifier == 'td': results = time_domain(rri) # output is dict elif specifier == 'nl': results = non_linear(rri) else: print("Invalid argument!") output_csv_name = filename_header + 'hrv_' + specifier + '.csv' with open(output_csv_name, 'wb') as f: # Just use 'w' mode in 3.x w = csv.DictWriter(f, results.keys()) w.writeheader()
def test_open_hrm_file(self): rri_file_name = 'tests/test_files/test_file_2.hrm' response = open_rri(rri_file_name) expected = FAKE_RRI np.testing.assert_equal(response, expected)
def test_open_rri_text_file(self): rri_file_name = 'tests/test_files/test_file_1.txt' expected = FAKE_RRI response = open_rri(rri_file_name) np.testing.assert_equal(response, expected)
def setUp(self): self.real_rri = open_rri('tests/test_files/real_rri.txt')
def test_open_rri_from_hrm_fileobj(self): rri_file_name = 'tests/test_files/test_file_2.hrm' file_obj = open(rri_file_name, 'r') expected = FAKE_RRI response = open_rri(file_obj) np.testing.assert_equal(response, expected)