vendredi 11 septembre 2020

How to randomly adjust values in each array of a column without surpassing the bounds of an interval set by values in another column

I have the following data in a pandas dataframe:

FileName    Onsets              Offsets
FileName1   [0, 270.78, 763.33] [188.56, 727.28, 1252.90]
FileName2   [0, 634.34, 1166.57, 1775.95, 2104.01]  [472.04, 1034.37, 1575.88, 1970.79, 2457.09]
FileName3   [0, 560.97, 1332.21, 1532.47]   [356.79, 1286.26, 1488.54, 2018.61]

These are data from audio files. Each row contains a series of onset and offset times for each of the sounds I'm researching. This means that the numbers are coupled, e.g. the second offset time marks the end of the sound that began at the second onset time.

To test a hypothesis, I need to select random offset times within various ranges. For instance, I need to multiply each offset time by between 0.95 and 1.05 to create random adjustments within a +/- 5% range around the actual offset time. Then 0.90 to 1.10, and so forth.

Importantly, the adjustment needs to not push the offset time earlier or later than the preceding or subsequent onset time. I think this means that I need to initially calculate the largest acceptable adjustment for each offset time, and then set the maximum allowable time for the whole dataset to be whatever the lowest acceptable adjustment is. I'll be using this code for different datasets, so this maximum adjustment percentage shouldn't be hardcoded.

How can I code this function?

The code below generates adjustments, but I haven't figured out to calculate and set the bounds yet.

import random

Offsets_5 = Offsets*(random.uniform(0.95,1.05))
Offsets_10 = Offsets*(random.uniform(0.90,1.10))
Offsets_15 = Offsets*(random.uniform(0.85,1.15))



Aucun commentaire:

Enregistrer un commentaire