source: anuga_core/source/anuga/alpha_shape/test_alpha_shape.py @ 7276

Last change on this file since 7276 was 7276, checked in by ole, 15 years ago

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

File size: 10.6 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4import sys
5import unittest
6
7import numpy as num
8
9try:
10    from anuga.alpha_shape.alpha_shape import *
11except ImportError: 
12    from alpha_shape import *
13
14class TestCase(unittest.TestCase):
15
16    def setUp(self):
17        pass
18
19         
20    def tearDown(self):
21        pass
22
23    def test_delaunay(self):
24        #print "test_delaunay"
25        a = [0.0, 0.0]
26        b = [1.0, 0.0]
27        c = [2.0, 0.0]
28        d = [2.0, 2.0]
29        e = [1.0, 2.0]
30        f = [0.0, 2.0]
31
32        alpha = Alpha_Shape([a,b,c,d,e,f])
33        result = alpha.get_delaunay()
34        answer = [(0, 1, 5), (5, 1, 4), (4, 2, 3), (2, 4, 1)]
35        assert num.allclose(answer, result) 
36
37    def test_3_points_on_line(self):
38        #print "test_delaunay"
39        a = [0.0, 0.0]
40        b = [1.0, 0.0]
41        c = [2.0, 0.0]
42
43        try:
44            alpha = Alpha_Shape([a,b,c])
45        except PointError:
46            pass
47        else:
48            self.failUnless(0==1, \
49                        'point list with 2 points did not raise an error!')
50
51
52
53    def test_alpha_1(self):
54        #print "test_alpha"
55        a = [0.0, 0.0]
56        b = [1.0, 0.0]
57        c = [2.0, 0.0]
58        d = [2.0, 2.0]
59        e = [1.0, 2.0]
60        f = [0.0, 2.0]
61
62        alpha = Alpha_Shape([a,b,c,d,e,f])
63        result = alpha.get_boundary()
64        #print "result",result
65        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
66        assert num.allclose(answer, result) 
67
68
69    def test_alpha_2(self):
70        #print "test_alpha"
71        a = [0.0, 0.0]
72        b = [2.0, 0.0]
73        c = [4.0, 0.0]
74        cd = [3.0,2.0]
75        d = [4.0, 4.0]
76        e = [2.0, 4.0]
77        f = [0.0, 4.0]
78
79        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
80        result = alpha.get_boundary()
81        #print "result",result
82        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
83        assert num.allclose(answer, result) 
84
85
86    def test_alpha_3(self):
87        #print "test_alpha"
88        a = [0.0, 0.0]
89        b = [1.0, 0.0]
90        c = [2.0, 0.0]
91        d = [2.0, 2.0]
92        e = [1.0, 2.0]
93        f = [0.0, 2.0]
94        alf = 1.5
95
96        alpha = Alpha_Shape([a,b,c,d,e,f],alf)
97        result = alpha.get_boundary()
98        #print "result",result
99        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
100        assert num.allclose(answer, result) 
101
102
103    def test_boundary_1(self):
104        a = [0.0, 0.0]
105        b = [2.0, 0.0]
106        c = [4.0, 0.0]
107        cd = [3.0,2.0]
108        d = [4.0, 4.0]
109        e = [2.0, 4.0]
110        f = [0.0, 4.0]
111
112        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
113        alpha.set_boundary_type(0,1,0,0)
114        result = alpha.get_boundary()
115        #print "result",result
116        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
117        assert num.allclose(answer, result) 
118   
119
120
121    def test_boundary_2(self):
122        a = [0.0, 0.0]
123        b = [2.0, 0.0]
124        c = [4.0, 0.0]
125        cd = [3.0,2.0]
126        d = [4.0, 4.0]
127        e = [2.0, 4.0]
128        f = [0.0, 4.0]
129
130        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
131        alpha.set_boundary_type(0,0,1,0)
132        result = alpha.get_boundary()
133        #print "result",result
134        answer = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 0)]
135        assert num.allclose(answer, result) 
136
137
138    def test_boundary_3(self):
139        a = [452.0, -300.0]
140        b = [637.0, -322.0]
141        c = [844.0, -474.0]
142        d = [900.0, -585.0]
143        e = [726.0, -492.0]
144        f = [481.0, -354.0]
145        g = [744.0, -388.0]
146
147        alpha = Alpha_Shape([a,b,c,d,e,f,g])
148        alpha.set_boundary_type(0,0,0,1)
149        result = alpha.get_boundary()
150        answer = [(1, 0), (0, 5), (3, 2), (4, 3), (2, 6), (6, 1), (5, 4)]
151        assert num.allclose(answer, result)
152
153
154       
155    def test_not_enough_points(self):
156        #print "test_not_enought_points"
157        a = [0.0, 0.0]
158        b = [1.0, 0.0]
159
160        try:
161            alpha = Alpha_Shape([a,b])
162        except PointError:
163            pass
164        else:
165            self.failUnless(0==1,
166                        'point list with 2 points did not raise an error!')
167
168## FIXME?  Running from the Comand line isn't in vogue these days
169#  The test was breaking when test_all at the inundation level was running
170# was running it. issue - not running the test in this directory
171    def Bad_test_alpha_stand_alone(self):
172        #print "test_alpha_stand_alone"
173        import os
174        import tempfile
175
176        fileName = tempfile.mktemp(".csv")
177        file = open(fileName,"w")
178        file.write("x,y\n\
1790.0, 0.0\n\
1801.0, 0.0\n\
1812.0, 0.0\n\
1822.0, 2.0\n\
1831.0, 2.0\n\
1840.0, 2.0\n")
185        file.close()
186
187        output_file_name = tempfile.mktemp(".bnd")
188        #(ho, output_file_name) = tempfile.mkstemp(".bnd")
189        command = sys.executable  + 'alpha_shape'+ os.sep +'alpha_shape.py ' \
190                  + fileName  + ' ' + output_file_name
191        #print "command", command
192        os.system(command)
193        os.remove(fileName)
194       
195        file = open(output_file_name,"r")
196        lFile = file.read().split('\n')
197        file.close()
198        os.remove(output_file_name)
199        self.failUnless(lFile[1] == "5,0" and
200                        lFile[2] == "0,1" and 
201                        lFile[3] == "4,5" and 
202                        lFile[4] == "2,3" and 
203                        lFile[5] == "3,4" and 
204                        lFile[6] == "1,2" and 
205                        lFile[7] == ""
206                        ,
207                        'boundary file is wrong')
208
209
210    def test_expand_pinch(self):
211        a = [1.0, 1.0]
212        b = [1.0, 5.0]
213        c = [4.0, 3.0]
214        d = [8.0, 5.0]
215        e = [8.0, 1.0]
216
217        alpha = Alpha_Shape([a,b,c,d,e])
218        alpha.set_boundary_type(raw_boundary=True,
219                          remove_holes=False,
220                          smooth_indents=False,
221                          expand_pinch=True)
222        result = alpha.get_boundary()
223        #print "result",result
224        answer = [(1, 0), (4, 3), (0, 4), (3, 1)]
225        assert num.allclose(answer, result)
226 
227    def test_sharp_indents(self):
228        a = [3.0, 1.0]
229        b = [5.0, 3.0]
230        c = [4.0, 4.0]
231        d = [3.0, 5.0]
232        e = [1.0, 3.0]
233
234        alpha = Alpha_Shape([a,b,c,d,e])
235        alpha.set_boundary_type(raw_boundary=True,
236                          remove_holes=False,
237                          smooth_indents=True,
238                          expand_pinch=False)
239        result = alpha.get_boundary()
240        #print "result",result
241        answer = [(3, 4), (2, 3), (0, 1), (1, 2), (4, 0)]
242        assert num.allclose(answer, result)
243
244       
245    def test_small_islands(self):
246        """
247        I couldn't find a small data set that could test this feature...
248        """
249        alpha = Alpha_Shape([(191.0,92.0), \
250(166.0,79.0), \
251(142.0,63.0), \
252(124.0,44.0), \
253(134.0,19.0), \
254(154.0,-9.0), \
255(182.0,-21.0), \
256(200.0,-3.0), \
257(227.0,13.0), \
258(237.0,59.0), \
259(208.0,86.0), \
260(243.0,34.0), \
261(209.0,35.0), \
262(171.0,15.0), \
263(159.0,41.0), \
264(189.0,56.0), \
265(235.0,-6.0), \
266(221.0,-17.0), \
267(204.0,-30.0), \
268(184.0,-41.0), \
269(160.0,-37.0), \
270(144.0,-24.0), \
271(128.0,-8.0), \
272(113.0,9.0), \
273(102.0,29.0), \
274(95.0,46.0), \
275(112.0,61.0), \
276(127.0,72.0), \
277(145.0,85.0), \
278(164.0,100.0), \
279(187.0,112.0), \
280(210.0,107.0), \
281(227.0,94.0), \
282(244.0,74.0), \
283(261.0,49.0), \
284(266.0,20.0), \
285(256.0,1.0), \
286(244.0,-28.0), \
287(228.0,-40.0), \
288(208.0,-50.0), \
289(192.0,-56.0), \
290(169.0,-64.0), \
291(155.0,-58.0), \
292(141.0,-46.0), \
293(129.0,-35.0), \
294(113.0,-17.0), \
295(103.0,-3.0), \
296(90.0,9.0), \
297(78.0,26.0), \
298(70.0,46.0), \
299(86.0,62.0), \
300(101.0,79.0), \
301(118.0,89.0), \
302(135.0,102.0), \
303(152.0,115.0), \
304(177.0,125.0), \
305(192.0,130.0), \
306(209.0,125.0), \
307(227.0,116.0), \
308(234.0,104.0), \
309(249.0,92.0), \
310(258.0,74.0), \
311(264.0,60.0), \
312(280.0,42.0), \
313(279.0,26.0), \
314(274.0,3.0), \
315(270.0,-19.0), \
316(259.0,-34.0), \
317(244.0,-45.0), \
318(229.0,-60.0), \
319(186.0,4.0), \
320(203.0,20.0), \
321(215.0,75.0), \
322(228.0,49.0), \
323(151.0,21.0), \
324(163.0,60.0), \
325(178.0,70.0), \
326(196.0,45.0), \
327(261.0,37.0), \
328(113.0,36.0), \
329(241.0,12.0), \
330(196.0,75.0), \
331(210.0,58.0), \
332(163.248648097,111.633266713), \
333(150.844951796,94.8282588211), \
334(130.438870783,84.0250394618), \
335(112.033385949,72.8217008669), \
336(98.8294511765,61.2182430364), \
337(184.855086816,-50.4150236771), \
338(171.251032808,-52.0155006192), \
339(156.446621093,-49.2146659705), \
340(148.044117147,-42.8127582019), \
341(264.878933922,-2.80083464873), \
342(260.077503096,-12.4036963015), \
343(255.27607227,-28.0083464873), \
344(163.248648097,-22.0065579543), \
345(128.03815537,6.00178853298), \
346(221.265937249,0.0), \
347(202.060213944,-13.6040540081), \
348(166.449601981,-2.80083464873), \
349(149.244474854,6.80202700405), \
350(182.454371403,29.2087041938), \
351(147.243878676,30.4090619004), \
352(126.437678428,28.0083464873), \
353(133.639824668,50.0149044415), \
354(176.852702105,43.612996673), \
355(123.636843779,-24.4072733675), \
356(73.6219393379,35.2104927268), \
357(212.463314068,-39.2116850822), \
358(254.875953034,66.4197930983), \
359(231.669037373,71.6213431603), \
360(85.6255164039,23.6070348964), \
361(98.4293319409,16.4048886568), \
362(223.266533427,-52.0155006192), \
363(208.062002477,-57.2170506811), \
364(243.672614439,86.425754875), \
365(214.06379101,113.633862891), \
366(205.261167828,115.234339833), \
367(194.057829233,120.836009131), \
368(85.2253971684,50.8151429126), \
369(84.0250394618,34.0101350202), \
370(194.457948469,-47.6141890283), \
371(232.86939508,-20.4060810121), \
372(252.875356856,23.2069156609), \
373(232.86939508,27.2081080162), \
374(214.463910245,13.6040540081), \
375(182.054252167,18.4054848345), \
376(163.248648097,29.6088234294), \
377(218.865221836,42.8127582019), \
378(176.45258287,97.2289742343), \
379(154.04590568,70.8211046892), \
380(200.059617766,110.833028242)])
381        alpha.set_boundary_type(raw_boundary=False ,
382                          remove_holes=True,
383                          smooth_indents=True,
384                          expand_pinch=True)
385        result = alpha.get_boundary()
386        #print "result",result
387        answer = [(45, 106), (44, 106), (46, 47), (44, 43), (48, 111), \
388                  (47, 111), (43, 42), (42, 41), (41, 88), (40, 88), \
389                  (107, 48), (49, 107), (49, 50), (50, 51), (52, 53), \
390                  (51, 52), (53, 54), (55, 83), (54, 83), (40, 114), \
391                  (67, 68), (68, 69), (67, 66), (65, 66), (64, 65), \
392                  (69, 114), (57, 56), (56, 55), (58, 57), (64, 63), \
393                  (61, 62), (62, 63), (59, 60), (58, 59), (60, 61), \
394                  (46, 45)]
395        assert num.allclose(answer, result)   
396
397#-------------------------------------------------------------
398
399if __name__ == "__main__":
400    suite = unittest.makeSuite(TestCase,'test')
401    runner = unittest.TextTestRunner(verbosity=1)
402    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.