1 |
""" |
2 |
|
3 |
Tests slice getting for Data objects. |
4 |
|
5 |
Version $Id$ |
6 |
|
7 |
""" |
8 |
|
9 |
import sys |
10 |
import unittest |
11 |
import os |
12 |
|
13 |
from esys.escript import * |
14 |
from esys import bruce |
15 |
|
16 |
import numarray |
17 |
from numarray import array,Float64,ones,greater |
18 |
|
19 |
Tag1=10 |
20 |
Tag2=11 |
21 |
|
22 |
tol=1.E-15 |
23 |
|
24 |
# |
25 |
# list of arguments: a list item has the form [a0,a1,a2] |
26 |
# what a0 is the default value and a1 is used for tag Tag1 |
27 |
# and a2 for tag2. a0,a1,a2 are converted into numarrays. |
28 |
# |
29 |
|
30 |
arglist = [ \ |
31 |
[ [3,4], [-5,6.], [2,3] ], \ |
32 |
[ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \ |
33 |
[ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \ |
34 |
[ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \ |
35 |
[ 3.0, 6.0, 3 ] \ |
36 |
] |
37 |
|
38 |
# these are used to test slicing: |
39 |
a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ] |
40 |
a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ] |
41 |
a_r4=[ \ |
42 |
[ [ [[ 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]] ] ], \ |
43 |
[ [ [[ -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]] ] ], \ |
44 |
[ [[[ 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]] ] ] ] |
45 |
a_r4_in=[ \ |
46 |
[ [ [[ 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]] ] ], \ |
47 |
[ [ [[ -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]] ] ], \ |
48 |
[ [[[ 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]] ] ] ] |
49 |
|
50 |
def turnToArray(val,tagged): |
51 |
if tagged=="Tagged1": |
52 |
out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)] |
53 |
elif tagged=="Tagged2": |
54 |
out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)] |
55 |
else: |
56 |
out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)] |
57 |
return out |
58 |
|
59 |
def prepareArg(val,ex,wh): |
60 |
if ex=="Array": |
61 |
out=val[0] |
62 |
else: |
63 |
if ex=="Expanded": |
64 |
exx=True |
65 |
else: |
66 |
exx=False |
67 |
out=Data(val[0],what=wh,expand=exx) |
68 |
if ex=="Tagged1": |
69 |
out.setTaggedValue(Tag1,val[1]) |
70 |
elif ex=="Tagged2": |
71 |
out.setTaggedValue(Tag1,val[1]) |
72 |
out.setTaggedValue(Tag2,val[2]) |
73 |
return out |
74 |
|
75 |
def checkResult(text,res,val0,val1,val2,wh): |
76 |
ref=Data(val0,what=wh,expand=False) |
77 |
ref.setTaggedValue(Tag1,val1) |
78 |
ref.setTaggedValue(Tag2,val2) |
79 |
norm=Lsup(ref)+tol |
80 |
error=Lsup(ref-res)/norm |
81 |
print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error) |
82 |
if error>tol: |
83 |
print "**** %s: error is too large"%(text) |
84 |
raise SystemError,"@@ %s at %s: error is too large"%(text,wh) |
85 |
sys.exit(1) |
86 |
|
87 |
def getRank(arg): |
88 |
if isinstance(arg,Data): |
89 |
return arg.getRank() |
90 |
else: |
91 |
g=array(arg) |
92 |
if g.rank==0: |
93 |
return 1 |
94 |
else: |
95 |
return g.rank |
96 |
|
97 |
def isScalar(arg): |
98 |
if isinstance(arg,Data): |
99 |
if arg.getRank()==1 and arg.getShape()[0]==1: |
100 |
return not None |
101 |
else: |
102 |
return None |
103 |
else: |
104 |
g=array(arg) |
105 |
if g.rank==0: |
106 |
return not None |
107 |
else: |
108 |
if g.rank==1 and g.shape[0]==1: |
109 |
return not None |
110 |
else: |
111 |
return None |
112 |
|
113 |
# |
114 |
# ============================================================== |
115 |
# |
116 |
# test slice getting: |
117 |
# |
118 |
|
119 |
msh=bruce.Rectangle(20,6) |
120 |
for wh in [ContinuousFunction(msh),Function(msh)]: |
121 |
|
122 |
print wh |
123 |
|
124 |
for ex1 in ["Constant", "Expanded"]: |
125 |
|
126 |
print "Slice getting: ", ex1, ":" |
127 |
|
128 |
# rank 1 |
129 |
|
130 |
arg=prepareArg(a_r1,ex1,wh) |
131 |
arrays=turnToArray(a_r1,ex1) |
132 |
|
133 |
checkResult("slicing: rank=1 [:] "+ex1, \ |
134 |
arg[:], \ |
135 |
arrays[0][:], \ |
136 |
arrays[1][:], \ |
137 |
arrays[2][:], \ |
138 |
wh) |
139 |
|
140 |
checkResult("slicing: rank=1 [1] "+ex1, \ |
141 |
arg[1], \ |
142 |
arrays[0][1], \ |
143 |
arrays[1][1], \ |
144 |
arrays[2][1], \ |
145 |
wh) |
146 |
|
147 |
checkResult("slicing: rank=1 [1:3] "+ex1, \ |
148 |
arg[1:3], \ |
149 |
arrays[0][1:3], \ |
150 |
arrays[1][1:3], \ |
151 |
arrays[2][1:3], \ |
152 |
wh) |
153 |
|
154 |
checkResult("slicing: rank=1 [2:] "+ex1, \ |
155 |
arg[2:], \ |
156 |
arrays[0][2:], \ |
157 |
arrays[1][2:], \ |
158 |
arrays[2][2:], \ |
159 |
wh) |
160 |
|
161 |
checkResult("slicing: rank=1 [:1] "+ex1, \ |
162 |
arg[:1], \ |
163 |
arrays[0][:1], \ |
164 |
arrays[1][:1], \ |
165 |
arrays[2][:1], \ |
166 |
wh) |
167 |
|
168 |
# rank 4 |
169 |
|
170 |
arg=prepareArg(a_r4,ex1,wh) |
171 |
arrays=turnToArray(a_r4,ex1) |
172 |
|
173 |
checkResult("slicing: rank=4 [:] "+ex1, \ |
174 |
arg[:], \ |
175 |
arrays[0][:], \ |
176 |
arrays[1][:], \ |
177 |
arrays[2][:], \ |
178 |
wh) |
179 |
|
180 |
checkResult("slicing: rank=4 [1] "+ex1, \ |
181 |
arg[1], \ |
182 |
arrays[0][1], \ |
183 |
arrays[1][1], \ |
184 |
arrays[2][1], \ |
185 |
wh) |
186 |
|
187 |
checkResult("slicing: rank=4 [1,1,1,1] "+ex1, \ |
188 |
arg[1,1,1,1], \ |
189 |
arrays[0][1,1,1,1], \ |
190 |
arrays[1][1,1,1,1], \ |
191 |
arrays[2][1,1,1,1], \ |
192 |
wh) |
193 |
|
194 |
checkResult("slicing: rank=4 [1:3] "+ex1, \ |
195 |
arg[1:3], \ |
196 |
arrays[0][1:3], \ |
197 |
arrays[1][1:3], \ |
198 |
arrays[2][1:3], \ |
199 |
wh) |
200 |
|
201 |
checkResult("slicing: rank=4 [:2] "+ex1, \ |
202 |
arg[:2], \ |
203 |
arrays[0][:2], \ |
204 |
arrays[1][:2], \ |
205 |
arrays[2][:2], \ |
206 |
wh) |
207 |
|
208 |
checkResult("slicing: rank=4 [1:] "+ex1, \ |
209 |
arg[1:], \ |
210 |
arrays[0][1:], \ |
211 |
arrays[1][1:], \ |
212 |
arrays[2][1:], \ |
213 |
wh) |
214 |
|
215 |
checkResult("slicing: rank=4 [:,:] "+ex1, \ |
216 |
arg[:,:], \ |
217 |
arrays[0][:,:], \ |
218 |
arrays[1][:,:], \ |
219 |
arrays[2][:,:], \ |
220 |
wh) |
221 |
|
222 |
checkResult("slicing: rank=4 [:,1] "+ex1, \ |
223 |
arg[:,1], \ |
224 |
arrays[0][:,1], \ |
225 |
arrays[1][:,1], \ |
226 |
arrays[2][:,1], \ |
227 |
wh) |
228 |
|
229 |
checkResult("slicing: rank=4 [:,1:3] "+ex1, \ |
230 |
arg[:,1:3], \ |
231 |
arrays[0][:,1:3], \ |
232 |
arrays[1][:,1:3], \ |
233 |
arrays[2][:,1:3], \ |
234 |
wh) |
235 |
|
236 |
checkResult("slicing: rank=4 [1:2,1:3] "+ex1, \ |
237 |
arg[1:2,1:3], \ |
238 |
arrays[0][1:2,1:3], \ |
239 |
arrays[1][1:2,1:3], \ |
240 |
arrays[2][1:2,1:3], \ |
241 |
wh) |
242 |
|
243 |
checkResult("slicing: rank=4 [:,1:2,1:2] "+ex1, \ |
244 |
arg[:,1:2,1:2], \ |
245 |
arrays[0][:,1:2,1:2], \ |
246 |
arrays[1][:,1:2,1:2], \ |
247 |
arrays[2][:,1:2,1:2], \ |
248 |
wh) |
249 |
|
250 |
checkResult("slicing: rank=4 [:,:,1:2,1:3] "+ex1, \ |
251 |
arg[:,:,1:2,1:3], \ |
252 |
arrays[0][:,:,1:2,1:3], \ |
253 |
arrays[1][:,:,1:2,1:3], \ |
254 |
arrays[2][:,:,1:2,1:3], \ |
255 |
wh) |
256 |
|
257 |
checkResult("slicing: rank=4 [1:2,1:3,1,1] "+ex1, \ |
258 |
arg[1:2,1:3,1,1], \ |
259 |
arrays[0][1:2,1:3,1,1], \ |
260 |
arrays[1][1:2,1:3,1,1], \ |
261 |
arrays[2][1:2,1:3,1,1], \ |
262 |
wh) |
263 |
|
264 |
checkResult("slicing: rank=4 [1:2,1:3,:,1] "+ex1, \ |
265 |
arg[1:2,1:3,:,1], \ |
266 |
arrays[0][1:2,1:3,:,1], \ |
267 |
arrays[1][1:2,1:3,:,1], \ |
268 |
arrays[2][1:2,1:3,:,1], \ |
269 |
wh) |
270 |
|
271 |
checkResult("slicing: rank=4 [1:2,1:3,0,:] "+ex1, \ |
272 |
arg[1:2,1:3,0,:], \ |
273 |
arrays[0][1:2,1:3,0,:], \ |
274 |
arrays[1][1:2,1:3,0,:], \ |
275 |
arrays[2][1:2,1:3,0,:], \ |
276 |
wh) |
277 |
|
278 |
checkResult("slicing: rank=4 [1:2,1:3,0,1] "+ex1, \ |
279 |
arg[1:2,1:3,0,1], \ |
280 |
arrays[0][1:2,1:3,0,1], \ |
281 |
arrays[1][1:2,1:3,0,1], \ |
282 |
arrays[2][1:2,1:3,0,1], \ |
283 |
wh) |
284 |
|
285 |
checkResult("slicing: rank=4 [1,1,0,1] "+ex1, \ |
286 |
arg[1,1,0,1], \ |
287 |
arrays[0][1,1,0,1], \ |
288 |
arrays[1][1,1,0,1], \ |
289 |
arrays[2][1,1,0,1], \ |
290 |
wh) |
291 |
|
292 |
sys.exit(0) |
293 |
# end |