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

Last change on this file since 3514 was 3514, checked in by duncan, 18 years ago

Hi all,
I'm doing a change in the anuga structure, moving the code to

\anuga_core\source\anuga

After you have done an svn update, the PYTHONPATH has to be changed to;
PYTHONPATH = anuga_core/source/

This is part of changes required to make installation of anuga quicker and reducing the size of our sandpits.

If any imports are broken, try fixing them. With adding anuga. to them for example. If this seems to have really broken things, email/phone me.

Cheers
Duncan

File size: 11.1 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4import sys
5import unittest
6from Numeric import allclose
7
8try:
9    from anuga.alpha_shape.alpha_shape import *
10except ImportError: 
11    from alpha_shape import *
12
13class TestCase(unittest.TestCase):
14
15    def setUp(self):
16        pass
17
18         
19    def tearDown(self):
20        pass
21
22    def test_delaunay(self):
23        #print "test_delaunay"
24        a = [0.0, 0.0]
25        b = [1.0, 0.0]
26        c = [2.0, 0.0]
27        d = [2.0, 2.0]
28        e = [1.0, 2.0]
29        f = [0.0, 2.0]
30
31        alpha = Alpha_Shape([a,b,c,d,e,f])
32        result = alpha.get_delaunay()
33        answer = [(0, 1, 5), (5, 1, 4), (4, 2, 3), (2, 4, 1)]
34        assert allclose(answer, result) 
35
36    def test_3_points_on_line(self):
37        #print "test_delaunay"
38        a = [0.0, 0.0]
39        b = [1.0, 0.0]
40        c = [2.0, 0.0]
41
42        try:
43            alpha = Alpha_Shape([a,b,c])
44        except PointError:
45            pass
46        else:
47            self.failUnless(0==1, \
48                        'point list with 2 points did not raise an error!')
49
50
51
52    def test_alpha_1(self):
53        #print "test_alpha"
54        a = [0.0, 0.0]
55        b = [1.0, 0.0]
56        c = [2.0, 0.0]
57        d = [2.0, 2.0]
58        e = [1.0, 2.0]
59        f = [0.0, 2.0]
60
61        alpha = Alpha_Shape([a,b,c,d,e,f])
62        result = alpha.get_boundary()
63        #print "result",result
64        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
65        assert allclose(answer, result) 
66
67
68    def test_alpha_2(self):
69        #print "test_alpha"
70        a = [0.0, 0.0]
71        b = [2.0, 0.0]
72        c = [4.0, 0.0]
73        cd = [3.0,2.0]
74        d = [4.0, 4.0]
75        e = [2.0, 4.0]
76        f = [0.0, 4.0]
77
78        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
79        result = alpha.get_boundary()
80        #print "result",result
81        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
82        assert allclose(answer, result) 
83
84
85    def test_alpha_3(self):
86        #print "test_alpha"
87        a = [0.0, 0.0]
88        b = [1.0, 0.0]
89        c = [2.0, 0.0]
90        d = [2.0, 2.0]
91        e = [1.0, 2.0]
92        f = [0.0, 2.0]
93        alf = 1.5
94
95        alpha = Alpha_Shape([a,b,c,d,e,f],alf)
96        result = alpha.get_boundary()
97        #print "result",result
98        answer = [(5, 0), (0, 1), (4, 5), (2, 3), (3, 4), (1, 2)]
99        assert allclose(answer, result) 
100
101
102    def test_boundary_1(self):
103        a = [0.0, 0.0]
104        b = [2.0, 0.0]
105        c = [4.0, 0.0]
106        cd = [3.0,2.0]
107        d = [4.0, 4.0]
108        e = [2.0, 4.0]
109        f = [0.0, 4.0]
110
111        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
112        alpha.set_boundary_type(0,1,0,0)
113        result = alpha.get_boundary()
114        #print "result",result
115        answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
116        assert allclose(answer, result) 
117   
118
119
120    def test_boundary_2(self):
121        a = [0.0, 0.0]
122        b = [2.0, 0.0]
123        c = [4.0, 0.0]
124        cd = [3.0,2.0]
125        d = [4.0, 4.0]
126        e = [2.0, 4.0]
127        f = [0.0, 4.0]
128
129        alpha = Alpha_Shape([a,b,c,cd,d,e,f])
130        alpha.set_boundary_type(0,0,1,0)
131        result = alpha.get_boundary()
132        #print "result",result
133        answer = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 0)]
134        assert allclose(answer, result) 
135
136
137    def test_boundary_3(self):
138        a = [452.0, -300.0]
139        b = [637.0, -322.0]
140        c = [844.0, -474.0]
141        d = [900.0, -585.0]
142        e = [726.0, -492.0]
143        f = [481.0, -354.0]
144        g = [744.0, -388.0]
145
146        alpha = Alpha_Shape([a,b,c,d,e,f,g])
147        alpha.set_boundary_type(0,0,0,1)
148        result = alpha.get_boundary()
149        answer = [(1, 0), (0, 5), (3, 2), (4, 3), (2, 6), (6, 1), (5, 4)]
150        assert allclose(answer, result)
151
152
153       
154    def test_not_enough_points(self):
155        #print "test_not_enought_points"
156        a = [0.0, 0.0]
157        b = [1.0, 0.0]
158
159        try:
160            alpha = Alpha_Shape([a,b])
161        except PointError:
162            pass
163        else:
164            self.failUnless(0==1,
165                        'point list with 2 points did not raise an error!')
166
167## FIXME?  Running from the Comand line isn't in vogue these days
168#  The test was breaking when test_all at the inundation level was running
169# was running it. issue - not running the test in this directory
170    def Bad_test_alpha_stand_alone(self):
171        #print "test_alpha_stand_alone"
172        import os
173        import tempfile
174
175        fileName = tempfile.mktemp(".xya")
176        #(h,fileName) = tempfile.mkstemp(".xya")
177        file = open(fileName,"w")
178        file.write("\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 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 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 allclose(answer, result)   
396#-------------------------------------------------------------
397if __name__ == "__main__":
398    #print "starting tests \n"
399    suite = unittest.makeSuite(TestCase,'test')
400    runner = unittest.TextTestRunner(verbosity=1)
401    runner.run(suite)
402    #print "finished tests \n"
403   
404
405   
406   
407
408
409
Note: See TracBrowser for help on using the repository browser.