-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjpyx_org.pyx
646 lines (520 loc) · 15.5 KB
/
jpyx_org.pyx
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
import numpy as np
cimport numpy as np
import jchem
cdef extern from "jc.h":
int prt()
int prt_str( char*)
unsigned int bin_sum( unsigned int, unsigned int)
int sumup( int)
def prt_c():
prt()
def prt_str_c( str):
prt_str( str)
def sumup_c( int N):
return sumup( N)
#=================================================
def type_checkable( int x):
"""
This is cython code, which return x+1
arg: int x
"""
return x+1
#=================================================
def calc_corr( smiles_l, radius = 6, nBits = 4096):
"""
It emulate calc_corr in jchem using cython.
"""
xM = jchem.get_xM( smiles_l, radius = radius, nBits = nBits)
A = calc_tm_sim_M( xM)
return A
def calc_bin_sim_M( np.ndarray[np.long_t, ndim=2] xM, gamma = 1.0):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long)
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
A[ ix, iy] = gamma * float( c) / ( gamma * float( c) + a[ix] + a[iy] - 2*c)
return A
def calc_tm_sim_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long)
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c
A[ ix, iy] = float( c) / d
return A
def calc_tm_sim_MM( np.ndarray[np.long_t, ndim=2] xM_data, np.ndarray[np.long_t, ndim=2] xM_db):
"""
calc_tm_sim_MM : Calculate similarity between new and training binary vectors
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long), np.array( xM, dtype = long))
"""
cdef int ln_data = xM_data.shape[0]
cdef int ln_db = xM_db.shape[0]
cdef int lm = xM_data.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln_data,ln_db))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a_data = np.zeros( ln_data, dtype = long)
cdef np.ndarray[np.long_t, ndim=1] a_db = np.zeros( ln_db, dtype = long)
cdef int a_ix = 0
cdef int c, d
# a represents the number of on bits in a binary vector.
# Therefore, it should be divided to two variables such a_db and a_data
for ix in range( ln_data):
a_ix = 0
for ii in range( lm):
a_ix += xM_data[ix, ii]
#print ix, a_ix
a_data[ix] = a_ix
for ix in range( ln_db):
a_ix = 0
for ii in range( lm):
a_ix += xM_db[ix, ii]
#print ix, a_ix
a_db[ix] = a_ix
# The maximum bound of ix is changed from ln to ln_data since
# it is associated with the number of calculation molecules.
# The maximum bound of iy is changed from ln to ln_data since
# it is associated with the number of training molecules.
# lm is not changed since it should be the same for both new and training vectors.
# If lasso is used in regularization, the computation time for prediction can be
# further reduced by the use of sparsity in the training molecules where
# only part of training molecules will be used in the prediction process.
for ix in range( ln_data):
for iy in range( ln_db):
c = 0
for ii in range( lm):
c += xM_data[ix, ii] & xM_db[iy, ii]
if a_data[ix] == 0 and a_db[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a_data[ix] + a_db[iy] - c
A[ ix, iy] = float( c) / d
return A
def calc_RBF( np.ndarray[np.long_t, ndim=2] xM, float epsilon):
"""
calculate Radial basis function for xM with epsilon
in terms of direct distance
where distance is the number of different bits between the two vectors.
"""
d = calc_atm_dist_M( xM)
return RBF(d, epsilon)
def calc_rRBF( np.ndarray[np.long_t, ndim=2] xM, float epsilon):
"""
calculate Radial basis function for xM with epsilon
in terms of relative distance
where distance is the number of different bits between the two vectors.
"""
rd = calc_atm_rdist_M( xM)
return RBF(rd, epsilon)
def RBF( d, e = 1):
return np.exp( - e*np.power(d,2))
def calc_atm_dist_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long)
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - 2*c
A[ ix, iy] = d
return A
def calc_atm_rdist_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long)
Relative Tanimoto distance is calculated
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c # A or B
A[ ix, iy] = (float) (d-c) / d # A or B - A and B / A or B (relative distance)
return A
def calc_tm_dist_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long)
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c
A[ ix, iy] = float( d - c) / d
return A
def _calc_ec_sim_M_r0( np.ndarray[np.long_t, ndim=2] xM):
"""
Euclidean-tanimoto distance
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c
A[ ix, iy] = float(lm - d + c) / lm
return A
def _calc_ec_dist_M_r0( np.ndarray[np.long_t, ndim=2] xM):
"""
Euclidean-tanimoto distance
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c
A[ ix, iy] = float( d - c) / lm
return A
def calc_ec_sim_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Euclidean-tanimoto distance
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln), dtype = float)
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] ^ xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 1.0
else:
#d = a[ix] + a[iy] - c
A[ ix, iy] = float( lm - c) / lm
return A
def calc_ec_dist_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Euclidean-tanimoto distance
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] ^ xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
#d = a[ix] + a[iy] - c
A[ ix, iy] = float( c) / lm
return A
def bcalc_tm_sim_vec(int a, int b, int ln):
cdef int ii
cdef int a_and_b = a & b
cdef int a_or_b = a | b
cdef int a_and_b_sum = 0
cdef int a_or_b_sum = 0
for ii in range(ln):
a_and_b_sum += a_and_b & 1
a_and_b = a_and_b >> 1
a_or_b_sum += a_or_b & 1
a_or_b = a_or_b >> 1
return float(a_and_b_sum) / float(a_or_b_sum)
def calc_tm_sim_vec(np.ndarray[np.long_t, ndim=1] a, np.ndarray[np.long_t, ndim=1] b):
cdef int ii
cdef int a_and_b_sum = 0
cdef int a_or_b_sum = 0
cdef int ln = a.shape[0]
for ii in range( ln):
a_and_b_sum += a[ii] & b[ii]
a_or_b_sum += a[ii] | b[ii]
return float(a_and_b_sum) / float(a_or_b_sum)
"""
################################################################################
fast tm function is developed.
################################################################################
"""
def fast_calc_tm_sim_M( np.ndarray[np.long_t, ndim=2] xM):
"""
Ex. A = calc_tm_sim_M( np.array( xM, dtype = long))
"""
cdef int ln = xM.shape[0]
cdef int lm = xM.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] A = np.zeros( (ln,ln))
cdef int ix, iy, ii
cdef np.ndarray[np.long_t, ndim=1] a = np.zeros( ln, dtype = long)
cdef int a_ix = 0
cdef int c, d
for ix in range( ln):
a_ix = 0
for ii in range( lm):
a_ix += xM[ix, ii]
#print ix, a_ix
a[ix] = a_ix
for ix in range( ln):
for iy in range( ln):
c = 0
for ii in range( lm):
c += xM[ix, ii] & xM[iy, ii]
if a[ix] == 0 and a[iy] == 0:
A[ ix, iy] = 0.0
else:
d = a[ix] + a[iy] - c
A[ ix, iy] = float( c) / d
return A
def bin_sum_pyx( int a, int b):
"""
Sum of two binary integer variables.
bin_sum( int a, int b)
a: integer variable
b: integer variable
return a + b
"""
c = a + b
return c
def bin_sum_c( unsigned int a, unsigned int b):
return bin_sum( a, b)
"""
################################################################################
Communication routines are developed since 12th Jan., 2016
################################################################################
"""
def mld( r_l, mod_l = [-0.70710678, 0.70710678]):
"""
maximum likelihood detection
r_l: received signals after reception processing
mod_l: list of all modulation signals
BPSK: [-0.70710678, 0.70710678]
return the demodulated signals (0, 1, ...)
"""
sd_l = list() # store demodulated signal
for r in r_l:
dist = list() #Store distance
for m in mod_l:
d = np.power( np.abs( r - m), 2)
dist.append( d)
sd = np.argmin( dist)
sd_l.append( sd)
return np.array( sd_l)
def mld_list_fast( r_list, mod_list = [-0.70710678, 0.70710678]):
"""
maximum likelihood detection
r_l: received signals after reception processing
mod_l: list of all modulation signals
BPSK: [-0.70710678, 0.70710678]
return the demodulated signals (0, 1, ...)
"""
assert type(r_list) == list
assert type(mod_list) == list
cdef N_r = len( r_list)
cdef N_mod = len( mod_list)
cdef np.ndarray[np.float64_t, ndim=1] m_l = np.array( mod_list)
cdef np.ndarray[np.float64_t, ndim=1] r_l = np.array( r_list)
cdef np.ndarray[np.float64_t, ndim=1] dist = np.zeros( N_mod, dtype = float)
cdef np.ndarray[np.float64_t, ndim=1] sd_l = np.zeros( N_r, dtype = float)
cdef int i_m
# sd_l.fill(0) # this is not needed since sd_l is initiated by zeros()
for i_r in range( N_r):
dist.fill( 0) #Store distance
for i_m in range( N_mod):
dist[ i_m] = np.power( np.abs( r_l[ i_r] - m_l[ i_m]), 2)
sd_l[ i_r] = np.argmin( dist)
return sd_l
def _mld_fast_r0( np.ndarray[np.float64_t, ndim=1] r_a,
np.ndarray[np.float64_t, ndim=1] m_a):
"""
maximum likelihood detection
r_l: received signals after reception processing
mod_l: list of all modulation signals
BPSK: [-0.70710678, 0.70710678]
return the demodulated signals (0, 1, ...)
"""
assert type(r_a) == np.ndarray
assert type(m_a) == np.ndarray
cdef int N_r = r_a.shape[0]
cdef int N_mod = m_a.shape[0]
#cdef np.ndarray[np.float64_t, ndim=1] m_l = np.array( mod_list)
#cdef np.ndarray[np.float64_t, ndim=1] r_l = np.array( r_list)
cdef np.ndarray[np.float64_t, ndim=1] sd_a = np.zeros( N_r, dtype = float)
cdef np.ndarray[np.float64_t, ndim=1] dist_a = np.zeros( N_mod, dtype = float)
cdef int i_m
cdef int i_r
# sd_l.fill(0) # this is not needed since sd_l is initiated by zeros()
for i_r in range( N_r):
dist_a.fill( 0) #Store distance
for i_m in range( N_mod):
# dist_a[ i_m] = np.power( np.abs( r_a[ i_r] - m_a[ i_m]), 2)
dist_a[ i_m] = (r_a[ i_r] - m_a[ i_m]) * (r_a[ i_r] - m_a[ i_m])
sd_a[ i_r] = np.argmin( dist_a)
return sd_a
def mld_fast( np.ndarray[np.float64_t, ndim=1] r_a,
np.ndarray[np.float64_t, ndim=1] m_a):
"""
maximum likelihood detection
r_l: received signals after reception processing
mod_l: list of all modulation signals
BPSK: [-0.70710678, 0.70710678]
return the demodulated signals (0, 1, ...)
"""
assert type(r_a) == np.ndarray
assert type(m_a) == np.ndarray
cdef int N_r = r_a.shape[0]
cdef int N_mod = m_a.shape[0]
#cdef np.ndarray[np.float64_t, ndim=1] m_l = np.array( mod_list)
#cdef np.ndarray[np.float64_t, ndim=1] r_l = np.array( r_list)
cdef np.ndarray[np.float64_t, ndim=1] sd_a = np.zeros( N_r, dtype = float)
cdef np.ndarray[np.float64_t, ndim=1] dist_a = np.zeros( N_mod, dtype = float)
cdef int i_m
cdef int i_r
cdef int argmin_d
cdef float min_d, d
# sd_l.fill(0) # this is not needed since sd_l is initiated by zeros()
for i_r in range( N_r):
#dist_a.fill( 0) #Store distance
argmin_d = 0
min_d = (r_a[ i_r] - m_a[ 0]) * (r_a[ i_r] - m_a[ 0])
for i_m in range( 1, N_mod):
# dist_a[ i_m] = np.power( np.abs( r_a[ i_r] - m_a[ i_m]), 2)
# dist_a[ i_m] = (r_a[ i_r] - m_a[ i_m]) * (r_a[ i_r] - m_a[ i_m])
d = (r_a[ i_r] - m_a[ i_m]) * (r_a[ i_r] - m_a[ i_m])
if d < min_d:
argmin_d = i_m
min_d = d
sd_a[ i_r] = argmin_d
return sd_a