Skip to content

Commit

Permalink
Add tests and implementation that allows assertions about overlap rat…
Browse files Browse the repository at this point in the history
…io for a scale's frequency bands
  • Loading branch information
JohnVinyard committed Sep 2, 2017
1 parent 1aa2165 commit aaf1401
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
17 changes: 16 additions & 1 deletion zounds/spectral/frequencyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def bandwidth_ratio(self, other):

def intersection_ratio(self, other):
intersection = self.intersect(other)
print intersection
return self.bandwidth_ratio(intersection)

@staticmethod
Expand Down Expand Up @@ -117,6 +116,22 @@ def center_frequencies(self):
def bandwidths(self):
return (band.bandwidth for band in self)

def ensure_overlap_ratio(self, required_ratio=0.5):
msg = \
'band {i}: ratio must be at least {required_ratio} but was {ratio}'

for i in xrange(0, len(self) - 1):
b1 = self[i]
b2 = self[i + 1]

try:
ratio = b1.intersection_ratio(b2)
except ValueError:
ratio = 0

if ratio < required_ratio:
raise AssertionError(msg.format(**locals()))

@property
def Q(self):
"""
Expand Down
51 changes: 51 additions & 0 deletions zounds/spectral/test_frequencyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ def test_identical_frequency_bands_have_same_hash_value(self):
fb2 = FrequencyBand.from_center(1000, 50)
self.assertEqual(hash(fb1), hash(fb2))

def test_can_intersect(self):
fb1 = FrequencyBand(0, 100)
fb2 = FrequencyBand(50, 150)
intersection = fb1.intersect(fb2)
self.assertEqual(FrequencyBand(50, 100), intersection)

def test_error_raised_when_no_intersection(self):
fb1 = FrequencyBand(0, 100)
fb2 = FrequencyBand(200, 500)
self.assertRaises(ValueError, lambda: fb1.intersect(fb2))

def test_intersection_ratio(self):
fb1 = FrequencyBand(0, 100)
fb2 = FrequencyBand(50, 150)
ratio = fb1.intersection_ratio(fb2)
self.assertEqual(0.5, ratio)


class FrequencyScaleTests(unittest2.TestCase):
def test_can_get_all_even_sized_bands(self):
Expand Down Expand Up @@ -138,6 +155,40 @@ def test_slicing_geometric_scale_returns_explicit_scale(self):
sliced = scale[FrequencyBand(100, 1000)]
self.assertIsInstance(sliced, ExplicitScale)

def test_ensure_minimal_inersection_ratio_no_overlap(self):
scale = GeometricScale(
start_center_hz=300,
stop_center_hz=3030,
bandwidth_ratio=0.001,
n_bands=300)

self.assertRaises(
AssertionError,
lambda: scale.ensure_overlap_ratio(0.5))

def test_ensure_minimal_inersection_ratio_insufficient_overlap(self):
scale = GeometricScale(
start_center_hz=300,
stop_center_hz=3030,
bandwidth_ratio=0.01,
n_bands=300)

self.assertRaises(
AssertionError,
lambda: scale.ensure_overlap_ratio(0.5))

def test_ensure_minimal_intersection_ratio(self):
scale = GeometricScale(
start_center_hz=300,
stop_center_hz=3030,
bandwidth_ratio=0.017,
n_bands=300)

try:
scale.ensure_overlap_ratio(0.5)
except AssertionError:
self.fail('AssertionError was raised')


class ExplicitScaleTests(unittest2.TestCase):
def test_can_construct_explicit_scale_from_scale(self):
Expand Down

0 comments on commit aaf1401

Please sign in to comment.