hrmonitor module

Heart Rate Monitor Python Module

class hrmonitor.DataHandler(file_path)

Class for importing and packaging data

csvReader(file_path)

Reads in a .csv file into a list of lists

Parameters:file_path – file path to .csv file

:return dictionary of data values

get_file_type(file_path)

Extracts the file extension from a path

Parameters:file_path – path to file
Returns:extension of file (with the dot)
remove_file_type(file_path)

Removes the file extension from a path, useful for saving in same location with different file type

Parameters:file_path – path to file
Returns:path to file without the extension
class hrmonitor.HRMonitor(file_path, time_units=1, voltage_units=1, window_size=10)

Class for processing ECG data into heart rate parameters

export_JSON(file_path)

Exports calculated attributes to a json file

Parameters:file_path – json file path to export to
get_duration()

Calculates the time duration of the ECG signal

Returns:difference between the first and last time value
get_mean_hr(window_size)

Determines heart rate (bpm) for block chunks

Parameters:window_size – size of window to determine heart rate for
Returns:numpy vector of heart rate for each block interval
get_peak_interval(data)

Determines interval between peaks using auto-correlation

Parameters:data – data interval to process into heart
Returns:tuple containing (interval size between ECG peaks in seconds, array index of interval location)
get_voltage_extremes()

Gets the min and max of the voltage signal

Returns:tuple of the (min, max) for voltage
static is_float(input)

Check if string is a float and not NaN

Parameters:input – string to check
Returns:boolean indicating if the string is a float
locate_peaks()

Locates the heart beats in the signal

Returns:numpy array with approximate locations of beats given as indices of the time array
static moving_avg(data, n)

Calculates moving average of size n.

Uses code from https://stackoverflow.com/questions/14313510/how-to-calculate-moving-average-using-numpy

Parameters:
  • data – numpy vector to sum up
  • n – size of moving average window, gets shrinkwrapped to size of vector
Returns:

moving average for vector

parse_data(data)

Validate and sanitize input data, parse valid lines as floats, also performs unit standardization

Parameters:data – list containing the input data to check
Raises:ValueError – if data is empty
Returns:tuple of numpy arrays (time [s], voltage [mV])
parse_line(line, line_num)

Parses a single line into the float tuple of (time, voltage), throws exceptions as necessary

Parameters:
  • line – list of elements to parse
  • line_num – line number in file, for tracking exceptions
Raises:

ValueError – if there are more or less than two elements per line

Returns:

float tuple of (time, voltage)

plot_data()

Plots ECG data and calculated attributes and saves it as a .png file with the same name as the input .csv.

repair_line(line, line_num)

Attempts interpolated repair of line

Parameters:
  • line – list of elements to repair
  • line_num – line number in file, for finding position in data
Raises:

RuntimeError – if unable to perform interpolation of values

Returns:

float tuple of (time, voltage) as averaged between the previous and next lines