Example #1
0
def test_get_len_of_group():
    """Test getting the length of a TdmsGroup
    """
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    assert len(tdms_data['Group']) == 2
Example #2
0
def test_root_object_paths():
    """Test the group and channel properties for the root object"""
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    obj = tdms_data.object()
    assert obj.group is None
    assert obj.channel is None
Example #3
0
def test_group_object_paths():
    """Test the path and name properties for a group"""
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    group = tdms_data["Group"]
    assert group.path == "/'Group'"
    assert group.name == "Group"
Example #4
0
def test_group_property_read():
    """Test reading property of a group"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    group = tdms_data["Group"]
    assert group.properties["num"] == 10
Example #5
0
def test_file_as_dataframe_without_time():
    """Converting file to dataframe with time index should raise when
    time properties aren't present"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    with pytest.raises(KeyError):
        tdms_data.as_dataframe(time_index=True)
Example #6
0
def test_file_properties():
    """Test reading properties of the file (root object)"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())

    tdms_file = test_file.load()

    file_props = tdms_file.properties
    assert file_props['num'] == 15
Example #7
0
def test_channel_object_paths():
    """Test the path and name properties for a channel"""
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    channel = tdms_data["Group"]["Channel1"]
    assert channel.path == "/'Group'/'Channel1'"
    assert channel.name == "Channel1"
    assert channel.group_name == "Group"
Example #8
0
def test_key_error_getting_invalid_group():
    """Test getting a group that doesn't exist raises a KeyError
    """
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    with pytest.raises(KeyError) as exc_info:
        _ = tdms_data['non-existent group']
    assert 'non-existent group' in str(exc_info.value)
Example #9
0
def test_get_object_from_group():
    """Test passing a TdmsGroup to object returns the group"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_file = test_file.load()

    groups = tdms_file.groups()
    assert tdms_file.object(groups[0]) is groups[0]
    assert tdms_file.object(groups[0].name) is groups[0]
Example #10
0
def test_channel_object_paths():
    """Test the group and channel properties for a group"""
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    obj = tdms_data["Group"]["Channel1"]
    assert obj.path == "/'Group'/'Channel1'"
    assert obj.name == "Channel1"
    assert obj.group == "Group"
    assert obj.channel == "Channel1"
Example #11
0
def test_tdmsinfo_with_debug_output(caplog):
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    temp_file = test_file.get_tempfile(delete=False)
    try:
        temp_file.file.close()
        with patch.object(sys, 'argv',
                          ['tdmsinfo.py', temp_file.name, '--debug']):
            tdmsinfo.main()
            assert "Reading metadata for object /'Group'/'Channel1'" in caplog.text
    finally:
        os.remove(temp_file.name)
Example #12
0
def test_object_repr():
    """Test getting object representations of groups and channels
    """
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    group = tdms_data["Group"]
    assert repr(group) == "<TdmsGroup with path /'Group'>"

    channel = group["Channel1"]
    assert repr(channel) == "<TdmsChannel with path /'Group'/'Channel1'>"
Example #13
0
def test_tdmsinfo(capsys):
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    temp_file = test_file.get_tempfile(delete=False)
    try:
        temp_file.file.close()
        with patch.object(sys, 'argv', ['tdmsinfo.py', temp_file.name]):
            tdmsinfo.main()
            captured = capsys.readouterr()
            assert "/'Group'/'Channel1'" in captured.out
            assert "wf_start_offset" not in captured.out
    finally:
        os.remove(temp_file.name)
Example #14
0
def test_get_objects():
    """Test reading data"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_file = test_file.load()

    objects = tdms_file.objects
    assert len(objects) == 4
    assert "/" in objects.keys()
    assert "/'Group'" in objects.keys()
    assert "/'Group'/'Channel1'" in objects.keys()
    assert "/'Group'/'Channel2'" in objects.keys()
Example #15
0
def test_time_track():
    """Add a time track to waveform data"""

    test_file = GeneratedFile()
    (toc, metadata, data) = basic_segment()
    test_file.add_segment(toc, metadata, data)
    tdms_data = test_file.load()

    channel = tdms_data["Group"]["Channel2"]
    time = channel.time_track()
    assert len(time) == len(channel.data)
    epsilon = 1.0E-15
    assert abs(time[0]) < epsilon
    assert abs(time[1] - 0.1) < epsilon
Example #16
0
def test_channel_as_dataframe_without_time():
    """Converting channel to dataframe should work correctly"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    df = tdms_data["Group"]["Channel2"].as_dataframe()

    assert len(df.index) == 2
    assert len(df.values) == 2
    assert_within_tol(df.index[0], 0)
    assert_within_tol(df.index[1], 1)
    assert_within_tol(df.values[0], 3.0)
    assert_within_tol(df.values[1], 4.0)
Example #17
0
def test_data_read_from_bytes_io():
    """Test reading data"""

    test_file = BytesIoTestFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    data = tdms_data["Group"]["Channel1"].data
    assert len(data) == 2
    assert data[0] == 1
    assert data[1] == 2
    data = tdms_data["Group"]["Channel2"].data
    assert len(data) == 2
    assert data[0] == 3
    assert data[1] == 4
Example #18
0
def test_memmapped_read():
    """Test reading data into memmapped arrays"""

    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load(memmap_dir=tempfile.gettempdir())

    data = tdms_data["Group"]["Channel1"].data
    assert len(data) == 2
    assert data[0] == 1
    assert data[1] == 2
    data = tdms_data["Group"]["Channel2"].data
    assert len(data) == 2
    assert data[0] == 3
    assert data[1] == 4
Example #19
0
def test_hdf_properties(tmp_path):
    """ Test properties are converted to attributes in HDF files
    """
    test_file = GeneratedFile()
    test_file.add_segment(*basic_segment())
    tdms_data = test_file.load()

    h5_path = tmp_path / 'h5_properties_test.h5'
    h5 = tdms_data.as_hdf(h5_path)

    # File level properties
    assert h5.attrs['num'] == 15

    # Group properties
    assert h5['Group'].attrs['prop'] == 'value'
    assert h5['Group'].attrs['num'] == 10

    # Channel properties
    assert h5['Group']['Channel2'].attrs['wf_start_offset'] == 0.0
    assert h5['Group']['Channel2'].attrs['wf_increment'] == 0.1