Skip to main content
added 400 characters in body
Source Link
Mathieu
  • 251
  • 1
  • 10
import numpy as np

t0 = 0
tf = 300
fq = 60

t1 = np.round(np.arange(t0, tf, 1/fq*1000), 3)
t2 = np.round(np.linspace(t0, int(tf-(1/fq*1000)), int((tf-t0)*fq/1000)), 31)

And for the find_closest_t:

def find_closest_t(timeline, t):
    id = np.searchsorted(timeline, t)
    if id == 0:
        return timeline[0], id
    else:
        return timeline[id-1], id-1
import numpy as np

t0 = 0
tf = 300
fq = 60

t1 = np.round(np.arange(t0, tf, 1/fq*1000), 3)
t2 = np.round(np.linspace(t0, int(tf-(1/fq*1000)), int((tf-t0)*fq/1000)), 3)
import numpy as np

t0 = 0
tf = 300
fq = 60

t1 = np.round(np.arange(t0, tf, 1/fq*1000), 3)
t2 = np.round(np.linspace(t0, int(tf-(1/fq*1000)), int((tf-t0)*fq/1000)), 1)

And for the find_closest_t:

def find_closest_t(timeline, t):
    id = np.searchsorted(timeline, t)
    if id == 0:
        return timeline[0], id
    else:
        return timeline[id-1], id-1
added 284 characters in body
Source Link
Mathieu
  • 251
  • 1
  • 10

EDIT2: In regards of the answer, the correct linspace() is:

import numpy as np

t0 = 0
tf = 300
fq = 60

t1 = np.round(np.arange(t0, tf, 1/fq*1000), 3)
t2 = np.round(np.linspace(t0, int(tf-(1/fq*1000)), int((tf-t0)*fq/1000)), 3)

EDIT2: In regards of the answer, the correct linspace() is:

import numpy as np

t0 = 0
tf = 300
fq = 60

t1 = np.round(np.arange(t0, tf, 1/fq*1000), 3)
t2 = np.round(np.linspace(t0, int(tf-(1/fq*1000)), int((tf-t0)*fq/1000)), 3)
added 1777 characters in body
Source Link
Mathieu
  • 251
  • 1
  • 10

EDIT

Following the comment, numpy implementation of duo_overlap:

def duo_overlap_np(S1, S2, perc):
    p1_overlapping = np.zeros(shape = (len(S1.timeline)))
    p2_overlapping = np.zeros(shape = (len(S2.timeline)))
    
    start = max(S1.t0, S2.t0) - max(S1.width, S2.width)
    if start <= 0:
        start = 0
        start_id_S1 = 0
        start_id_S2 = 0
    else:
        start_id_S1 = S1.find_closest_t(start)[1]
        start_id_S2 = S2.find_closest_t(start)[1]
    
    stop = min(S1.tf, S2.tf) + max(S1.width, S2.width)
    
    for i in range(start_id_S1, len(S1.timeline)):
        if S1.timeline[i] > stop:
            break
        
        for j in range(start_id_S2, len(S2.timeline)):
            if S2.timeline[j] > stop:
                break
            
            d = round(abs(S2.timeline[j] - S1.timeline[i]), 3)  # Computation of the distance of the 2 point
            
            if S1.timeline[i] <= S2.timeline[j] and d < S1.width:
                p1_overlapping[i] = 1
                p2_overlapping[j] = 1
                continue
            
            elif S1.timeline[i] >= S2.timeline[j] and d < S2.width:
                p1_overlapping[i] = 1
                p2_overlapping[j] = 1
                continue
            
            else:
                continue
    
    return list(np.nonzero(p1_overlapping)[0]), list(np.nonzero(p2_overlapping)[0])

I'm quite sure I didn't get it right since it doesn't improve much. On 50 calls I get the following timing in second:

 Old: 8.20273494720459
 New: 8.188030004501343

EDIT

Following the comment, numpy implementation of duo_overlap:

def duo_overlap_np(S1, S2, perc):
    p1_overlapping = np.zeros(shape = (len(S1.timeline)))
    p2_overlapping = np.zeros(shape = (len(S2.timeline)))
    
    start = max(S1.t0, S2.t0) - max(S1.width, S2.width)
    if start <= 0:
        start = 0
        start_id_S1 = 0
        start_id_S2 = 0
    else:
        start_id_S1 = S1.find_closest_t(start)[1]
        start_id_S2 = S2.find_closest_t(start)[1]
    
    stop = min(S1.tf, S2.tf) + max(S1.width, S2.width)
    
    for i in range(start_id_S1, len(S1.timeline)):
        if S1.timeline[i] > stop:
            break
        
        for j in range(start_id_S2, len(S2.timeline)):
            if S2.timeline[j] > stop:
                break
            
            d = round(abs(S2.timeline[j] - S1.timeline[i]), 3)  # Computation of the distance of the 2 point
            
            if S1.timeline[i] <= S2.timeline[j] and d < S1.width:
                p1_overlapping[i] = 1
                p2_overlapping[j] = 1
                continue
            
            elif S1.timeline[i] >= S2.timeline[j] and d < S2.width:
                p1_overlapping[i] = 1
                p2_overlapping[j] = 1
                continue
            
            else:
                continue
    
    return list(np.nonzero(p1_overlapping)[0]), list(np.nonzero(p2_overlapping)[0])

I'm quite sure I didn't get it right since it doesn't improve much. On 50 calls I get the following timing in second:

 Old: 8.20273494720459
 New: 8.188030004501343
Source Link
Mathieu
  • 251
  • 1
  • 10
Loading