Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust how far sliding window slides (step)

Tags:

python

I am trying to adjust how far the window slides in a sliding window. I see that there are a lot of posts about sliding windows on SO, however, I can’t seem to find a post that explains how to adjust how far the distance the sliding window slides. I am also not necessarily interested in chunking or only adjusting window size (1,2).

As an example If I had a string of six characters

seq = 'ATCGAC'

If I set the window size to 1 and I want the window to slide over 2 characters per step. I would want the following output:

Expected output:

['A', 'C', 'A']

Another example, if I have the same string and want to set the window size to 3 and the window to slide over 3 characters at a time. I would want the following output:

Expected output:

['ATC', 'GAC']

As a final example, a window size with a long string. With a sliding window size of 3 and adjusting the slide to slide over 6 characters at a time:

seq = 'ATCGACATCGACATCGAC'

Expected output:

['ATC', 'ATC', 'ATC']
like image 835
neuron Avatar asked Nov 19 '25 20:11

neuron


1 Answers

I'm sure there are more elegant solutions to this. Whenever I find myself using range(len(some_iterable)) I feel a little dirty.

That being said, you could achieve this with a simple generator.

def window(s: str, size: int, slide_amount: int):
    str_len = len(s)
    for i in range(0, str_len, slide_amount):
        # make sure nothing is yielded smaller than
        # the desired size
        if i + size <= str_len:
            yield s[i:i + size]

print([i for i in window('ATCGAC', 1, 2)]) # ['A', 'C', 'A']
print([i for i in window('ATCGAC', 3, 3)]) # ['ATC', 'GAC']
print([i for i in window('ATCGACATCGACATCGAC', 3, 6)]) # ['ATC', 'ATC', 'ATC']

Alternatively as a function wrapper around a generator expression.

def window(s: str, size: int, slide_amount: int):
    return (
        s[i:i + size] 
        for i in range(0, len(s), slide_amount) 
        if i + size <= len(s)
    )

Which could easily be modified to return a list instead.

def window(s: str, size: int, slide_amount: int):
    return [
        s[i:i + size] 
        for i in range(0, len(s), slide_amount) 
        if i + size <= len(s)
    ]
like image 117
Axe319 Avatar answered Nov 22 '25 11:11

Axe319



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!