Skip to content

Commit

Permalink
Merge pull request #47 from bmcfee/random-output-selection
Browse files Browse the repository at this point in the history
randomized target annotation selection
  • Loading branch information
bmcfee committed Mar 20, 2017
2 parents b928ce2 + 34ebce0 commit 7879d33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 11 additions & 5 deletions pumpp/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@ def sample(self, data, interval):
data_slice = dict()

for key in data:
if key in self._time:
index = [slice(None)] * data[key].ndim
if '_valid' in key:
continue

if self._time[key] is not None:
index[self._time[key]] = interval
index = [slice(None)] * data[key].ndim

data_slice[key] = data[key][index]
# if we have multiple observations for this key, pick one
index[0] = np.random.randint(0, data[key].shape[0])
index[0] = slice(index[0], index[0] + 1)

if self._time.get(key, None) is not None:
index[self._time[key]] = interval

data_slice[key] = data[key][index]

return data_slice

Expand Down
10 changes: 6 additions & 4 deletions tests/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def ops(sr, hop_length):
win_length=hop_length))

# A time-varying annotation
ops.append(pumpp.task.BeatTransformer(name='beat', sr=sr,
hop_length=hop_length))
ops.append(pumpp.task.ChordTransformer(name='chord', sr=sr,
hop_length=hop_length))

# And a static annotation
ops.append(pumpp.task.VectorTransformer(namespace='vector',
Expand Down Expand Up @@ -80,10 +80,12 @@ def test_sampler(data, ops, n_samples, duration):
# Now test that shape is preserved in the right way
for key in datum:
ref_shape = list(data[key].shape)
if sampler._time[key] is not None:
if sampler._time.get(key, None) is not None:
ref_shape[sampler._time[key]] = duration

assert list(datum[key].shape) == ref_shape
# Check that all keys have length=1
assert datum[key].shape[0] == 1
assert list(datum[key].shape[1:]) == ref_shape[1:]

# Test that we got the right number of samples out
if n_samples is None:
Expand Down

0 comments on commit 7879d33

Please sign in to comment.