Spaces:
Sleeping
Sleeping
devjas1
commited on
Commit
·
846d13f
1
Parent(s):
88e8763
(TESTS)[Multi File]: Add unit tests for multi-format spectrum parsing
Browse files- Created comprehensive tests for spectrum parsing from TXT, CSV, and JSON file formats.
- Validates correct extraction of wavenumber and intensity values from various data layouts.
- Covers error handling for malformed files, verifies format detection logic, and ensures compatibility with new multi-format upload pipeline.
- Protects against regression and parsing failures as input support expands.
- tests/test_multi_format.py +218 -0
tests/test_multi_format.py
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for multi-format file parsing functionality."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
import numpy as np
|
| 5 |
+
from utils.multifile import (
|
| 6 |
+
parse_spectrum_data,
|
| 7 |
+
detect_file_format,
|
| 8 |
+
parse_json_spectrum,
|
| 9 |
+
parse_csv_spectrum,
|
| 10 |
+
parse_txt_spectrum,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_detect_file_format():
|
| 15 |
+
"""Test automatic file format detection."""
|
| 16 |
+
# JSON detection
|
| 17 |
+
json_content = '{"wavenumbers": [1, 2, 3], "intensities": [0.1, 0.2, 0.3]}'
|
| 18 |
+
assert detect_file_format("test.json", json_content) == "json"
|
| 19 |
+
|
| 20 |
+
# CSV detection
|
| 21 |
+
csv_content = "wavenumber,intensity\n1000,0.5\n1001,0.6"
|
| 22 |
+
assert detect_file_format("test.csv", csv_content) == "csv"
|
| 23 |
+
|
| 24 |
+
# TXT detection (default)
|
| 25 |
+
txt_content = "1000 0.5\n1001 0.6"
|
| 26 |
+
assert detect_file_format("test.txt", txt_content) == "txt"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_parse_json_spectrum():
|
| 30 |
+
"""Test JSON spectrum parsing."""
|
| 31 |
+
# Test object format
|
| 32 |
+
json_content = '{"wavenumbers": [1000, 1001, 1002], "intensities": [0.1, 0.2, 0.3]}'
|
| 33 |
+
x, y = parse_json_spectrum(json_content)
|
| 34 |
+
|
| 35 |
+
expected_x = np.array([1000, 1001, 1002])
|
| 36 |
+
expected_y = np.array([0.1, 0.2, 0.3])
|
| 37 |
+
|
| 38 |
+
np.testing.assert_array_equal(x, expected_x)
|
| 39 |
+
np.testing.assert_array_equal(y, expected_y)
|
| 40 |
+
|
| 41 |
+
# Test alternative key names
|
| 42 |
+
json_content_alt = '{"x": [1000, 1001, 1002], "y": [0.1, 0.2, 0.3]}'
|
| 43 |
+
x_alt, y_alt = parse_json_spectrum(json_content_alt)
|
| 44 |
+
np.testing.assert_array_equal(x_alt, expected_x)
|
| 45 |
+
np.testing.assert_array_equal(y_alt, expected_y)
|
| 46 |
+
|
| 47 |
+
# Test array of objects format
|
| 48 |
+
json_array = """[
|
| 49 |
+
{"wavenumber": 1000, "intensity": 0.1},
|
| 50 |
+
{"wavenumber": 1001, "intensity": 0.2},
|
| 51 |
+
{"wavenumber": 1002, "intensity": 0.3}
|
| 52 |
+
]"""
|
| 53 |
+
x_arr, y_arr = parse_json_spectrum(json_array)
|
| 54 |
+
np.testing.assert_array_equal(x_arr, expected_x)
|
| 55 |
+
np.testing.assert_array_equal(y_arr, expected_y)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_parse_csv_spectrum():
|
| 59 |
+
"""Test CSV spectrum parsing."""
|
| 60 |
+
# Test with headers
|
| 61 |
+
csv_with_headers = """wavenumber,intensity
|
| 62 |
+
1000,0.1
|
| 63 |
+
1001,0.2
|
| 64 |
+
1002,0.3
|
| 65 |
+
1003,0.4
|
| 66 |
+
1004,0.5
|
| 67 |
+
1005,0.6
|
| 68 |
+
1006,0.7
|
| 69 |
+
1007,0.8
|
| 70 |
+
1008,0.9
|
| 71 |
+
1009,1.0
|
| 72 |
+
1010,1.1
|
| 73 |
+
1011,1.2"""
|
| 74 |
+
|
| 75 |
+
x, y = parse_csv_spectrum(csv_with_headers)
|
| 76 |
+
expected_x = np.array(
|
| 77 |
+
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011]
|
| 78 |
+
)
|
| 79 |
+
expected_y = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2])
|
| 80 |
+
|
| 81 |
+
np.testing.assert_array_equal(x, expected_x)
|
| 82 |
+
np.testing.assert_array_equal(y, expected_y)
|
| 83 |
+
|
| 84 |
+
# Test without headers
|
| 85 |
+
csv_no_headers = """1000,0.1
|
| 86 |
+
1001,0.2
|
| 87 |
+
1002,0.3
|
| 88 |
+
1003,0.4
|
| 89 |
+
1004,0.5
|
| 90 |
+
1005,0.6
|
| 91 |
+
1006,0.7
|
| 92 |
+
1007,0.8
|
| 93 |
+
1008,0.9
|
| 94 |
+
1009,1.0
|
| 95 |
+
1010,1.1
|
| 96 |
+
1011,1.2"""
|
| 97 |
+
|
| 98 |
+
x_no_h, y_no_h = parse_csv_spectrum(csv_no_headers)
|
| 99 |
+
np.testing.assert_array_equal(x_no_h, expected_x)
|
| 100 |
+
np.testing.assert_array_equal(y_no_h, expected_y)
|
| 101 |
+
|
| 102 |
+
# Test semicolon delimiter
|
| 103 |
+
csv_semicolon = """1000;0.1
|
| 104 |
+
1001;0.2
|
| 105 |
+
1002;0.3
|
| 106 |
+
1003;0.4
|
| 107 |
+
1004;0.5
|
| 108 |
+
1005;0.6
|
| 109 |
+
1006;0.7
|
| 110 |
+
1007;0.8
|
| 111 |
+
1008;0.9
|
| 112 |
+
1009;1.0
|
| 113 |
+
1010;1.1
|
| 114 |
+
1011;1.2"""
|
| 115 |
+
|
| 116 |
+
x_semi, y_semi = parse_csv_spectrum(csv_semicolon)
|
| 117 |
+
np.testing.assert_array_equal(x_semi, expected_x)
|
| 118 |
+
np.testing.assert_array_equal(y_semi, expected_y)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_parse_txt_spectrum():
|
| 122 |
+
"""Test TXT spectrum parsing."""
|
| 123 |
+
txt_content = """# Comment line
|
| 124 |
+
1000 0.1
|
| 125 |
+
1001 0.2
|
| 126 |
+
1002 0.3
|
| 127 |
+
1003 0.4
|
| 128 |
+
1004 0.5
|
| 129 |
+
1005 0.6
|
| 130 |
+
1006 0.7
|
| 131 |
+
1007 0.8
|
| 132 |
+
1008 0.9
|
| 133 |
+
1009 1.0
|
| 134 |
+
1010 1.1
|
| 135 |
+
1011 1.2"""
|
| 136 |
+
|
| 137 |
+
x, y = parse_txt_spectrum(txt_content)
|
| 138 |
+
expected_x = np.array(
|
| 139 |
+
[1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011]
|
| 140 |
+
)
|
| 141 |
+
expected_y = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2])
|
| 142 |
+
|
| 143 |
+
np.testing.assert_array_equal(x, expected_x)
|
| 144 |
+
np.testing.assert_array_equal(y, expected_y)
|
| 145 |
+
|
| 146 |
+
# Test comma-separated
|
| 147 |
+
txt_comma = """1000,0.1
|
| 148 |
+
1001,0.2
|
| 149 |
+
1002,0.3
|
| 150 |
+
1003,0.4
|
| 151 |
+
1004,0.5
|
| 152 |
+
1005,0.6
|
| 153 |
+
1006,0.7
|
| 154 |
+
1007,0.8
|
| 155 |
+
1008,0.9
|
| 156 |
+
1009,1.0
|
| 157 |
+
1010,1.1
|
| 158 |
+
1011,1.2"""
|
| 159 |
+
|
| 160 |
+
x_comma, y_comma = parse_txt_spectrum(txt_comma)
|
| 161 |
+
np.testing.assert_array_equal(x_comma, expected_x)
|
| 162 |
+
np.testing.assert_array_equal(y_comma, expected_y)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def test_parse_spectrum_data_integration():
|
| 166 |
+
"""Test integrated spectrum data parsing with format detection."""
|
| 167 |
+
# Test automatic format detection and parsing
|
| 168 |
+
test_cases = [
|
| 169 |
+
(
|
| 170 |
+
'{"wavenumbers": [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011], "intensities": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]}',
|
| 171 |
+
"test.json",
|
| 172 |
+
),
|
| 173 |
+
(
|
| 174 |
+
"wavenumber,intensity\n1000,0.1\n1001,0.2\n1002,0.3\n1003,0.4\n1004,0.5\n1005,0.6\n1006,0.7\n1007,0.8\n1008,0.9\n1009,1.0\n1010,1.1\n1011,1.2",
|
| 175 |
+
"test.csv",
|
| 176 |
+
),
|
| 177 |
+
(
|
| 178 |
+
"1000 0.1\n1001 0.2\n1002 0.3\n1003 0.4\n1004 0.5\n1005 0.6\n1006 0.7\n1007 0.8\n1008 0.9\n1009 1.0\n1010 1.1\n1011 1.2",
|
| 179 |
+
"test.txt",
|
| 180 |
+
),
|
| 181 |
+
]
|
| 182 |
+
|
| 183 |
+
for content, filename in test_cases:
|
| 184 |
+
x, y = parse_spectrum_data(content, filename)
|
| 185 |
+
assert len(x) >= 10
|
| 186 |
+
assert len(y) >= 10
|
| 187 |
+
assert len(x) == len(y)
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_insufficient_data_points():
|
| 191 |
+
"""Test handling of insufficient data points."""
|
| 192 |
+
# Test with too few points
|
| 193 |
+
insufficient_data = "1000 0.1\n1001 0.2" # Only 2 points, need at least 10
|
| 194 |
+
|
| 195 |
+
with pytest.raises(ValueError, match="Insufficient data points"):
|
| 196 |
+
parse_txt_spectrum(insufficient_data, "test.txt")
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def test_invalid_json():
|
| 200 |
+
"""Test handling of invalid JSON."""
|
| 201 |
+
invalid_json = (
|
| 202 |
+
'{"wavenumbers": [1000, 1001], "intensities": [0.1}' # Missing closing bracket
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
with pytest.raises(ValueError, match="Invalid JSON format"):
|
| 206 |
+
parse_json_spectrum(invalid_json)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_empty_file():
|
| 210 |
+
"""Test handling of empty files."""
|
| 211 |
+
empty_content = ""
|
| 212 |
+
|
| 213 |
+
with pytest.raises(ValueError, match="No data lines found"):
|
| 214 |
+
parse_txt_spectrum(empty_content, "empty.txt")
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
if __name__ == "__main__":
|
| 218 |
+
pytest.main([__file__])
|