openworm / behavioral_syntax

behavioral syntax analysis based on the paper of Andre Brown
Other
5 stars 1 forks source link

find sub-sequence in array?? #3

Closed ghost closed 9 years ago

ghost commented 9 years ago

I can find sub-strings in a string with str.find() but how am I supposed to do this with arrays??

ghost commented 9 years ago

Could convert array to list, then convert list to string:

def stringer(y):
    x = str(y[0])
    for i in range(len(y)):
        x+=' '+str(y[i])
    return x

But, this is an ugly solution.

ghost commented 9 years ago

Here's a better solution:

def find_subsequence(seq, subseq):
    target = np.dot(subseq, subseq)
    candidates = np.where(np.correlate(seq, subseq, mode='valid') == target)[0]
    # some of the candidates entries may be false positives, double check
    check = candidates[:, np.newaxis] + np.arange(len(subseq))
    mask = np.all((np.take(seq, check) == subseq), axis=-1)
    return candidates[mask]