/[escript]/trunk/escript/test/python/SliceTest.py
ViewVC logotype

Annotation of /trunk/escript/test/python/SliceTest.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 613 - (hide annotations)
Wed Mar 22 01:12:32 2006 UTC (15 years, 7 months ago) by elspeth
File MIME type: text/x-python
File size: 13141 byte(s)
More copyright information inserted.

1 elspeth 613
2     """
3    
4     Tests various slicing operations on Data.
5    
6     the results are compared to reference result. if the relative difference is
7     bigger than tol an exception is raised.
8    
9     by Lutz Gross, ACcESS, University of Queensland, Australia, 2003.
10    
11     Version $Id$
12    
13     """
14    
15     __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
16     http://www.access.edu.au
17     Primary Business: Queensland, Australia"""
18     __licence__="""Licensed under the Open Software License version 3.0
19     http://www.opensource.org/licences/osl-3.0.php"""
20    
21 jgs 102 import sys
22     import unittest
23     import os
24    
25     esys_root=os.getenv('ESYS_ROOT')
26     sys.path.append(esys_root+'/finley/lib')
27     sys.path.append(esys_root+'/escript/lib')
28     sys.path.append(esys_root+'/escript/py_src')
29    
30     from escript import *
31     import finley
32     import numarray
33     from util import *
34    
35     from numarray import array,Float64,ones,greater
36    
37     Tag1=10
38     Tag2=11
39    
40     tol=1.E-15
41    
42     #
43     # list of arguments: a list item has the form [a0,a1,a2]
44     # what a0 is the default value and a1 is used for tag Tag1
45     # and a2 for Tag2. a0,a1,a2 are converted into numarrays.
46     #
47     # binary operations are tested on all pairs from arglist
48     #
49     # each item in the arglist are used to construct the following 5 argument
50     # types arg for arithmetic operations:
51     #
52     # 1) arg is a numarray/list a0
53     # 2) arg is a Data with default value a0
54     # 3) arg is an DataArray with constant value a0
55     # 4) arg is a Data object with constant value a0 and value a1 for tag Tag1
56     # 5) arg is a Data object with constant value a0 and value a1 for tag Tag1
57     # and value a2 for Tag2.
58     #
59     # i.e for a single binary arithmetic operation (len(arglist)*5)**2
60     # test are performed.
61     #
62    
63     arglist = [ \
64     [ [3,4], [-5,6.], [2,3] ], \
65     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
66     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
67     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
68     [ [3.], [6.], [3] ] \
69     ]
70    
71     # these are used to test slicing:
72    
73     a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
74    
75     a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
76    
77     a_r4=[ \
78     [ [ [[ 1,2,3],[11,12,13]], [[21,22,23],[31,32,33]], [[41,42,43],[51,52,53]] ], [ [[101,102,103],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[201,202,203],[211,212,213]], [[221,222,223],[231,232,233]], [[241,242,243],[251,252,253]] ] ], \
79     [ [ [[-1,-2,-3],[-11,-12,-13]], [[-21,-22,-23],[-31,-32,-33]], [[-41,-42,-43],[-51,-52,-53]] ], [ [[-101,-102,-103],[-111,-112,-113]], [[-121,-122,-123],[-131,-132,-133]], [[-141,-142,-143],[-151,-152,-153]] ], [ [[-201,-202,-203],[-211,-212,-213]], [[-221,-222,-223],[-231,-232,-233]], [[-241,-242,-243],[-251,-252,-253]] ] ], \
80     [ [[[11,12,13],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[1101,1102,1103],[1111,1112,1113]], [[1121,1122,1123],[1131,1132,1133]], [[1141,1142,1143],[1151,1152,1153]] ], [ [[1201,1202,1203],[1211,1212,1213]], [[1221,1222,1223],[1231,1232,1233]], [[1241,1242,1243],[1251,1252,1253]] ] ] ]
81    
82     a_r4_in=[ \
83     [ [ [[1./1,1./2,1./3],[1./11,1./12,1./13]], [[1./21,1./22,1./23],[1./31,1./32,1./33]], [[1./41,1./42,1./43],[1./51,1./52,1./53]] ], [ [[1./101,1./102,1./103],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./201,1./202,1./203],[1./211,1./212,1./213]], [[1./221,1./222,1./223],[1./231,1./232,1./233]], [[1./241,1./242,1./243],[1./251,1./252,1./253]] ] ], \
84     [ [ [[-1./1,-1./2,-1./3],[-1./11,-1./12,-1./13]], [[-1./21,-1./22,-1./23],[-1./31,-1./32,-1./33]], [[-1./41,-1./42,-1./43],[-1./51,-1./52,-1./53]] ], [ [[-1./101,-1./102,-1./103],[-1./111,-1./112,-1./113]], [[-1./121,-1./122,-1./123],[-1./131,-1./132,-1./133]], [[-1./141,-1./142,-1./143],[1./-151,-1./152,-1./153]] ], [ [[-1./201,-1./202,-1./203],[-1./211,-1./212,-1./213]], [[-1./221,-1./222,-1./223],[-1./231,-1./232,-1./233]], [[-1./241,-1./242,-1./243],[-1./251,-1./252,-1./253]] ] ], \
85     [ [ [[1./11,1./12,1./13],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./1101,1./1102,1./1103],[1./1111,1./1112,1./1113]], [[1./1121,1./1122,1./1123],[1./1131,1./1132,1./1133]], [[1./1141,1./1142,1./1143],[1./1151,1./1152,1./1153]] ], [ [[1./1201,1./1202,1./1203],[1./1211,1./1212,1./1213]], [[1./1221,1./1222,1./1223],[1./1231,1./1232,1./1233]], [[1./1241,1./1242,1./1243],[1./1251,1./1252,1./1253]] ] ] ]
86    
87    
88     def turnToArray(val,tagged):
89     if tagged=="Tagged1":
90     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
91     elif tagged=="Tagged2":
92     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
93     else:
94     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
95     return out
96    
97     def prepareArg(val,ex,wh):
98     if ex=="Array":
99     out=val[0]
100     else:
101     if ex=="Expanded":
102     exx=True
103     else:
104     exx=False
105     out=Data(val[0],what=wh,expand=exx)
106     if ex=="Tagged1":
107     out.setTaggedValue(Tag1,val[1])
108     elif ex=="Tagged2":
109     out.setTaggedValue(Tag1,val[1])
110     out.setTaggedValue(Tag2,val[2])
111     return out
112    
113     def getRank(arg):
114     if isinstance(arg,Data):
115     return arg.getRank()
116     else:
117     g=array(arg)
118     if g.rank==0:
119     return 1
120     else:
121     return g.rank
122    
123     def isScalar(arg):
124     if isinstance(arg,Data):
125     if arg.getRank()==1 and arg.getShape()[0]==1:
126     return not None
127     else:
128     return None
129     else:
130     g=array(arg)
131     if g.rank==0:
132     return not None
133     else:
134     if g.rank==1 and g.shape[0]==1:
135     return not None
136     else:
137     return None
138    
139     def checkResult(text,org,res,val0,val1,val2,wh):
140     print "%s" % (text)
141     print "%s shape %s" % ("argument",org.getShape())
142     print "%s shape %s" % ("result",res.getShape())
143    
144     ref=Data(val0,what=wh,expand=False)
145     ref.setTaggedValue(Tag1,val1)
146     ref.setTaggedValue(Tag2,val2)
147     norm=Lsup(ref)+tol
148     error=Lsup(ref-res)/norm
149     print "error = %e\n" % (error)
150     if error>tol:
151     raise SystemError,"@@ %s at %s: error is too large" % (text,wh)
152    
153     #
154     # test slicing
155     #
156    
157     msh=finley.Rectangle(20,6,1)
158    
159     for wh in [ContinuousFunction(msh),Function(msh)]:
160    
161     for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
162    
163     print "\nData type: ", ex1
164     print "******************************************\n"
165    
166     # rank 1
167     arg=prepareArg(a_r1,ex1,wh)
168     arrays=turnToArray(a_r1,ex1)
169    
170     if isinstance(arg,Data):
171    
172     checkResult("slicing: rank=1,[:]",
173     arg, \
174     arg[:], \
175     arrays[0][:], \
176     arrays[1][:], \
177     arrays[2][:], \
178     wh)
179    
180     checkResult("slicing: rank=1 [1]",
181     arg, \
182     arg[1], \
183     arrays[0][1], \
184     arrays[1][1], \
185     arrays[2][1], \
186     wh)
187    
188     checkResult("slicing: rank=1 [1:3]",
189     arg, \
190     arg[1:3], \
191     arrays[0][1:3], \
192     arrays[1][1:3], \
193     arrays[2][1:3], \
194     wh)
195    
196     checkResult("slicing: rank=1 [2:]",
197     arg, \
198     arg[2:], \
199     arrays[0][2:], \
200     arrays[1][2:], \
201     arrays[2][2:], \
202     wh)
203    
204     checkResult("slicing: rank=1 [:1]",
205     arg, \
206     arg[:1], \
207     arrays[0][:1], \
208     arrays[1][:1], \
209     arrays[2][:1], \
210     wh)
211    
212     # rank 4
213     arg=prepareArg(a_r4,ex1,wh)
214     arrays=turnToArray(a_r4,ex1)
215    
216     if isinstance(arg,Data):
217    
218     checkResult("slicing: rank=4,[:]",
219     arg, \
220     arg[:], \
221     arrays[0][:], \
222     arrays[1][:], \
223     arrays[2][:], \
224     wh)
225    
226     checkResult("slicing: rank=4 [1]",
227     arg, \
228     arg[1], \
229     arrays[0][1], \
230     arrays[1][1], \
231     arrays[2][1], \
232     wh)
233    
234     checkResult("slicing: rank=4 [1,1,1,1]",
235     arg, \
236     arg[1,1,1,1], \
237     arrays[0][1,1,1,1], \
238     arrays[1][1,1,1,1], \
239     arrays[2][1,1,1,1], \
240     wh)
241    
242     checkResult("slicing: rank=4 [1:3]",
243     arg, \
244     arg[1:3], \
245     arrays[0][1:3], \
246     arrays[1][1:3], \
247     arrays[2][1:3], \
248     wh)
249    
250     checkResult("slicing: rank=4 [:2]",
251     arg, \
252     arg[:2], \
253     arrays[0][:2], \
254     arrays[1][:2], \
255     arrays[2][:2], \
256     wh)
257    
258     checkResult("slicing: rank=4 [1:]",
259     arg, \
260     arg[1:], \
261     arrays[0][1:], \
262     arrays[1][1:], \
263     arrays[2][1:], \
264     wh)
265    
266     checkResult("slicing: rank=4,[:,:]",
267     arg, \
268     arg[:,:], \
269     arrays[0][:,:], \
270     arrays[1][:,:], \
271     arrays[2][:,:], \
272     wh)
273    
274     checkResult("slicing: rank=4 [:,1]",
275     arg, \
276     arg[:,1], \
277     arrays[0][:,1], \
278     arrays[1][:,1], \
279     arrays[2][:,1], \
280     wh)
281    
282     checkResult("slicing: rank=4 [:,1:3]",
283     arg, \
284     arg[:,1:3], \
285     arrays[0][:,1:3], \
286     arrays[1][:,1:3], \
287     arrays[2][:,1:3], \
288     wh)
289    
290     checkResult("slicing: rank=4 [1:2,1:3]",
291     arg, \
292     arg[1:2,1:3], \
293     arrays[0][1:2,1:3], \
294     arrays[1][1:2,1:3], \
295     arrays[2][1:2,1:3], \
296     wh)
297    
298     checkResult("slicing: rank=4 [:,1:2,1:2]",
299     arg, \
300     arg[:,1:2,1:2], \
301     arrays[0][:,1:2,1:2], \
302     arrays[1][:,1:2,1:2], \
303     arrays[2][:,1:2,1:2], \
304     wh)
305    
306     checkResult("slicing: rank=4 [:,:,1:2,1:3]",
307     arg, \
308     arg[:,:,1:2,1:3], \
309     arrays[0][:,:,1:2,1:3], \
310     arrays[1][:,:,1:2,1:3], \
311     arrays[2][:,:,1:2,1:3], \
312     wh)
313    
314     checkResult("slicing: rank=4 [1:2,1:3,1,1]",
315     arg, \
316     arg[1:2,1:3,1,1], \
317     arrays[0][1:2,1:3,1,1], \
318     arrays[1][1:2,1:3,1,1], \
319     arrays[2][1:2,1:3,1,1], \
320     wh)
321    
322     checkResult("slicing: rank=4 [1:2,1:3,:,1] ",
323     arg, \
324     arg[1:2,1:3,:,1], \
325     arrays[0][1:2,1:3,:,1], \
326     arrays[1][1:2,1:3,:,1], \
327     arrays[2][1:2,1:3,:,1], \
328     wh)
329    
330     checkResult("slicing: rank=4 [1:2,1:3,0,:] ",
331     arg, \
332     arg[1:2,1:3,0,:], \
333     arrays[0][1:2,1:3,0,:], \
334     arrays[1][1:2,1:3,0,:], \
335     arrays[2][1:2,1:3,0,:], \
336     wh)
337    
338     checkResult("slicing: rank=4 [1:2,1:3,0,1] ",
339     arg, \
340     arg[1:2,1:3,0,1], \
341     arrays[0][1:2,1:3,0,1], \
342     arrays[1][1:2,1:3,0,1], \
343     arrays[2][1:2,1:3,0,1], \
344     wh)
345    
346     checkResult("slicing: rank=4 [1,1,0,1] ",
347     arg, \
348     arg[1,1,0,1], \
349     arrays[0][1,1,0,1], \
350     arrays[1][1,1,0,1], \
351     arrays[2][1,1,0,1], \
352     wh)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26