aboutsummaryrefslogtreecommitdiff
path: root/tests/test_io_path_handling.py
blob: 8c140ca021b09307e667d06dd01fc8834bbd52a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""Test path handling in io.py, particularly for rd() function."""

import os
import tempfile
from pathlib import Path

import numpy as np
import pytest

from transivent.io import get_waveform_params, rd


def test_rd_with_data_path_and_absolute_filename():
    """
    Test that rd() correctly handles data_path when filename is absolute.
    
    This tests the fix for the issue where rd() would ignore data_path
    if filename was an absolute path, causing XML sidecar lookup to fail.
    """
    with tempfile.TemporaryDirectory() as tmpdir:
        # Create test data directory
        data_dir = Path(tmpdir) / "data"
        data_dir.mkdir()
        
        # Create test files
        bin_file = data_dir / "test.Wfm.bin"
        xml_file = data_dir / "test.bin"
        
        # Create a simple binary file with header
        # Header: 8 bytes (element_size=4, record_length=1000)
        # Data: 1000 float32 values
        header = np.array([4, 1000], dtype=np.uint32)
        data = np.random.randn(1000).astype(np.float32)
        
        with open(bin_file, "wb") as f:
            header.tofile(f)
            data.tofile(f)
        
        # Create XML sidecar content
        xml_content = """<?xml version="1.0" standalone="no"?>
<Database>
    <Group Name="ReferenceCurveAttributes">
        <Prop Name="Resolution" Value="5e-08"/>
        <Prop Name="ByteOrder" Value="LSB"/>
        <Prop Name="SignalFormat" Value="FLOAT"/>
        <Prop Name="SignalHardwareRecordLength" Value="1000"/>
    </Group>
</Database>"""
        
        with open(xml_file, "w") as f:
            f.write(xml_content)
        
        # Test 1: rd() with absolute filename and data_path should work
        # This is the main test for the fix
        time, signal = rd(str(bin_file.absolute()), data_path=str(data_dir))
        
        assert len(time) == 1000
        assert len(signal) == 1000
        assert time[1] - time[0] == 5e-08
        
        # Test 2: rd() with relative filename and data_path should also work
        os.chdir(tmpdir)
        time2, signal2 = rd("test.Wfm.bin", data_path="data")
        
        np.testing.assert_array_equal(time, time2)
        np.testing.assert_array_equal(signal, signal2)
        
        # Test 3: rd() without data_path should work if XML is in same dir
        time3, signal3 = rd(str(bin_file))
        
        np.testing.assert_array_equal(time, time3)
        np.testing.assert_array_equal(signal, signal3)


def test_get_waveform_params_with_data_path():
    """Test that get_waveform_params correctly uses data_path."""
    with tempfile.TemporaryDirectory() as tmpdir:
        # Create test XML file
        xml_file = Path(tmpdir) / "test.bin"
        xml_content = """<?xml version="1.0" standalone="no"?>
<Database>
    <Group Name="ReferenceCurveAttributes">
        <Prop Name="Resolution" Value="1e-07"/>
        <Prop Name="ByteOrder" Value="MSB"/>
        <Prop Name="SignalFormat" Value="INT16"/>
    </Group>
</Database>"""
        
        with open(xml_file, "w") as f:
            f.write(xml_content)
        
        # Test with data_path
        params = get_waveform_params("test.Wfm.bin", data_path=tmpdir)
        
        assert params["sampling_interval"] == 1e-07
        assert params["byte_order"] == "MSB"
        assert params["signal_format"] == "int16"


def test_rd_missing_xml_sidecar():
    """Test that rd() raises appropriate error when XML sidecar is missing."""
    with tempfile.TemporaryDirectory() as tmpdir:
        # Create only binary file, no XML
        bin_file = Path(tmpdir) / "test.Wfm.bin"
        header = np.array([4, 1000], dtype=np.uint32)
        data = np.random.randn(1000).astype(np.float32)
        
        with open(bin_file, "wb") as f:
            header.tofile(f)
            data.tofile(f)
        
        # Should raise FileNotFoundError for missing XML
        with pytest.raises(FileNotFoundError, match="XML sidecar file not found"):
            rd(str(bin_file), data_path=tmpdir)
        
        # Also should fail without data_path
        with pytest.raises(FileNotFoundError, match="XML sidecar file not found"):
            rd(str(bin_file))


if __name__ == "__main__":
    test_rd_with_data_path_and_absolute_filename()
    test_get_waveform_params_with_data_path()
    test_rd_missing_xml_sidecar()
    print("All tests passed!")