1 | import os |
---|
2 | import unittest |
---|
3 | import tempfile |
---|
4 | import numpy as num |
---|
5 | |
---|
6 | from anuga.utilities.system_tools import get_pathname_from_package |
---|
7 | |
---|
8 | from csv_file import load_csv_as_array, load_csv_as_dict, store_parameters, \ |
---|
9 | load_csv_as_matrix, load_csv_as_polygons, \ |
---|
10 | load_csv_as_building_polygons |
---|
11 | |
---|
12 | class Test_csv(unittest.TestCase): |
---|
13 | def setUp(self): |
---|
14 | pass |
---|
15 | |
---|
16 | def tearDown(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def _create_csv_file(self): |
---|
20 | """ |
---|
21 | Create a dummy csv file. |
---|
22 | Return its filename. |
---|
23 | """ |
---|
24 | filename = tempfile.mktemp(".txt") |
---|
25 | file = open(filename,"w") |
---|
26 | file.write("elevation, stage\n\ |
---|
27 | 1.0, 3 \n\ |
---|
28 | 0.0, 4 \n\ |
---|
29 | 4.0, 3 \n\ |
---|
30 | 1.0, 6 \n") |
---|
31 | file.close() |
---|
32 | return filename |
---|
33 | |
---|
34 | def test_get_data_from_file1(self): |
---|
35 | filename = self._create_csv_file() |
---|
36 | |
---|
37 | x = load_csv_as_array(filename) |
---|
38 | |
---|
39 | os.remove(filename) |
---|
40 | |
---|
41 | assert num.allclose(x['elevation'], [1.0, 0.0, 4.0, 1.0]) |
---|
42 | assert num.allclose(x['stage'], [3.0, 4.0, 3.0, 6.0]) |
---|
43 | |
---|
44 | |
---|
45 | def test_get_data_from_file(self): |
---|
46 | filename = self._create_csv_file() |
---|
47 | |
---|
48 | header,x = load_csv_as_matrix(filename) |
---|
49 | os.remove(filename) |
---|
50 | |
---|
51 | assert num.allclose(x[:,0], [1.0, 0.0,4.0, 1.0]) |
---|
52 | |
---|
53 | |
---|
54 | def test_store_parameters(self): |
---|
55 | """tests store temporary file |
---|
56 | """ |
---|
57 | |
---|
58 | from os import sep, getenv |
---|
59 | |
---|
60 | output_dir='' |
---|
61 | file_name='details.csv' |
---|
62 | |
---|
63 | kwargs = {'file_name':'new2.txt', |
---|
64 | 'output_dir':output_dir, |
---|
65 | 'file_name':file_name, |
---|
66 | 'who':'me', |
---|
67 | 'what':'detail', |
---|
68 | 'how':2, |
---|
69 | 'why':241, |
---|
70 | # 'completed':345 |
---|
71 | } |
---|
72 | store_parameters(verbose=False,**kwargs) |
---|
73 | |
---|
74 | temp='detail_temp.csv' |
---|
75 | fid = open(temp) |
---|
76 | file_header = fid.readline() |
---|
77 | file_line = fid.readline() |
---|
78 | fid.close() |
---|
79 | |
---|
80 | |
---|
81 | keys = kwargs.keys() |
---|
82 | keys.sort() |
---|
83 | line='' |
---|
84 | header='' |
---|
85 | count=0 |
---|
86 | #used the sorted keys to create the header and line data |
---|
87 | for k in keys: |
---|
88 | # print "%s = %s" %(k, kwargs[k]) |
---|
89 | header = header+str(k) |
---|
90 | line = line+str(kwargs[k]) |
---|
91 | count+=1 |
---|
92 | if count <len(kwargs): |
---|
93 | header = header+',' |
---|
94 | line = line+',' |
---|
95 | header+='\n' |
---|
96 | line+='\n' |
---|
97 | |
---|
98 | |
---|
99 | #file exists |
---|
100 | assert os.access(temp, os.F_OK) |
---|
101 | assert header == file_header |
---|
102 | assert line == file_line |
---|
103 | |
---|
104 | os.remove(temp) |
---|
105 | |
---|
106 | def test_store_parameters1(self): |
---|
107 | """tests store in temporary file and other file |
---|
108 | """ |
---|
109 | |
---|
110 | from os import sep, getenv |
---|
111 | |
---|
112 | output_dir='' |
---|
113 | file_name='details.csv' |
---|
114 | |
---|
115 | kwargs = {'file_name':'new2.txt', |
---|
116 | 'output_dir':output_dir, |
---|
117 | 'file_name':file_name, |
---|
118 | 'who':'me', |
---|
119 | 'what':'detail', |
---|
120 | 'how':2, |
---|
121 | 'why':241, |
---|
122 | # 'completed':345 |
---|
123 | } |
---|
124 | store_parameters(verbose=False,**kwargs) |
---|
125 | |
---|
126 | kwargs['how']=55 |
---|
127 | kwargs['completed']=345 |
---|
128 | |
---|
129 | keys = kwargs.keys() |
---|
130 | keys.sort() |
---|
131 | line='' |
---|
132 | header='' |
---|
133 | count=0 |
---|
134 | #used the sorted keys to create the header and line data |
---|
135 | for k in keys: |
---|
136 | # print "%s = %s" %(k, kwargs[k]) |
---|
137 | header = header+str(k) |
---|
138 | line = line+str(kwargs[k]) |
---|
139 | count+=1 |
---|
140 | if count <len(kwargs): |
---|
141 | header = header+',' |
---|
142 | line = line+',' |
---|
143 | header+='\n' |
---|
144 | line+='\n' |
---|
145 | |
---|
146 | kwargs['how']=55 |
---|
147 | kwargs['completed']=345 |
---|
148 | |
---|
149 | store_parameters(verbose=False,**kwargs) |
---|
150 | |
---|
151 | # temp='detail_temp.csv' |
---|
152 | fid = open(file_name) |
---|
153 | file_header = fid.readline() |
---|
154 | file_line1 = fid.readline() |
---|
155 | file_line2 = fid.readline() |
---|
156 | fid.close() |
---|
157 | |
---|
158 | |
---|
159 | #file exists |
---|
160 | # print 'header',header,'line',line |
---|
161 | # print 'file_header',file_header,'file_line1',file_line1,'file_line2',file_line2 |
---|
162 | assert os.access(file_name, os.F_OK) |
---|
163 | assert header == file_header |
---|
164 | assert line == file_line1 |
---|
165 | |
---|
166 | temp='detail_temp.csv' |
---|
167 | os.remove(temp) |
---|
168 | os.remove(file_name) |
---|
169 | |
---|
170 | def test_store_parameters2(self): |
---|
171 | """tests appending the data to the end of an existing file |
---|
172 | """ |
---|
173 | |
---|
174 | from os import sep, getenv |
---|
175 | |
---|
176 | output_dir='' |
---|
177 | file_name='details.csv' |
---|
178 | |
---|
179 | kwargs = {'file_name':'new2.txt', |
---|
180 | 'output_dir':output_dir, |
---|
181 | 'file_name':file_name, |
---|
182 | 'who':'me', |
---|
183 | 'what':'detail', |
---|
184 | 'how':2, |
---|
185 | 'why':241, |
---|
186 | 'completed':345 |
---|
187 | } |
---|
188 | store_parameters(verbose=False,**kwargs) |
---|
189 | |
---|
190 | kwargs['how']=55 |
---|
191 | kwargs['completed']=23.54532 |
---|
192 | |
---|
193 | store_parameters(verbose=False,**kwargs) |
---|
194 | |
---|
195 | keys = kwargs.keys() |
---|
196 | keys.sort() |
---|
197 | line='' |
---|
198 | header='' |
---|
199 | count=0 |
---|
200 | #used the sorted keys to create the header and line data |
---|
201 | for k in keys: |
---|
202 | # print "%s = %s" %(k, kwargs[k]) |
---|
203 | header = header+str(k) |
---|
204 | line = line+str(kwargs[k]) |
---|
205 | count+=1 |
---|
206 | if count <len(kwargs): |
---|
207 | header = header+',' |
---|
208 | line = line+',' |
---|
209 | header+='\n' |
---|
210 | line+='\n' |
---|
211 | |
---|
212 | fid = open(file_name) |
---|
213 | file_header = fid.readline() |
---|
214 | file_line1 = fid.readline() |
---|
215 | file_line2 = fid.readline() |
---|
216 | fid.close() |
---|
217 | |
---|
218 | assert os.access(file_name, os.F_OK) |
---|
219 | assert header == file_header |
---|
220 | assert line == file_line2 |
---|
221 | |
---|
222 | os.remove(file_name) |
---|
223 | |
---|
224 | |
---|
225 | |
---|
226 | def test_csv2polygons(self): |
---|
227 | """test loading of a csv polygon file. |
---|
228 | """ |
---|
229 | |
---|
230 | path = get_pathname_from_package('anuga.shallow_water') |
---|
231 | testfile = os.path.join(path, 'polygon_values_example.csv') |
---|
232 | |
---|
233 | polygons, values = load_csv_as_polygons(testfile, |
---|
234 | value_name='floors') |
---|
235 | |
---|
236 | assert len(polygons) == 7, 'Must have seven polygons' |
---|
237 | assert len(values) == 7, 'Must have seven values' |
---|
238 | |
---|
239 | # Known floor values |
---|
240 | floors = {'1': 2, '2': 0, '3': 1, '4': 2, '5': 0, '8': 1, '9': 1} |
---|
241 | |
---|
242 | # Known polygon values |
---|
243 | known_polys = {'1': [[422681.61,871117.55], |
---|
244 | [422691.02,871117.60], |
---|
245 | [422690.87,871084.23], |
---|
246 | [422649.36,871081.85], |
---|
247 | [422649.36,871080.39], |
---|
248 | [422631.86,871079.50], |
---|
249 | [422631.72,871086.75], |
---|
250 | [422636.75,871087.20], |
---|
251 | [422636.75,871091.50], |
---|
252 | [422649.66,871092.09], |
---|
253 | [422649.83,871084.91], |
---|
254 | [422652.94,871084.90], |
---|
255 | [422652.84,871092.39], |
---|
256 | [422681.83,871093.73], |
---|
257 | [422681.61,871117.55]], |
---|
258 | '2': [[422664.22,870785.46], |
---|
259 | [422672.48,870780.14], |
---|
260 | [422668.17,870772.62], |
---|
261 | [422660.35,870777.17], |
---|
262 | [422664.22,870785.46]], |
---|
263 | '3': [[422661.30,871215.06], |
---|
264 | [422667.50,871215.70], |
---|
265 | [422668.30,871204.86], |
---|
266 | [422662.21,871204.33], |
---|
267 | [422661.30,871215.06]], |
---|
268 | '4': [[422473.44,871191.22], |
---|
269 | [422478.33,871192.26], |
---|
270 | [422479.52,871186.03], |
---|
271 | [422474.78,871185.14], |
---|
272 | [422473.44,871191.22]], |
---|
273 | '5': [[422369.69,871049.29], |
---|
274 | [422378.63,871053.58], |
---|
275 | [422383.91,871044.51], |
---|
276 | [422374.97,871040.32], |
---|
277 | [422369.69,871049.29]], |
---|
278 | '8': [[422730.56,871203.13], |
---|
279 | [422734.10,871204.90], |
---|
280 | [422735.26,871202.18], |
---|
281 | [422731.87,871200.58], |
---|
282 | [422730.56,871203.13]], |
---|
283 | '9': [[422659.85,871213.80], |
---|
284 | [422660.91,871210.97], |
---|
285 | [422655.42,871208.85], |
---|
286 | [422654.36,871211.68], |
---|
287 | [422659.85,871213.80]] |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | |
---|
293 | for id in ['1', '2', '3', '4', '5' ,'8' ,'9']: |
---|
294 | assert id in polygons.keys() |
---|
295 | assert id in values.keys() |
---|
296 | |
---|
297 | assert int(values[id]) == int(floors[id]) |
---|
298 | assert len(polygons[id]) == len(known_polys[id]) |
---|
299 | assert num.allclose(polygons[id], known_polys[id]) |
---|
300 | |
---|
301 | |
---|
302 | def test_csv2polygons_with_clipping(self): |
---|
303 | """test_csv2polygons with optional clipping |
---|
304 | """ |
---|
305 | #FIXME(Ole): Not Done!! |
---|
306 | |
---|
307 | path = get_pathname_from_package('anuga.shallow_water') |
---|
308 | testfile = os.path.join(path, 'polygon_values_example.csv') |
---|
309 | |
---|
310 | polygons, values = load_csv_as_polygons(testfile, |
---|
311 | value_name='floors', |
---|
312 | clipping_polygons=None) |
---|
313 | |
---|
314 | assert len(polygons) == 7, 'Must have seven polygons' |
---|
315 | assert len(values) == 7, 'Must have seven values' |
---|
316 | |
---|
317 | # Known floor values |
---|
318 | floors = {'1': 2, '2': 0, '3': 1, '4': 2, '5': 0, '8': 1, '9': 1} |
---|
319 | |
---|
320 | # Known polygon values |
---|
321 | known_polys = {'1': [[422681.61,871117.55], |
---|
322 | [422691.02,871117.60], |
---|
323 | [422690.87,871084.23], |
---|
324 | [422649.36,871081.85], |
---|
325 | [422649.36,871080.39], |
---|
326 | [422631.86,871079.50], |
---|
327 | [422631.72,871086.75], |
---|
328 | [422636.75,871087.20], |
---|
329 | [422636.75,871091.50], |
---|
330 | [422649.66,871092.09], |
---|
331 | [422649.83,871084.91], |
---|
332 | [422652.94,871084.90], |
---|
333 | [422652.84,871092.39], |
---|
334 | [422681.83,871093.73], |
---|
335 | [422681.61,871117.55]], |
---|
336 | '2': [[422664.22,870785.46], |
---|
337 | [422672.48,870780.14], |
---|
338 | [422668.17,870772.62], |
---|
339 | [422660.35,870777.17], |
---|
340 | [422664.22,870785.46]], |
---|
341 | '3': [[422661.30,871215.06], |
---|
342 | [422667.50,871215.70], |
---|
343 | [422668.30,871204.86], |
---|
344 | [422662.21,871204.33], |
---|
345 | [422661.30,871215.06]], |
---|
346 | '4': [[422473.44,871191.22], |
---|
347 | [422478.33,871192.26], |
---|
348 | [422479.52,871186.03], |
---|
349 | [422474.78,871185.14], |
---|
350 | [422473.44,871191.22]], |
---|
351 | '5': [[422369.69,871049.29], |
---|
352 | [422378.63,871053.58], |
---|
353 | [422383.91,871044.51], |
---|
354 | [422374.97,871040.32], |
---|
355 | [422369.69,871049.29]], |
---|
356 | '8': [[422730.56,871203.13], |
---|
357 | [422734.10,871204.90], |
---|
358 | [422735.26,871202.18], |
---|
359 | [422731.87,871200.58], |
---|
360 | [422730.56,871203.13]], |
---|
361 | '9': [[422659.85,871213.80], |
---|
362 | [422660.91,871210.97], |
---|
363 | [422655.42,871208.85], |
---|
364 | [422654.36,871211.68], |
---|
365 | [422659.85,871213.80]] |
---|
366 | } |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | |
---|
371 | for id in ['1', '2', '3', '4', '5' ,'8' ,'9']: |
---|
372 | assert id in polygons.keys() |
---|
373 | assert id in values.keys() |
---|
374 | |
---|
375 | assert int(values[id]) == int(floors[id]) |
---|
376 | assert len(polygons[id]) == len(known_polys[id]) |
---|
377 | assert num.allclose(polygons[id], known_polys[id]) |
---|
378 | |
---|
379 | |
---|
380 | |
---|
381 | |
---|
382 | |
---|
383 | def test_csv2building_polygons(self): |
---|
384 | """test_csv2building_polygons |
---|
385 | """ |
---|
386 | |
---|
387 | path = get_pathname_from_package('anuga.shallow_water') |
---|
388 | testfile = os.path.join(path, 'polygon_values_example.csv') |
---|
389 | |
---|
390 | polygons, values = load_csv_as_building_polygons(testfile, |
---|
391 | floor_height=3) |
---|
392 | |
---|
393 | assert len(polygons) == 7, 'Must have seven polygons' |
---|
394 | assert len(values) == 7, 'Must have seven values' |
---|
395 | |
---|
396 | # Known floor values |
---|
397 | floors = {'1': 6, '2': 0, '3': 3, '4': 6, '5': 0, '8': 3, '9': 3} |
---|
398 | |
---|
399 | |
---|
400 | for id in ['1', '2', '3', '4', '5' ,'8' ,'9']: |
---|
401 | assert id in polygons.keys() |
---|
402 | assert id in values.keys() |
---|
403 | |
---|
404 | assert float(values[id]) == float(floors[id]) |
---|
405 | |
---|
406 | |
---|
407 | |
---|
408 | ################################################################################# |
---|
409 | |
---|
410 | if __name__ == "__main__": |
---|
411 | suite = unittest.makeSuite(Test_csv, 'test') |
---|
412 | runner = unittest.TextTestRunner(verbosity=1) |
---|
413 | runner.run(suite) |
---|