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=1 |
20 |
Tag2=2 |
21 |
|
22 |
tol=1.E-15 |
23 |
|
24 |
# a rank 1 test argument |
25 |
a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ] |
26 |
|
27 |
# a rank 4 test argument |
28 |
a_r4=[ \ |
29 |
[ [ [[ 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]] ] ], \ |
30 |
[ [ [[ -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]] ] ], \ |
31 |
[ [[[ 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]] ] ] ] |
32 |
|
33 |
def prepareArg(val,ex,wh): |
34 |
if ex=="Expanded": |
35 |
exx=True |
36 |
else: |
37 |
exx=False |
38 |
out=Data(val[0],what=wh,expand=exx) |
39 |
if ex=="Tagged1": |
40 |
out.setTaggedValue(Tag1,val[1]) |
41 |
elif ex=="Tagged2": |
42 |
out.setTaggedValue(Tag1,val[1]) |
43 |
out.setTaggedValue(Tag2,val[2]) |
44 |
return out |
45 |
|
46 |
def turnToArray(val,tagged): |
47 |
if tagged=="Tagged1": |
48 |
out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)] |
49 |
elif tagged=="Tagged2": |
50 |
out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)] |
51 |
else: |
52 |
out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)] |
53 |
return out |
54 |
|
55 |
def checkResult(text,res,val0,val1,val2,wh): |
56 |
ref=Data(val0,what=wh,expand=False) |
57 |
ref.setTaggedValue(Tag1,val1) |
58 |
ref.setTaggedValue(Tag2,val2) |
59 |
norm=Lsup(ref)+tol |
60 |
error=Lsup(ref-res)/norm |
61 |
print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error) |
62 |
if error>tol: |
63 |
print "**** %s: error is too large"%(text) |
64 |
raise SystemError,"@@ %s at %s: error is too large"%(text,wh) |
65 |
sys.exit(1) |
66 |
|
67 |
# |
68 |
# ============================================================== |
69 |
# |
70 |
# test slice getting: |
71 |
# |
72 |
|
73 |
msh=bruce.Rectangle(20,6) |
74 |
|
75 |
for wh in [ContinuousFunction(msh), Function(msh)]: |
76 |
|
77 |
print wh |
78 |
|
79 |
for ex1 in ["Constant", "Expanded", "Tagged1", "Tagged2"]: |
80 |
|
81 |
print "Slice getting: ", ex1 |
82 |
|
83 |
# rank 1 |
84 |
|
85 |
arg=prepareArg(a_r1,ex1,wh) |
86 |
arrays=turnToArray(a_r1,ex1) |
87 |
|
88 |
checkResult("slicing: rank=1 [:] "+ex1, \ |
89 |
arg[:], \ |
90 |
arrays[0][:], \ |
91 |
arrays[1][:], \ |
92 |
arrays[2][:], \ |
93 |
wh) |
94 |
|
95 |
checkResult("slicing: rank=1 [1] "+ex1, \ |
96 |
arg[1], \ |
97 |
arrays[0][1], \ |
98 |
arrays[1][1], \ |
99 |
arrays[2][1], \ |
100 |
wh) |
101 |
|
102 |
checkResult("slicing: rank=1 [1:3] "+ex1, \ |
103 |
arg[1:3], \ |
104 |
arrays[0][1:3], \ |
105 |
arrays[1][1:3], \ |
106 |
arrays[2][1:3], \ |
107 |
wh) |
108 |
|
109 |
checkResult("slicing: rank=1 [2:] "+ex1, \ |
110 |
arg[2:], \ |
111 |
arrays[0][2:], \ |
112 |
arrays[1][2:], \ |
113 |
arrays[2][2:], \ |
114 |
wh) |
115 |
|
116 |
checkResult("slicing: rank=1 [:1] "+ex1, \ |
117 |
arg[:1], \ |
118 |
arrays[0][:1], \ |
119 |
arrays[1][:1], \ |
120 |
arrays[2][:1], \ |
121 |
wh) |
122 |
|
123 |
# rank 4 |
124 |
|
125 |
arg=prepareArg(a_r4,ex1,wh) |
126 |
arrays=turnToArray(a_r4,ex1) |
127 |
|
128 |
checkResult("slicing: rank=4 [:] "+ex1, \ |
129 |
arg[:], \ |
130 |
arrays[0][:], \ |
131 |
arrays[1][:], \ |
132 |
arrays[2][:], \ |
133 |
wh) |
134 |
|
135 |
checkResult("slicing: rank=4 [1] "+ex1, \ |
136 |
arg[1], \ |
137 |
arrays[0][1], \ |
138 |
arrays[1][1], \ |
139 |
arrays[2][1], \ |
140 |
wh) |
141 |
|
142 |
checkResult("slicing: rank=4 [1,1,1,1] "+ex1, \ |
143 |
arg[1,1,1,1], \ |
144 |
arrays[0][1,1,1,1], \ |
145 |
arrays[1][1,1,1,1], \ |
146 |
arrays[2][1,1,1,1], \ |
147 |
wh) |
148 |
|
149 |
checkResult("slicing: rank=4 [1:3] "+ex1, \ |
150 |
arg[1:3], \ |
151 |
arrays[0][1:3], \ |
152 |
arrays[1][1:3], \ |
153 |
arrays[2][1:3], \ |
154 |
wh) |
155 |
|
156 |
checkResult("slicing: rank=4 [:2] "+ex1, \ |
157 |
arg[:2], \ |
158 |
arrays[0][:2], \ |
159 |
arrays[1][:2], \ |
160 |
arrays[2][:2], \ |
161 |
wh) |
162 |
|
163 |
checkResult("slicing: rank=4 [1:] "+ex1, \ |
164 |
arg[1:], \ |
165 |
arrays[0][1:], \ |
166 |
arrays[1][1:], \ |
167 |
arrays[2][1:], \ |
168 |
wh) |
169 |
|
170 |
checkResult("slicing: rank=4 [:,:] "+ex1, \ |
171 |
arg[:,:], \ |
172 |
arrays[0][:,:], \ |
173 |
arrays[1][:,:], \ |
174 |
arrays[2][:,:], \ |
175 |
wh) |
176 |
|
177 |
checkResult("slicing: rank=4 [:,1] "+ex1, \ |
178 |
arg[:,1], \ |
179 |
arrays[0][:,1], \ |
180 |
arrays[1][:,1], \ |
181 |
arrays[2][:,1], \ |
182 |
wh) |
183 |
|
184 |
checkResult("slicing: rank=4 [:,1:3] "+ex1, \ |
185 |
arg[:,1:3], \ |
186 |
arrays[0][:,1:3], \ |
187 |
arrays[1][:,1:3], \ |
188 |
arrays[2][:,1:3], \ |
189 |
wh) |
190 |
|
191 |
checkResult("slicing: rank=4 [1:2,1:3] "+ex1, \ |
192 |
arg[1:2,1:3], \ |
193 |
arrays[0][1:2,1:3], \ |
194 |
arrays[1][1:2,1:3], \ |
195 |
arrays[2][1:2,1:3], \ |
196 |
wh) |
197 |
|
198 |
checkResult("slicing: rank=4 [:,1:2,1:2] "+ex1, \ |
199 |
arg[:,1:2,1:2], \ |
200 |
arrays[0][:,1:2,1:2], \ |
201 |
arrays[1][:,1:2,1:2], \ |
202 |
arrays[2][:,1:2,1:2], \ |
203 |
wh) |
204 |
|
205 |
checkResult("slicing: rank=4 [:,:,1:2,1:3] "+ex1, \ |
206 |
arg[:,:,1:2,1:3], \ |
207 |
arrays[0][:,:,1:2,1:3], \ |
208 |
arrays[1][:,:,1:2,1:3], \ |
209 |
arrays[2][:,:,1:2,1:3], \ |
210 |
wh) |
211 |
|
212 |
checkResult("slicing: rank=4 [1:2,1:3,1,1] "+ex1, \ |
213 |
arg[1:2,1:3,1,1], \ |
214 |
arrays[0][1:2,1:3,1,1], \ |
215 |
arrays[1][1:2,1:3,1,1], \ |
216 |
arrays[2][1:2,1:3,1,1], \ |
217 |
wh) |
218 |
|
219 |
checkResult("slicing: rank=4 [1:2,1:3,:,1] "+ex1, \ |
220 |
arg[1:2,1:3,:,1], \ |
221 |
arrays[0][1:2,1:3,:,1], \ |
222 |
arrays[1][1:2,1:3,:,1], \ |
223 |
arrays[2][1:2,1:3,:,1], \ |
224 |
wh) |
225 |
|
226 |
checkResult("slicing: rank=4 [1:2,1:3,0,:] "+ex1, \ |
227 |
arg[1:2,1:3,0,:], \ |
228 |
arrays[0][1:2,1:3,0,:], \ |
229 |
arrays[1][1:2,1:3,0,:], \ |
230 |
arrays[2][1:2,1:3,0,:], \ |
231 |
wh) |
232 |
|
233 |
checkResult("slicing: rank=4 [1:2,1:3,0,1] "+ex1, \ |
234 |
arg[1:2,1:3,0,1], \ |
235 |
arrays[0][1:2,1:3,0,1], \ |
236 |
arrays[1][1:2,1:3,0,1], \ |
237 |
arrays[2][1:2,1:3,0,1], \ |
238 |
wh) |
239 |
|
240 |
checkResult("slicing: rank=4 [1,1,0,1] "+ex1, \ |
241 |
arg[1,1,0,1], \ |
242 |
arrays[0][1,1,0,1], \ |
243 |
arrays[1][1,1,0,1], \ |
244 |
arrays[2][1,1,0,1], \ |
245 |
wh) |
246 |
|
247 |
sys.exit(0) |
248 |
# end |