File: tConvert.py

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (218 lines) | stat: -rw-r--r-- 6,351 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
from _tConvert import *

def dotest(t):

    print ('');
    print ('begin dotest');
    print (t.testbool (True));
    print (t.testbool (False));
    print (t.testint (-1));
    print (t.testint (10L));
    print (t.testint64 (-123456789013L));
    print (t.testint64 (123456789014L));
    print (t.testssize (-2));
    print (t.testssize (11));
    print (t.testfloat (3.14));
    print (t.testfloat (12));
    print (t.teststring ("this is a string"));
    print (t.teststring (u"this is a unicode string"));

    print (t.testipos ([2,3,4]));
    print (t.testipos (1));
    print (t.testipos (NUM.array([2])));
    print (t.testipos (NUM.array(3)));

    print (t.testvecbool ([True,False,False,True]));
    print (t.testvecint ([1,2,3,4]));
    print (t.testvecint ([]));
    print (t.testvecint ((-1,-2,-3,-4)));
    print (t.testvecint (-10));
    print (t.testvecint (NUM.array((10,11,12))));
    print (t.testvecint (NUM.array(1)));
    print (t.testveccomplex ([1+2j, -1-3j, -1.5+2.5j]));
    print (t.testvecstr (["a1","a2","b1","b2"]));
    print (t.testvecstr (()));
    print (t.testvecstr ("sc1"));
    print (t.testvecstr (u"unicode sc1"));
    print (t.testvecstr ([u"uni1", "uni2"]));
    print (t.teststdvecbool ([False,True]));
    print (t.teststdvecuint ([1,2,4]));
    print (t.teststdvecuint (()));
    print (t.teststdvecuint (10));
    print (t.teststdvecvecuint ([[1,2,4]]));
    print (t.teststdvecvecuint ((())));
    print (t.teststdvecvecuint (()));
    print (t.teststdvecvecuint ([1,2,4]));
    print (t.teststdvecvecuint (20));

    print (t.testvh (True));
    print (t.testvh (2));
    print (t.testvh (1234567890123L));
    print (t.testvh (1.3));
    print (t.testvh (10-11j));
    print (t.testvh ("str"));
    print (t.testvh (u"unistr"));
    print (t.testvh ([True]) + 0);         # add 0 to convert numpy to integer
    print (t.testvh ([2,4,6,8,10]));
    print (t.testvh ([1.3,4,5,6]));
    print (t.testvh ([10-11j,1+2j]));
#    print (t.testvh ([]));
    print (t.testvh (["str1","str2"]));
    print (t.testvh ([u"ustr1",u"ustr2"]));
    print (t.testvh ({"shape":[2,2],"array":["str1","str2","str3","str4"]}));
    print (t.testvh ({"shape":[3,1],"array":[u"ustr1",u"ustr2",u"ustr3"]}));
    a  =  t.testvh ({"shape":[2,2],"array":["str1","str2","str3","str4"]});
    print (a);
    print (t.testvh (a));

    a  =  t.testvh ([10-11j,1+2j]);
    print (a.shape);
    print (t.testvh (a));

    b  =  NUM.int32([[2,3],[4,5]]);
    print (b);
    print (t.testvh (b));

    b  =  NUM.int32([1,2,3,4,5,6,7,8,9,10]);
    print (b[2:9:2]);
    print (t.testvh (b[2:9:2]));

    b  =  NUM.array([1,2,3,4,5,6,7,8,9,10.]);
    print (b[2:9:2]);
    print (t.testvh (b[2:9:2]));
    a = b[2:9:2];
    print (t.testvh (a));

    print (t.testvh(NUM.array([20.+10j])));
    print (t.testvh(NUM.array([21.])));
    print (t.testvh(NUM.array(21.)));

    print ('>>>');
    res = t.testvh (NUM.array([]));
    print ('<<<');
    print (res.shape);
    print ('>>>');
    res = t.testvh (NUM.array([[]]));
    print ('<<<');
    print (res.shape);

    # Test a sequence of ValueHolders
    print (t.teststdvecvh([2, 1.3, [True,False]]));

    # On 64-bit machines the output also contains 'dtype=int32'
    # So leave it out.
    a = t.testrecord({"int":1, "int64":123456789012L, "str":"bc",
                      "ustr":"ubc", 'vecint':[1,2,3]})
    print ('>>>');
    print (a);
    print ('<<<');
    print ('end dotest');
    print ('');


def testarrvh(arr):
    print ('    testarrvh');
    print (t.testvh(arr));
    print (t.testvh(arr[0]));
    print (t.testvh([arr[0]]));
    print (t.testvh([arr[0], arr[1]]));

def testarrb(arr):
    print ('  testarrb')
    # Test array
    testarrvh(arr);
    # Test array scalar
    print (t.testbool(arr[0]));
    # Test scalar array
    print (t.testbool(NUM.array(arr[1])));

def testarri(arr):
    print ('  testarri')
    testarrvh(arr);
    print (t.testint(arr[0]));
    print (t.testint(NUM.array(arr[1])));
    print (t.testint64(arr[0]));
    print (t.testssize(arr[0]));
    print (t.testfloat(arr[0]));
    print (t.testcomplex(arr[0]));

def testarrf(arr):
    print ('  testarrf')
    testarrvh(arr);
    print (t.testfloat(arr[0]));
    print (t.testfloat(NUM.array(arr[1])));
    print (t.testcomplex(arr[0]));

def testarrc(arr):
    print ('  testarrc')
    testarrvh(arr);
    print (t.testcomplex(arr[0]));
    print (t.testcomplex(NUM.array(arr[1])));

def testarrstr(arr):
    print ('  testarrstr')
    testarrvh(arr);
    print (t.teststring(arr[0]));
    # numpy scalar array of string not supported
    #print (t.teststring(NUM.array(arr[1])));

def testnps():
    testarrb(NUM.array([True,False]));
    testarri(NUM.int8([-6,-7]));
    testarri(NUM.uint8([5,6]));
    testarri(NUM.int16([-16,-17]));
    testarri(NUM.uint16([15,16]));
    testarri(NUM.int32([-26,-27]));
    testarri(NUM.uint32([25,26]));
    testarri(NUM.int64([-36,-37]));
    testarri(NUM.uint64([35,36]));
    testarrf(NUM.float32([-46,-47]));
    testarrf(NUM.float64([45,46]));
    testarrc(NUM.complex64([-56-66j,-57-67j]));
    testarrc(NUM.complex128([-76-86j,-77-87j]));
    testarrstr(NUM.array(['1','2']));

def testexcp():
    # Test a normal exception.
    excp = False
    try:
        b = t.testvh([1, "str"])   # incompatible types
    except:
        excp = True
    if not excp:
        print ("Normal exception in testexcp was not converted");
    # Test an IterError exception.
    excp = False
    try:
        b = t.testitererror()
    except StopIteration:
        excp = True
    if not excp:
        print ("IterError exception in testexcp was not converted");

def testnp():
    # Test byte and sbyte.
    b = NUM.int8([-1,-2]);
    print (t.testvh(b));
    b = NUM.uint8([211,212]);
    print (t.testvh(b));
    print ('>>>');
    res = t.testvh(NUM.array([]));
    print ('<<<');
    print (res.shape);
    print (t.testvh(NUM.array([["abcd","c"],["12","x12"]])));
    testnps();
    testexcp();


if __name__ == "__main__":
    import numpy as NUM
    from distutils.version import LooseVersion
    if LooseVersion(NUM.version.version) >= LooseVersion("1.14"):
        NUM.set_printoptions(legacy="1.13")
    t = tConvert();
    print ("Doing numpy/array test ...");
    testnp();
    # Do other tests.
    dotest(t)