How to separate time ranges/intervals into bins if intervals occur over multiple bins
<p>I have a dataset which consists of pairs of start-end times (say seconds) of something happening across a recorded period of time. For example:</p>

<pre><code>#each tuple includes (start, stop) of the event happening
data = [(0, 1), (5,8), (14,21), (29,30)]
</code></pre>

<p>I want to quantify what percentage of the time this thing is happening within bins of any size that I desire. For example if I wanted bins of 5 seconds each I would like a function that would:</p>

<ul>
<li>split up any tuples that cross over into multiple bins</li>
<li>add up the total time event occurs and divide by bin size to get time event occurred during each bin</li>
</ul>

<p>I'm mostly having trouble with the first point.</p>

<p>Ideally it would look something like this where bin_times is the function that I need help writing, and the output is what the function would return:</p>

<pre><code>data = [(0, 1), (5,8), (15,21), (29,30)]
bin_times(data, bin_size=5, total_length=40)

&gt;&gt; [20, 60, 0, 100, 20, 20, 0, 0]
</code></pre>