def __init__(self, *args): """ Initialize ECG node. """ super().__init__(*args) # Append function handles to the metric_functions-list self.metric_functions.append(self.calculate_blinks) self.metric_functions.append(self.calculate_blinkrate) def calculate_blinks(self, x, fs=500): """ Calculate the number of blinks that occured in the specified time-window. """ blinks = blink_utilities.find_blinks(x['data'][0], fs) number_of_blinks = blink_utilities.calculate_blinks(blinks) return number_of_blinks def calculate_blinkrate(self, x, fs=500): """ Calculate the blink-rate in the specified time-window """ blinks = blink_utilities.find_blinks(x['data'][0], fs) number_of_blinks = blink_utilities.calculate_blinks(blinks) return number_of_blinks / (x['time'][0][0] - x['time'][0][-1]) # Run the node from command line if __name__ == '__main__': node = mu.midas_parse_config(BlinkNode, sys.argv) if node: node.start() node.show_ui()
#!/usr/bin/env python3 import sys from midas import utilities as mu from midas.dispatcher import Dispatcher # Run the dispatcher dp = mu.midas_parse_config(Dispatcher, sys.argv) dp.start()
from midas import utilities as mu import qe_utils # ------------------------------------------------------------------------------ # Create an Activity Node based on the Base Node # ------------------------------------------------------------------------------ class QENode(BaseNode): """ MIDAS Activity Node """ def __init__(self, *args): """ Initialize example node. """ super().__init__(*args) self.metric_functions.append(qe_utils.current_app) self.metric_functions.append(qe_utils.idle_time) self.metric_functions.append(qe_utils.net_stat_sent) self.metric_functions.append(qe_utils.net_stat_recv) self.metric_functions.append(qe_utils.system_info) # ------------------------------------------------------------------------------ # Run the node if started from the command line # ------------------------------------------------------------------------------ if __name__ == '__main__': node = mu.midas_parse_config(QENode, sys.argv) if node is not None: node.start() node.show_ui() # ------------------------------------------------------------------------------ # EOF # ------------------------------------------------------------------------------
class ECGNode(BaseNode): def __init__(self, *args): """ Initialize ECG node. """ super().__init__(*args) # Append function handles to the metric_functions-list self.metric_functions.append(self.mean_hr) self.metric_functions.append(self.rmssd) def mean_hr(self, x, fs=500): """ Calculate the average heart rate from the raw ECG signal x by first obtaining the RR-intervals using R-peak detection. """ rr = ecg_utilities.detect_r_peaks(x['data'][0], fs) return ecg_utilities.hrv_mean_hr(rr) def rmssd(self, x, fs=500): """ Calculate RMSSD from the RR-vector. """ rr = ecg_utilities.detect_r_peaks(x['data'][0], fs) return ecg_utilities.hrv_rmssd(rr) # Run the node from command line if __name__ == '__main__': node = mu.midas_parse_config(ECGNode, sys.argv) if node: node.start() node.show_ui()
#!/usr/bin/env python3 import sys from midas.node import BaseNode from midas import utilities as mu import numpy as np class LumiNode(BaseNode): def __init__(self, *args): """ Initialize example node. """ super().__init__(*args) self.metric_functions.append(self.mean_luminance) def mean_luminance(self, x): """ Returns the average luminance """ return np.mean(x['data'][0]) if __name__ == '__main__': node = mu.midas_parse_config(LumiNode, sys.argv) if node is not None: node.start() node.show_ui()
import sys from midas.node import BaseNode from midas import utilities as mu import numpy # IoT light-level processing node class IoTNode(BaseNode): def __init__(self, *args): """ Initialize ECG node. """ super().__init__(*args) # Append function handles to the metric_functions-list self.metric_functions.append(self.mean_lightlevel) def mean_lightlevel(self, x): """ Get the mean light level of the specified time-window from the sensor """ return numpy.mean(x['data'][0]) # Run the node from command line if __name__ == '__main__': node = mu.midas_parse_config(IoTNode, sys.argv) if node: node.start() node.show_ui()
Returns: c <float>: average power in frequency band f_lim1 - f_lim2 """ f, P = scipy.signal.welch(x['data'][0], fs=self.sampling_rate) c = np.mean(P[np.bitwise_and(f >= f_lim1, f <= f_lim2)]) return c # Metrics can handle multiple channels as well. If multiple channels are passed # to the metric they appear as a list under 'data' key in the input dict. This # function iterates through all the channels in the input dict. def metric_d(x): """ Normalizes the last sample of each channel given as input.""" d = [] for ch in x['data']: d.append(ch[-1] / np.max(ch)) return d # ----------------------------------------------------------------------------- # Run the node if started from the command line # ----------------------------------------------------------------------------- if __name__ == '__main__': node = mu.midas_parse_config(NodeExampleB, sys.argv) if node is not None: node.start() node.show_ui() # ----------------------------------------------------------------------------- # EOF # -----------------------------------------------------------------------------
#!/usr/bin/env python3 import sys from midas.node import BaseNode from midas import utilities as mu import numpy as np import scipy.signal class ACCNode(BaseNode): def __init__(self, *args): """ Initialize example node. """ super().__init__(*args) self.metric_functions.append(self.find_peaks) def find_peaks(self, x): """ Find peaks in the input data """ return np.mean(x['data'][0]) if __name__ == '__main__': node = mu.midas_parse_config(ACCNode, sys.argv) if node is not None: node.start() node.show_ui()
import sys from midas.node import BaseNode from midas import utilities as mu import numpy as np import scipy.signal class MyoNode(BaseNode): def __init__(self, *args): """ Initialize example node. """ super().__init__(*args) self.metric_functions.append(self.emg_power) def emg_power(self, x): """ Returns the average power of the EMG signal """ power = [] for data in x['data']: f, p = 10*np.log10(scipy.signal.welch(data, fs=50.0)) power.append(np.mean(p[np.bitwise_and(f >= 10.0, f <= 25.0)])) return power if __name__ == '__main__': node = mu.midas_parse_config(MyoNode, sys.argv) if node is not None: node.start() node.show_ui()
# Push standard deviation to 2nd secondary data channel self.push_sample_secondary(1, time_stamp, new_value2) # Sleep until next sample (note: for accurate timing you don't want # to use time.sleep) time.sleep(1.0) # Metric functions can also exist outside the class as long as they are added # to the metric_functions-list in node __init__. These "outside" functions have, # however, no access to class attributes. Note that it is also possible to # include metric functions from a completely separate module. def metric_b(x, arg1=0, arg2=0): """ Returns 'metric b'. Takes two additional arguments.""" b1 = np.max(x['data'][0]) - arg1 b2 = np.min(x['data'][0]) - arg2 b = (b1, b2) return b # ------------------------------------------------------------------------------ # Run the node if started from the command line # ------------------------------------------------------------------------------ if __name__ == '__main__': node = mu.midas_parse_config(NodeExampleA, sys.argv) if node is not None: node.start() node.show_ui() # ------------------------------------------------------------------------------ # EOF # ------------------------------------------------------------------------------
import sys from midas.node import BaseNode from midas import utilities as mu import activity_utils # ------------------------------------------------------------------------------ # Create an Activity Node based on the Base Node # ------------------------------------------------------------------------------ class ActivityNode(BaseNode): """ MIDAS Activity Node """ def __init__(self, *args): """ Initialize example node. """ super().__init__(*args) self.metric_functions.append(activity_utils.current_app) self.metric_functions.append(activity_utils.idle_time) # ------------------------------------------------------------------------------ # Run the node if started from the command line # ------------------------------------------------------------------------------ if __name__ == '__main__': node = mu.midas_parse_config(ActivityNode, sys.argv) if node is not None: node.start() node.show_ui() # ------------------------------------------------------------------------------ # EOF # ------------------------------------------------------------------------------