Example #1
0
def _get_time_from_ns(nanoseconds: int) -> Optional[Timestamp]:
    """Given epoch nanoseconds, split into epoch milliseconds and remaining
    nanoseconds"""
    if not nanoseconds:
        return None
    ts = Timestamp()
    # pylint: disable=no-member
    ts.FromNanoseconds(nanoseconds)
    return ts
Example #2
0
def proto_timestamp_from_time_ns(time_ns):
    """Converts datetime to protobuf timestamp.

    Args:
        time_ns: Time in nanoseconds

    Returns:
        Returns protobuf timestamp.
    """
    ts = Timestamp()
    if time_ns is not None:
        # pylint: disable=no-member
        ts.FromNanoseconds(time_ns)
    return ts
Example #3
0
def test_write_read():
    """Test writing a data to a file, and reading it back."""
    file_annotations = {'robot': 'spot', 'individual': 'spot-BD-99990001'}
    channel_annotations = {'ccc': '3', 'd': '4444'}
    filename = os.path.join(tempfile.gettempdir(), 'test1.bdf')
    series1_type = 'bosdyn/test/1'
    series1_spec = {'channel': 'channel_a'}
    series1_content_type = 'text/plain'
    series1_additional_indexes = ['idxa', 'idxb']
    timestamp_nsec = now_nsec()
    msg_data = b'This is some data'
    operator_message = LogAnnotationOperatorMessage(message="End of test",
                                                    timestamp=now_timestamp())
    pod_series_type = 'bosdyn/test/pod'
    pod_spec = {'varname': 'test_var'}

    # Test writing the file.
    with open(filename, 'wb') as outfile, \
         DataWriter(outfile, annotations=file_annotations) as data_writer:
        # Write generic message data to the file.
        series1_index = data_writer.add_message_series(
            series1_type,
            series1_spec,
            series1_content_type,
            'test_type',
            annotations=channel_annotations,
            additional_index_names=series1_additional_indexes)
        data_writer.write_data(series1_index, timestamp_nsec, msg_data, [1, 2])

        # Write a protobuf to the file.
        proto_writer = ProtoSeriesWriter(data_writer,
                                         LogAnnotationOperatorMessage)
        proto_writer.write(timestamp_to_nsec(operator_message.timestamp),
                           operator_message)

        # Write POD data (floats) to the file.
        pod_writer = PodSeriesWriter(data_writer,
                                     pod_series_type,
                                     pod_spec,
                                     bddf.TYPE_FLOAT32,
                                     annotations={'units': 'm/s^2'})
        for val in range(10, 20):
            pod_writer.write(timestamp_nsec, val)

    # Test reading the file.
    with open(filename, 'rb') as infile, DataReader(infile) as data_reader:
        # Check the file version number.
        assert data_reader.version.major_version == 1
        assert data_reader.version.minor_version == 0
        assert data_reader.version.patch_level == 0
        assert data_reader.annotations == file_annotations

        expected_timestamp = Timestamp()
        expected_timestamp.FromNanoseconds(timestamp_nsec)

        assert data_reader.series_block_index(
            0).block_entries[0].timestamp == expected_timestamp
        assert data_reader.series_block_index(
            0).block_entries[0].additional_indexes[0] == 1
        assert data_reader.series_block_index(
            0).block_entries[0].additional_indexes[1] == 2

        # Check that there are 3 series in the file.
        assert len(data_reader.file_index.series_identifiers) == 3

        # Read generic message data from the file.
        series_a_index = data_reader.series_spec_to_index(series1_spec)
        assert data_reader.num_data_blocks(series_a_index) == 1
        assert data_reader.total_bytes(series_a_index) == len(msg_data)
        _desc, timestamp_, data_ = data_reader.read(series_a_index, 0)
        assert timestamp_ == timestamp_nsec
        assert data_ == msg_data
        assert _desc.timestamp == expected_timestamp
        assert _desc.additional_indexes[0] == 1
        assert _desc.additional_indexes[1] == 2

        # Read a protobuf from the file.
        proto_reader = ProtobufReader(data_reader)
        operator_message_reader = ProtobufChannelReader(
            proto_reader, LogAnnotationOperatorMessage)
        assert operator_message_reader.num_messages == 1
        timestamp_, protobuf = operator_message_reader.get_message(0)
        assert protobuf == operator_message
        assert timestamp_ == timestamp_to_nsec(operator_message.timestamp)

        # Read POD (float) data from the file.
        with pytest.raises(ValueError):
            pod_reader = PodSeriesReader(data_reader, {'spec': 'bogus'})
        pod_reader = PodSeriesReader(data_reader, pod_spec)
        assert pod_reader.pod_type.pod_type == bddf.TYPE_FLOAT32
        assert pod_reader.series_descriptor.annotations['units'] == 'm/s^2'
        assert pod_reader.num_data_blocks == 1

        timestamp_, samples = pod_reader.read_samples(0)
        assert timestamp_ == timestamp_nsec
        assert samples == [float(val) for val in range(10, 20)]

    with open(filename,
              'rb') as infile, StreamDataReader(infile) as data_reader:
        # Check the file version number.
        assert data_reader.version.major_version == 1
        assert data_reader.version.minor_version == 0
        assert data_reader.version.patch_level == 0
        assert data_reader.annotations == file_annotations

        desc_, sdesc_, data_ = data_reader.read_data_block()
        assert desc_.timestamp == expected_timestamp
        assert desc_.additional_indexes[0] == 1
        assert desc_.additional_indexes[1] == 2
        assert sdesc_.message_type.content_type == series1_content_type
        assert sdesc_.message_type.type_name == 'test_type'
        assert data_ == msg_data

        desc_, sdesc_, data_ = data_reader.read_data_block()
        assert desc_.timestamp == operator_message.timestamp
        assert sdesc_.message_type.content_type == 'application/protobuf'
        assert sdesc_.message_type.type_name == LogAnnotationOperatorMessage.DESCRIPTOR.full_name
        dec_msg = LogAnnotationOperatorMessage()
        dec_msg.ParseFromString(data_)
        assert dec_msg == operator_message

        desc_, sdesc_, data_ = data_reader.read_data_block()
        assert desc_.timestamp == expected_timestamp
        assert sdesc_.pod_type.pod_type == bddf.TYPE_FLOAT32

        assert not data_reader.eof

        with pytest.raises(EOFError):
            data_reader.read_data_block()

        assert data_reader.eof

        # Check that there are 3 series in the file.
        assert len(data_reader.file_index.series_identifiers) == 3

        assert data_reader.series_block_indexes[0].block_entries[
            0].timestamp == expected_timestamp
        assert data_reader.series_block_index(
            0).block_entries[0].additional_indexes[0] == 1
        assert data_reader.series_block_index(
            0).block_entries[0].additional_indexes[1] == 2

        assert (data_reader.file_index.series_identifiers ==
                data_reader.stream_file_index.series_identifiers)

    os.unlink(filename)