-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpletest.py
362 lines (302 loc) · 14.8 KB
/
simpletest.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'''
Unit test
'''
import os
import random
import shutil
from collections import OrderedDict
import copy
import numpy as np
from info import Patch
from PlotAnalysis import PlotSingle
from batch_analysis import BatchAnalysis
from batch_query import Batch
from plot_computation import separate_multiply_line
from dcpyps import dataset, dcio
class TestSuit:
'''
Unit test.
'''
def __init__(self, patch_num = 5, cluster_num = 5, stretch_num = 5, stretch_len = 1000, open_amp = 5, shut_amp = 0):
'''
Create testing data.
'''
random_float = str(random.random())[2:]
self._dir = os.path.join(os.getcwd(),'Unit_test_temp'+random_float)
try:
os.makedirs(self._dir)
except FileExistsError:
print('File already exist.')
self.patch_num = patch_num
self.cluster_num = cluster_num
self.stretch_num = stretch_num
self.stretch_len = stretch_len
self.open_amp = open_amp
self.shut_amp = shut_amp
self._patch_list = {}
self._name_list = ['test_csv{}.csv'.format(i) for i in range(patch_num)]
for i, name in enumerate(self._name_list):
new_patch = {}
csv_state = []
csv_start = []
csv_end = []
csv_amp = []
csv_dwell = []
start_time = np.array(sorted(random.sample(range(10), cluster_num))) * 60000 + np.array(random.sample(range(1000), cluster_num))
for j in range(cluster_num):
new_cluster = {}
new_cluster['start'] = start_time[j]
if stretch_num >1:
new_cluster['stretch'] = np.random.randint(1, stretch_num)
else:
new_cluster['stretch'] = 1
new_cluster['mean_open'] = np.linspace(0.6, 1.4, new_cluster['stretch'])
np.random.shuffle(new_cluster['mean_open'])
new_cluster['mean_shut'] = np.linspace(0.6, 1.4, new_cluster['stretch'])
np.random.shuffle(new_cluster['mean_shut'])
new_cluster['open_amp'] = []
new_cluster['shut_amp'] = []
new_cluster['open_period'] = []
new_cluster['shut_period'] = []
temp_state = np.empty(new_cluster['stretch']*stretch_len*2)
temp_amp = np.empty(new_cluster['stretch']*stretch_len*2)
temp_dwell = np.empty(new_cluster['stretch']*stretch_len*2)
for k in range(new_cluster['stretch']):
temp_state[k*stretch_len*2: (k+1)*stretch_len*2:2] = 1
temp_state[k*stretch_len*2+1: (k+1)*stretch_len*2:2] = 0
new_cluster[k] = {}
new_cluster[k]['open_amp'] = np.random.normal(loc = open_amp, scale = 0.5, size = stretch_len)
new_cluster['open_amp'].append(new_cluster[k]['open_amp'])
temp_amp[k*stretch_len*2: (k+1)*stretch_len*2:2] = new_cluster[k]['open_amp']
new_cluster[k]['shut_amp'] = np.random.normal(loc = shut_amp, scale = 0.5, size = stretch_len)
new_cluster['shut_amp'].append(new_cluster[k]['shut_amp'])
temp_amp[k*stretch_len*2+1: (k+1)*stretch_len*2:2] = new_cluster[k]['shut_amp']
new_cluster[k]['open_period'] = np.exp(np.random.normal(loc = np.log(new_cluster['mean_open'][k]), scale = 0.05, size = stretch_len))
new_cluster['open_period'].append(new_cluster[k]['open_period'])
temp_dwell[k*stretch_len*2: (k+1)*stretch_len*2:2] = new_cluster[k]['open_period']
new_cluster[k]['shut_period'] = np.exp(np.random.normal(loc = np.log(new_cluster['mean_shut'][k]), scale = 0.05, size = stretch_len))
new_cluster['shut_period'].append(new_cluster[k]['shut_period'])
temp_dwell[k*stretch_len*2+1: (k+1)*stretch_len*2:2] = new_cluster[k]['shut_period']
dwell = np.cumsum(temp_dwell)
temp_start = start_time[j] + np.hstack((0, dwell[:-1]))
temp_end = start_time[j] + dwell
csv_state.append(np.append(temp_state, np.nan))
csv_start.append(np.append(temp_start, np.nan))
csv_end.append(np.append(temp_end, np.nan))
csv_amp.append(np.append(temp_amp, np.nan))
csv_dwell.append(np.append(temp_dwell, np.nan))
new_cluster['open_amp'] = np.hstack(new_cluster['open_amp'])
new_cluster['shut_amp'] = np.hstack(new_cluster['shut_amp'])
new_cluster['open_period'] = np.hstack(new_cluster['open_period'])
new_cluster['shut_period'] = np.hstack(new_cluster['shut_period'])
new_patch[j] = new_cluster
csv_state = np.hstack(csv_state)
csv_start = np.hstack(csv_start)
csv_end = np.hstack(csv_end)
csv_amp = np.hstack(csv_amp)
csv_dwell = np.hstack(csv_dwell)
empty_col = np.empty(len(csv_state))
csv = np.vstack((empty_col, empty_col, csv_state, empty_col, csv_start, csv_end, csv_amp, empty_col, csv_dwell))
csv = np.transpose(csv)
np.savetxt(os.path.join(self._dir, self._name_list[i]), csv, delimiter=',')
self._patch_list[name] = new_patch
def test_info(self):
'''
Test the info module.
'''
for patch in self._name_list:
test_patch = Patch(os.path.join(self._dir, patch))
test_patch.scan()
for index, cluster in enumerate(test_patch):
# Testing basic information loading
test_start = abs(cluster.start - self._patch_list[patch][index]['start'])
if test_start > 0.0001:
print('Module Info "start" test failed')
print('Expected: {}, Obtained: {}'.format(self._patch_list[patch][index]['start'],
cluster.start))
test_open_period = sum((cluster.open_period - self._patch_list[patch][index]['open_period'])**2)
if test_open_period > 0.0001:
print('Module Info "open_period" test failed')
print('Total difference: {}'.format(test_open_period))
test_shut_period = sum((cluster.shut_period - self._patch_list[patch][index]['shut_period'])**2)
if test_shut_period > 0.0001:
print('Module Info "shut_period" test failed')
print('Total difference: {}'.format(test_shut_period))
test_open_amp = sum((cluster.open_amp - self._patch_list[patch][index]['open_amp'])**2)
if test_open_amp > 0.0001:
print('Module Info "open_amp" test failed')
print('Total difference: {}'.format(test_open_amp))
test_shut_amp = sum((cluster.shut_amp - self._patch_list[patch][index]['shut_amp'])**2)
if test_shut_amp > 0.0001:
print('Module Info "shut_amp" test failed')
print('Total difference: {}'.format(test_shut_amp))
def test_PlotComputation_using_PlotMPL(self):
'''
Testing the PlotComputation using the PlotMPL module which uses Matplotlib as backend.
'''
for patch in self._name_list:
test_patch = Patch(os.path.join(self._dir, patch))
test_patch.scan()
for index, cluster in enumerate(test_patch):
cluster.compute_mode()
cluster.compute_mode_detail()
test_plot = PlotSingle(self._dir)
test_plot.load_cluster(cluster)
test_plot.plot_original()
test_plot.plot_popen_on_original()
test_plot.plot_open_close()
test_plot.plot_cost_difference()
def test_batch_analysis(self):
'''
test the batch analysis.
'''
cluster_list = []
for patch in self._name_list:
test_patch = Patch(os.path.join(self._dir, patch))
test_patch.scan()
for cluster in test_patch:
cluster.compute_mode()
cluster.compute_mode_detail()
cluster_list.append(cluster)
# Test BatchAnalysis class
batch_analysis = BatchAnalysis(list(cluster_list))
test_dict = batch_analysis.compute_cluster_summary()
for patchname in self._name_list:
for cluster_no in range(1, self.cluster_num+1):
test_dict = batch_analysis.compute_cluster_summary(
patchname = patchname, cluster_no = cluster_no)
temp_amp = np.mean(self._patch_list[patchname][cluster_no-1]['open_amp']
-
self._patch_list[patchname][cluster_no-1]['shut_amp'])
if abs(test_dict['mean_amp'] - temp_amp) > 0.0001:
print('Amplitude error')
temp_duration = (sum(self._patch_list[patchname][cluster_no-1]['open_period'])
+
sum(self._patch_list[patchname][cluster_no-1]['shut_period']))
if abs(test_dict['duration'] - temp_duration) > 0.0001:
print('Duration error')
temp_popen = sum(self._patch_list[patchname][cluster_no-1]['open_period']) / temp_duration
if abs(test_dict['popen'] - temp_popen) > 0.0001:
print('Popen error')
# Test StretchSummary class
batch_analysis.compute_stretch_summary()
def test_batch_query(self):
'''
test the batch_query module.
'''
cluster_list = []
for patch in self._name_list:
test_patch = Patch(os.path.join(self._dir, patch))
test_patch.scan()
for cluster in test_patch:
cluster_list.append(cluster)
# Test search orded folder scan
choice_dict = OrderedDict([('receptor', ['NMDA', 'AMPA']),
('mutation', ['S219P', 'S200T']),
('composition', ['a1','ab']),
('agonist', ['glycine', 'taurine']),
('concentration', ['100', '0.1'])])
organisation = {keys:{key: [] for key in choice_dict[keys]} for keys in choice_dict}
for index in range(len(self._name_list)):
temp_filepath = self._dir
for key in choice_dict:
choice = random.choice(choice_dict[key])
temp_filepath = os.path.join(temp_filepath, choice)
organisation[key][choice].extend([index]*self.cluster_num)
try:
os.makedirs(temp_filepath)
except FileExistsError:
pass
shutil.copy2(os.path.join(self._dir, self._name_list[index]),
os.path.join(temp_filepath, self._name_list[index]))
test_batch = Batch(folder_list = self._dir)
if len(test_batch.scan_folder()) != len(cluster_list):
print('scan_folder error')
test_cluster_list = test_batch.scan_orded_folder(clear = True,export = True)
copy_organisation = copy.deepcopy(organisation)
for cluster in test_cluster_list:
index = self._name_list.index(cluster.patchname)
for key in organisation:
if not index in organisation[key][getattr(cluster, key)]:
print('scan_orded_folder error')
else:
copy_organisation[key][getattr(cluster, key)].remove(index)
for i in copy_organisation:
for j in copy_organisation[i]:
if copy_organisation[i][j]:
print('scan_orded_folder error')
# Test query
copy_organisation = copy.deepcopy(organisation)
for i in organisation:
for j in organisation[i]:
if organisation[i][j]:
query_list = test_batch.query(**{i: j})
for cluster in query_list:
index = self._name_list.index(cluster.patchname)
try:
copy_organisation[i][j].remove(index)
except ValueError:
print('query error')
for i in copy_organisation:
for j in copy_organisation[i]:
if copy_organisation[i][j]:
print('query error')
# Test filter
def finish_test(self):
'''
Delete the test file generate during the test.
'''
shutil.rmtree(self._dir)
class FunctionTest:
'''
Test for single function.
'''
def test_separate_multiply_line():
'''
Test separate_multiply_line from the plot_computation module.
'''
a = np.repeat(np.array(range(20)),2)
b=np.hstack((0, np.repeat(np.array(range(1,20)),2), 20))
new_a,new_b=separate_multiply_line(a,b,tracelength=3.5)
correct_a = \
[np.array([ 0. , 0. , 1. , 1. , 2. , 2. , 3. , 3. , 3.5]),
np.array([ 3.5, 4. , 4. , 5. , 5. , 6. , 6. , 7. , 7. ]),
np.array([ 8. , 8. , 9. , 9. , 10. , 10. , 10.5]),
np.array([ 10.5, 11. , 11. , 12. , 12. , 13. , 13. , 14. , 14. ]),
np.array([ 15. , 15. , 16. , 16. , 17. , 17. , 17.5]),
np.array([ 17.5, 18. , 18. , 19. , 19. ])]
correct_b = \
[np.array([0, 1, 1, 2, 2, 3, 3, 4, 4]),
np.array([4, 4, 5, 5, 6, 6, 7, 7, 8]),
np.array([ 8, 9, 9, 10, 10, 11, 11]),
np.array([11, 11, 12, 12, 13, 13, 14, 14, 15]),
np.array([15, 16, 16, 17, 17, 18, 18]),
np.array([18, 18, 19, 19, 20])]
for i in range(6):
if (new_a[i] == correct_a[i]).all() and (new_b[i] == correct_b[i]).all():
pass
else:
print('separate_multiply_line didn\'t pass test.')
def test_scn_write():
intervals = np.hstack((np.arange(1,501),np.arange(1,501)))
amplitudes = np.ones(1000)*-1
amplitudes[::2] = 0
amplitudes = amplitudes.astype('int')
flags = np.zeros(1000)
flags = flags.astype('int')
dcio.scn_write(intervals, amplitudes, flags, filename='./test.SCN')
new_patch = Patch('./test.SCN')
new_patch.read_scn(tres=0, tcrit=500)
new_patch.write_scn()
new_patch = Patch('./modified_test.SCN')
new_patch.read_scn(tres=0, tcrit=np.inf)
period = new_patch[1].period
print(sum(intervals[3:-1]-period))
#A = TestSuit()
#A.test_info()
#A.test_PlotComputation_using_PlotMPL()
#A.test_batch_analysis()
#A.test_batch_query()
#A.finish_test()
#FunctionTest.test_separate_multiply_line()
FunctionTest.test_scn_write()