-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestSimpleArray.py
More file actions
171 lines (123 loc) · 4.86 KB
/
TestSimpleArray.py
File metadata and controls
171 lines (123 loc) · 4.86 KB
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
from djcdata import SimpleArray
import numpy as np
import unittest
import os
class TestSimpleArray(unittest.TestCase):
def createNumpy(self,dtype):
arr = np.array(np.random.rand(500,3,5,6)*100., dtype=dtype)
rs = np.array([0,100,230,500],dtype='int64')
return arr, rs
def test_createFromNumpy(self):
print('TestSimpleArray: createFromNumpy')
arr,rs = self.createNumpy('float32')
a = SimpleArray(dtype='float32')
a.createFromNumpy(arr,rs)
narr, nrs = a.copyToNumpy()
nrs=nrs[:,0]
diff = np.max(np.abs(narr-arr))
diff += np.max(np.abs(nrs-rs))
self.assertTrue(diff< 0.000001)
def test_transferToNumpy(self):
print('TestSimpleArray: transferToNumpy')
arr,rs = self.createNumpy('float32')
a = SimpleArray(arr,rs)
narr, nrs = a.transferToNumpy()
nrs=nrs[:,0]
diff = np.max(np.abs(narr-arr))
diff += np.max(np.abs(nrs-rs))
self.assertTrue(diff< 0.000001)
def test_transferToNumpyInt(self):
print('TestSimpleArray: transferToNumpyInt')
arr,rs = self.createNumpy('int32')
a = SimpleArray(arr,rs)
narr, nrs = a.transferToNumpy()
nrs=nrs[:,0]
diff = np.max(np.abs(narr-arr))
diff += np.max(np.abs(nrs-rs))
self.assertTrue(diff< 0.000001)
def test_createFromNumpyInt(self):
print('TestSimpleArray: createFromNumpyInt')
arr,rs = self.createNumpy('int32')
a = SimpleArray(dtype='int32')
a.createFromNumpy(arr,rs)
narr, nrs = a.copyToNumpy()
nrs=nrs[:,0]
diff = np.max(np.abs(narr-arr))
diff += np.max(np.abs(nrs-rs))
self.assertTrue(diff< 0.000001)
def test_createFromNumpy_rowsplits_int32(self):
print('TestSimpleArray: createFromNumpy_rowsplits_int32')
arr = np.array(np.random.rand(500,3,5,6)*100., dtype='float32')
rs = np.array([0,100,230,500], dtype='int32')
a = SimpleArray(dtype='float32')
a.createFromNumpy(arr,rs)
narr, nrs = a.copyToNumpy()
nrs = nrs[:,0]
diff = np.max(np.abs(narr-arr))
diff += np.max(np.abs(nrs-rs.astype('int64')))
self.assertTrue(diff< 0.000001)
def test_dynamicTypeChange(self):
print('TestSimpleArray: dynamicTypeChange')
arr,rs = self.createNumpy('int32')
name = "lala"
a = SimpleArray(dtype='float32',name=name)
fnames = ["a","b","c","d","e","f"]
a.setFeatureNames(fnames)
a.createFromNumpy(arr,rs)
self.assertTrue(a.featureNames() == fnames)
self.assertTrue(a.name() == name)
def test_writeRead(self):
print('TestSimpleArray: writeRead')
arr,rs = self.createNumpy('float32')
a = SimpleArray(arr,rs)
a.setName("myname")
a.setFeatureNames(["a","b","c","d","e","f"])
a.writeToFile("testfile.djcsa")
b = SimpleArray()
b.readFromFile("testfile.djcsa")
os.system('rm -f testfile.djcsa')
#os.system("rf -f testfile")
ad, ars = a.copyToNumpy()
bd, brs = b.copyToNumpy()
diff = np.max(np.abs(ad-bd))
diff += np.max(np.abs(ars-brs))
self.assertTrue(diff==0)
def test_equal(self):
print('TestSimpleArray: equal')
arr,rs = self.createNumpy('float32')
a = SimpleArray()
a.createFromNumpy(arr,rs)
b = SimpleArray()
b.createFromNumpy(arr,rs)
self.assertEqual(a, b)
b = a.copy()
self.assertEqual(a, b)
b.setFeatureNames(["a","b","c","d","e","f"])
self.assertNotEqual(a, b)
def test_append(self):
print('TestSimpleArray: append')
arr,rs = self.createNumpy('float32')
arr2,_ = self.createNumpy('float32')
a = SimpleArray(arr,rs)
aa = SimpleArray(arr2,rs)
a.append(aa)
arr2 = np.concatenate([arr,arr2],axis=0)
rs2 = rs.copy()[1:]
rs2 += rs[-1]
rs2 = np.concatenate([rs,rs2],axis=0)
b = SimpleArray(arr2,rs2)
self.assertEqual(a, b)
def test_split(self):
print('TestSimpleArray: split')
arr,rs = self.createNumpy('float32')
a = SimpleArray(arr,rs,name="myarray")
arrs, rss = arr[:rs[2]], rs[:3]
b = SimpleArray(arrs,rss,name="myarray")
asplit = a.split(2)
self.assertEqual(asplit, b)
def test_name(self):
print('TestSimpleArray: name')
arr,rs=self.createNumpy('float32')
a = SimpleArray(arr,rs)
a.setName("myname")
self.assertEqual("myname", a.name())