def packet_loss_marker(stream_id: uuid, CC_obj: CerebralCortex, config: dict, start_time=None, end_time=None):
    """
    Label a window as packet-loss if received packets are less than the expected packets.
    All the labeled data (st, et, label) with its metadata are then stored in a datastore.
    :param stream_id:
    :param CC_obj:
    :param config:
    """
    stream = CC_obj.get_datastream(stream_id, data_type=DataSet.COMPLETE, start_time=start_time, end_time=end_time)
    name = stream._name
    results = OrderedDict()

    if name == config["sensor_types"]["autosense_ecg"]:
        sampling_rate = config["sampling_rate"]["ecg"]
        threshold_val = config["packet_loss_marker"]["ecg_acceptable_packet_loss"]
        label = config["labels"]["ecg_packet_loss"]
        windowed_data = window(stream.data, config['general']['window_size'], False)
    elif name == config["sensor_types"]["autosense_rip"]:
        sampling_rate = config["sampling_rate"]["rip"]
        threshold_val = config["packet_loss_marker"]["rip_acceptable_packet_loss"]
        label = config["labels"]["rip_packet_loss"]
        windowed_data = window(stream.data, config['general']['window_size'], False)
    elif name == config["sensor_types"]["motionsense_accel"]:
        sampling_rate = config["sampling_rate"]["motionsense"]
        threshold_val = config["packet_loss_marker"]["motionsense_acceptable_packet_loss"]
        label = config["labels"]["motionsense_packet_loss"]
        motionsense_accel_magni = magnitude(stream)
        windowed_data = window(motionsense_accel_magni.data, config['general']['window_size'], False)
    else:
        raise ValueError("Incorrect sensor type.")

    for key, data in windowed_data.items():

        available_packets = len(data)
        expected_packets = sampling_rate * config['general']['window_size']

        if (available_packets / expected_packets) < threshold_val:
            results[key] = label

    merged_windows = merge_consective_windows(results)
    input_streams = [{"id": str(stream_id), "name": name}]
    store(input_streams, merged_windows, CC_obj, config, config["algo_names"]["packet_loss_marker"])
def accelerometer_features(accel: DataStream,
                           window_length: float = 10.0,
                           activity_threshold: float = 0.21,
                           percentile_low: int = 1,
                           percentile_high: int = 99) -> Tuple[DataStream, DataStream, DataStream]:
    """

    References:
        Figure 3: http://www.cs.memphis.edu/~santosh/Papers/Timing-JIT-UbiComp-2014.pdf

    :param percentile_high:
    :param percentile_low:
    :param accel:
    :param window_length:
    :param activity_threshold:
    :return:
    """
    accelerometer_magnitude = magnitude(normalize(accel))

    accelerometer_win_mag_deviations_data = []
    for key, data in window(accelerometer_magnitude.data, window_length).items():
        accelerometer_win_mag_deviations_data.append(window_std_dev(data, key[0]))

    accelerometer_win_mag_deviations = DataStream.from_datastream([accel])
    accelerometer_win_mag_deviations.data = accelerometer_win_mag_deviations_data

    am_values = np.array([dp.sample for dp in accelerometer_magnitude.data])
    low_limit = np.percentile(am_values, percentile_low)
    high_limit = np.percentile(am_values, percentile_high)
    range = high_limit - low_limit

    accel_activity_data = []
    for dp in accelerometer_win_mag_deviations_data:
        comparison = dp.sample > (low_limit + activity_threshold * range)
        accel_activity_data.append(DataPoint.from_tuple(dp.start_time, comparison))

    accel_activity = DataStream.from_datastream([accel])
    accel_activity.data = accel_activity_data

    return accelerometer_magnitude, accelerometer_win_mag_deviations, accel_activity
def wireless_disconnection(stream_id: uuid,
                           CC_obj: CerebralCortex,
                           config: dict,
                           start_time=None,
                           end_time=None):
    """
    Analyze whether a sensor was unavailable due to a wireless disconnection
    or due to sensor powered off. This method automatically loads related
    accelerometer streams of an owner. All the labeled data (st, et, label)
    with its metadata are then stored in a datastore.
    Note: If an owner owns more than one accelerometer (for example, more
    than one motionsense accelerometer) then this might not work.
    :param stream_id: stream_id should be of "battery-powered-off"
    :param CC_obj:
    :param config:
    """

    results = OrderedDict()
    threshold = 0

    stream_info = CC_obj.get_datastream(stream_id,
                                        data_type=DataSet.ONLY_METADATA,
                                        start_time=start_time,
                                        end_time=end_time)

    owner_id = stream_info._owner
    name = stream_info._name

    stream_name = stream_info._name

    if name == config["sensor_types"]["autosense_ecg"]:
        threshold = config['sensor_unavailable_marker']['ecg']
        label = config['labels']['autosense_unavailable']
    if name == config["sensor_types"]["autosense_rip"]:
        threshold = config['sensor_unavailable_marker']['rip']
        label = config['labels']['autosense_unavailable']
    elif name == config["sensor_types"]["motionsense_accel"]:
        threshold = config['sensor_unavailable_marker']['motionsense']
        label = config['labels']['motionsense_unavailable']

    battery_off_data = CC_obj.get_datastream(stream_id,
                                             data_type=DataSet.ONLY_DATA,
                                             start_time=start_time,
                                             end_time=end_time)

    if battery_off_data:
        if name == config["sensor_types"]["motionsense_accel"]:
            motionsense_accel_stream_id = CC_obj.get_stream_id_by_owner_id(
                owner_id, config["sensor_types"]["motionsense_accel"], "id")
            input_streams = [{
                "id": str(stream_id),
                "name": str(stream_name)
            }, {
                "id":
                str(motionsense_accel_stream_id),
                "name":
                config["sensor_types"]["motionsense_accel"]
            }]
        else:
            x = CC_obj.get_stream_id_by_owner_id(
                owner_id, config["sensor_types"]["autosense_accel_x"])
            y = CC_obj.get_stream_id_by_owner_id(
                owner_id, config["sensor_types"]["autosense_accel_y"])
            z = CC_obj.get_stream_id_by_owner_id(
                owner_id, config["sensor_types"]["autosense_accel_z"])
            input_streams = [{
                "id": str(stream_id),
                "name": stream_name
            }, {
                "id":
                str(x),
                "name":
                config["sensor_types"]["autosense_accel_x"]
            }, {
                "id":
                str(y),
                "name":
                config["sensor_types"]["autosense_accel_y"]
            }, {
                "id":
                str(z),
                "name":
                config["sensor_types"]["autosense_accel_z"]
            }]

        for dp in battery_off_data:
            if dp.start_time != "" and dp.end_time != "":
                # get a window prior to a battery powered off
                start_time = dp.start_time - timedelta(
                    seconds=config['general']['window_size'])
                end_time = dp.start_time
                if name == config["sensor_types"]["motionsense_accel"]:
                    motionsense_accel_xyz = CC_obj.get_datastream(
                        motionsense_accel_stream_id,
                        start_time=start_time,
                        end_time=end_time,
                        data_type=DataSet.COMPLETE)
                    magnitudeValStream = magnitude(motionsense_accel_xyz)
                    magnitudeVals = []
                    for mv in magnitudeValStream.data:
                        magnitudeVals.append(mv.sample)

                else:
                    autosense_acc_x = CC_obj.get_datastream(
                        x,
                        start_time=start_time,
                        end_time=end_time,
                        data_type=DataSet.ONLY_DATA)
                    autosense_acc_y = CC_obj.get_datastream(
                        y,
                        start_time=start_time,
                        end_time=end_time,
                        data_type=DataSet.ONLY_DATA)
                    autosense_acc_z = CC_obj.get_datastream(
                        z,
                        start_time=start_time,
                        end_time=end_time,
                        data_type=DataSet.ONLY_DATA)

                    magnitudeVals = autosense_calculate_magnitude(
                        autosense_acc_x, autosense_acc_y, autosense_acc_z)

                if np.var(magnitudeVals) > threshold:
                    key = (dp.start_time, dp.end_time)
                    results[key] = label

        merged_windows = merge_consective_windows(results)
        store(input_streams, merged_windows, CC_obj, config,
              config["algo_names"]["sensor_unavailable_marker"])
Пример #4
0
def attachment_marker(stream_id: uuid,
                      CC_obj: CerebralCortex,
                      config: dict,
                      start_time=None,
                      end_time=None):
    """
    Label sensor data as sensor-on-body, sensor-off-body, or improper-attachment.
    All the labeled data (st, et, label) with its metadata are then stored in a datastore
    :param stream_id: UUID
    :param CC_obj: CerebralCortex object
    :param config: Data diagnostics configurations
    """

    stream = CC_obj.get_datastream(stream_id,
                                   data_type=DataSet.COMPLETE,
                                   start_time=start_time,
                                   end_time=end_time)

    CC_obj.get_datastream(stream_id,
                          data_type=DataSet.COMPLETE,
                          start_time=start_time,
                          end_time=end_time)

    results = OrderedDict()
    threshold_val = None
    name = stream._name

    if name == config["sensor_types"]["autosense_ecg"]:
        threshold_val = config['attachment_marker']['ecg_on_body']
        label_on = config['labels']['ecg_on_body']
        label_off = config['labels']['ecg_off_body']
        windowed_data = window(stream.data, config['general']['window_size'],
                               False)
    elif name == config["sensor_types"]["autosense_rip"]:
        threshold_val = config['attachment_marker']['rip_on_body']
        label_on = config['labels']['rip_on_body']
        label_off = config['labels']['rip_off_body']
        windowed_data = window(stream.data, config['general']['window_size'],
                               False)
    elif name == config["sensor_types"]["motionsense_accel"]:
        threshold_val = config['attachment_marker']['motionsense_on_body']
        label_on = config['labels']['motionsense_on_body']
        label_off = config['labels']['motionsense_off_body']
        motionsense_accel_magni = magnitude(stream)
        windowed_data = window(motionsense_accel_magni.data,
                               config['general']['window_size'], False)
    else:
        raise ValueError("Incorrect sensor type.")

    for key, data in windowed_data.items():
        # remove outliers from a window data
        normal_values = outlier_detection(data)

        if stat.variance(normal_values) < threshold_val:
            results[key] = label_off
        else:
            results[key] = label_on

    merged_windows = merge_consective_windows(results)
    input_streams = [{"id": str(stream_id), "name": name}]
    store(input_streams, merged_windows, CC_obj, config,
          config["algo_names"]["attachment_marker"])
Пример #5
0
    def test_magnitude(self):
        self.assertIsInstance(self.accel, DataStream)

        m = magnitude(normalize(self.accel))
        self.assertIsInstance(m, DataStream)