Skip to content

Commit 334415c

Browse files
committed
Cythonize get_coinc_indices
1 parent 47380b2 commit 334415c

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

pycbc/events/coherent.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@
2727
import logging
2828
import numpy as np
2929

30+
from .eventmgr_cython import get_coinc_indexes_cython_twodet_twocoinc
31+
3032
logger = logging.getLogger('pycbc.events.coherent')
3133

34+
def get_coinc_indexes_twodet_twocoinc(idxarr1, idxarr2, offset1, offset2, output):
35+
num_idxs = get_coinc_indexes_cython_twodet_twocoinc(
36+
idxarr1,
37+
idxarr2,
38+
offset1,
39+
offset2,
40+
output
41+
)
42+
return num_idxs
43+
3244

3345
def get_coinc_indexes(idx_dict, time_delay_idx, min_nifos):
3446
"""Return the indexes corresponding to coincident triggers. If only one
@@ -53,6 +65,20 @@ def get_coinc_indexes(idx_dict, time_delay_idx, min_nifos):
5365
List of indexes for triggers in geocent time that appear in
5466
multiple detectors
5567
"""
68+
if min_nifos == 2 and len(idx_dict) == 2:
69+
ifos = list(idx_dict.keys())
70+
# Could cache an output array if needed
71+
idxarr1 = idx_dict[ifos[0]]
72+
idxarr2 = idx_dict[ifos[1]]
73+
outarr = np.zeros(max(len(idxarr1), len(idxarr2)), dtype=idxarr1.dtype)
74+
num_idxs = get_coinc_indexes_twodet_twocoinc(
75+
idxarr1,
76+
idxarr2,
77+
time_delay_idx[ifos[0]],
78+
time_delay_idx[ifos[1]],
79+
outarr
80+
)
81+
return outarr[:num_idxs]
5682
coinc_list = np.array([], dtype=int)
5783
for ifo in idx_dict.keys():
5884
# Create list of indexes above single detector threshold, in geocent

pycbc/events/eventmgr_cython.pyx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
cimport numpy as cnp
3+
import cython
34
from cython import wraparound, boundscheck, cdivision
45
from libc.math cimport M_PI, sqrt
56
from libc.math cimport round as cround
@@ -306,3 +307,39 @@ def timecluster_cython(
306307
elif max_loc < i:
307308
i += 1
308309
return j
310+
311+
ctypedef fused numeric_type:
312+
cython.int
313+
cython.long
314+
315+
@cython.boundscheck(False)
316+
@cython.wraparound(False)
317+
def get_coinc_indexes_cython_twodet_twocoinc(
318+
numeric_type [::1] idxarr1,
319+
numeric_type [::1] idxarr2,
320+
numeric_type offset1,
321+
numeric_type offset2,
322+
numeric_type [::1] outputs
323+
):
324+
cdef int arr1pos = 0
325+
cdef int arr2pos = 0
326+
cdef int outpos = 0
327+
cdef int arr1_size = idxarr1.size
328+
cdef int arr2_size = idxarr2.size
329+
cdef int cpos1
330+
cdef int cpos2
331+
332+
while arr1pos < arr1_size and arr2pos < arr2_size:
333+
cpos1 = idxarr1[arr1pos] - offset1
334+
cpos2 = idxarr2[arr2pos]-offset2
335+
if (cpos1) == (cpos2):
336+
outputs[outpos] = cpos1
337+
outpos += 1
338+
arr1pos += 1
339+
arr2pos += 1
340+
elif cpos1 < cpos2:
341+
arr1pos += 1
342+
else:
343+
arr2pos += 1
344+
return outpos
345+

0 commit comments

Comments
 (0)