-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_arff.py
More file actions
246 lines (200 loc) · 6.39 KB
/
Copy pathtest_arff.py
File metadata and controls
246 lines (200 loc) · 6.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import pytest
from iterable.datatypes import ARFFIterable
from iterable.helpers.detect import open_iterable
try:
import arff # noqa: F401
HAS_ARFF = True
except ImportError:
HAS_ARFF = False
@pytest.mark.skipif(not HAS_ARFF, reason="ARFF format requires 'liac-arff' package")
class TestARFF:
def test_id(self):
datatype_id = ARFFIterable.id()
assert datatype_id == "arff"
def test_flatonly(self):
flag = ARFFIterable.is_flatonly()
assert flag
def test_read_standard_arff(self):
"""Test reading standard ARFF file"""
arff_content = """@relation test_data
@attribute name string
@attribute age numeric
@attribute city {NewYork,London,Paris}
@data
Alice,30,NewYork
Bob,25,London
Charlie,35,Paris
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
row = iterable.read()
assert row["name"] == "Alice"
assert row["age"] == 30.0 # ARFF numeric becomes float
assert row["city"] == "NewYork"
assert "_relation" in row
assert row["_relation"] == "test_data"
row = iterable.read()
assert row["name"] == "Bob"
assert row["age"] == 25.0
iterable.close()
finally:
os.unlink(temp_file)
def test_read_arff_with_missing_values(self):
"""Test reading ARFF with missing values"""
arff_content = """@relation test_data
@attribute name string
@attribute age numeric
@attribute city {NewYork,London,Paris}
@data
Alice,30,NewYork
Bob,?,London
Charlie,35,?
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
row = iterable.read()
assert row["name"] == "Alice"
assert row["age"] == 30.0
row = iterable.read()
assert row["name"] == "Bob"
assert row["age"] is None # Missing value should be None
row = iterable.read()
assert row["city"] is None
iterable.close()
finally:
os.unlink(temp_file)
def test_read_sparse_arff(self):
"""Test reading ARFF with sparse format"""
arff_content = """@relation sparse_data
@attribute attr1 numeric
@attribute attr2 numeric
@attribute attr3 numeric
@data
{0 1.0, 2 3.0}
{1 2.0}
{0 4.0, 1 5.0, 2 6.0}
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
row = iterable.read()
# Sparse format: {index value, ...}
# First row: index 0 = 1.0, index 2 = 3.0, index 1 = None
assert row["attr1"] == 1.0
assert row["attr2"] is None
assert row["attr3"] == 3.0
row = iterable.read()
# Second row: index 1 = 2.0, others None
assert row["attr1"] is None
assert row["attr2"] == 2.0
assert row["attr3"] is None
iterable.close()
finally:
os.unlink(temp_file)
def test_automatic_detection(self):
"""Test automatic format detection via open_iterable"""
arff_content = """@relation test
@attribute name string
@attribute value numeric
@data
Test,123
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
with open_iterable(temp_file) as source:
assert isinstance(source, ARFFIterable)
row = source.read()
assert row["name"] == "Test"
assert row["value"] == 123.0
finally:
os.unlink(temp_file)
def test_reset(self):
"""Test reset functionality"""
arff_content = """@relation test
@attribute name string
@data
First
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
row1 = iterable.read()
iterable.reset()
row2 = iterable.read()
assert row1 == row2
iterable.close()
finally:
os.unlink(temp_file)
def test_read_bulk(self):
"""Test bulk reading"""
arff_content = """@relation test
@attribute id numeric
@attribute name string
@data
1,Alice
2,Bob
3,Charlie
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
rows = iterable.read_bulk(2)
assert len(rows) == 2
assert rows[0]["id"] == 1.0
assert rows[0]["name"] == "Alice"
assert rows[1]["id"] == 2.0
assert rows[1]["name"] == "Bob"
iterable.close()
finally:
os.unlink(temp_file)
def test_relation_name_preserved(self):
"""Test that relation name is preserved in records"""
arff_content = """@relation my_dataset
@attribute value numeric
@data
42
"""
import os
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".arff", delete=False) as f:
f.write(arff_content)
temp_file = f.name
try:
iterable = ARFFIterable(temp_file)
row = iterable.read()
assert "_relation" in row
assert row["_relation"] == "my_dataset"
iterable.close()
finally:
os.unlink(temp_file)
def test_missing_dependency(self):
"""Test that missing liac-arff raises helpful error"""
# This test would need to mock the import, but we can at least verify
# the error message format is correct by checking the code
pass