From f080005ea0c81adb34e3ab5380fd8a015d6e755b Mon Sep 17 00:00:00 2001 From: towsey Date: Fri, 30 Oct 2020 17:50:26 +1100 Subject: [PATCH] Wrote unit tests for Event filters Issue #390 While writing tests for unit methods made some changes to boundary conditions for hertz and duration values. More unit tests could be done. --- src/AudioAnalysisTools/Events/EventFilters.cs | 12 +- .../Events/EventFilterTests.cs | 364 +++++++++++++++++- 2 files changed, 369 insertions(+), 7 deletions(-) diff --git a/src/AudioAnalysisTools/Events/EventFilters.cs b/src/AudioAnalysisTools/Events/EventFilters.cs index d10b83bc2..0af033b4e 100644 --- a/src/AudioAnalysisTools/Events/EventFilters.cs +++ b/src/AudioAnalysisTools/Events/EventFilters.cs @@ -62,13 +62,13 @@ public static List FilterOnBandwidth(List events, doub } var maxBandwidth = average + (sd * sigmaThreshold); - var outputEvents = events.Where(ev => ((SpectralEvent)ev).BandWidthHertz > minBandwidth && ((SpectralEvent)ev).BandWidthHertz < maxBandwidth).ToList(); + var outputEvents = events.Where(ev => ((SpectralEvent)ev).BandWidthHertz >= minBandwidth && ((SpectralEvent)ev).BandWidthHertz <= maxBandwidth).ToList(); return outputEvents; } public static List FilterOnBandwidth(List events, double minBandwidth, double maxBandwidth) { - var outputEvents = events.Where(ev => ((SpectralEvent)ev).BandWidthHertz > minBandwidth && ((SpectralEvent)ev).BandWidthHertz < maxBandwidth).ToList(); + var outputEvents = events.Where(ev => ((SpectralEvent)ev).BandWidthHertz >= minBandwidth && ((SpectralEvent)ev).BandWidthHertz <= maxBandwidth).ToList(); return outputEvents; } @@ -77,7 +77,7 @@ public static List FilterOnBandwidth(List events, doub /// public static List FilterShortEvents(List events, double minimumDurationSeconds) { - var outputEvents = events.Where(ev => ev.EventDurationSeconds > minimumDurationSeconds).ToList(); + var outputEvents = events.Where(ev => ev.EventDurationSeconds >= minimumDurationSeconds).ToList(); return outputEvents; } @@ -86,7 +86,7 @@ public static List FilterShortEvents(List events, /// public static List FilterLongEvents(List events, double maximumDurationSeconds) { - var outputEvents = events.Where(ev => ev.EventDurationSeconds < maximumDurationSeconds).ToList(); + var outputEvents = events.Where(ev => ev.EventDurationSeconds <= maximumDurationSeconds).ToList(); return outputEvents; } @@ -95,7 +95,7 @@ public static List FilterLongEvents(List events, d /// public static List FilterOnDuration(List events, double minimumDurationSeconds, double maximumDurationSeconds) { - var outputEvents = events.Where(ev => ((SpectralEvent)ev).EventDurationSeconds > minimumDurationSeconds && ((SpectralEvent)ev).EventDurationSeconds < maximumDurationSeconds).ToList(); + var outputEvents = events.Where(ev => ((SpectralEvent)ev).EventDurationSeconds >= minimumDurationSeconds && ((SpectralEvent)ev).EventDurationSeconds <= maximumDurationSeconds).ToList(); return outputEvents; } @@ -117,7 +117,7 @@ public static List FilterOnDuration(List events, doubl } var maxDuration = average + (sd * sigmaThreshold); - var outputEvents = events.Where(ev => ((SpectralEvent)ev).EventDurationSeconds > minDuration && ((SpectralEvent)ev).EventDurationSeconds < maxDuration).ToList(); + var outputEvents = events.Where(ev => ((SpectralEvent)ev).EventDurationSeconds >= minDuration && ((SpectralEvent)ev).EventDurationSeconds <= maxDuration).ToList(); return outputEvents; } diff --git a/tests/Acoustics.Test/AudioAnalysisTools/Events/EventFilterTests.cs b/tests/Acoustics.Test/AudioAnalysisTools/Events/EventFilterTests.cs index 4e47aad2d..6e1b4f422 100644 --- a/tests/Acoustics.Test/AudioAnalysisTools/Events/EventFilterTests.cs +++ b/tests/Acoustics.Test/AudioAnalysisTools/Events/EventFilterTests.cs @@ -15,7 +15,7 @@ namespace Acoustics.Test.AudioAnalysisTools.Events public class EventFilterTests { [TestMethod] - public void TestRemoveEnclosedEvent() + public void TestRemoveEnclosedEvents() { var ev1 = new SpectralEvent() { @@ -125,5 +125,367 @@ public void TestRemoveEnclosedEvent() Assert.AreEqual(500, @event.HighFrequencyHertz, 0.1); Assert.AreEqual("ev2", @event.Name); } + + [TestMethod] + public void TestFilterEventsOnBandwidth1() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 2000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterOnBandwidth(listOfEvents, minBandwidth: 100, maxBandwidth: 200); + + Assert.AreEqual(2, events.Count); + var @event = (SpectralEvent)events[0]; + + Assert.AreEqual(400, @event.LowFrequencyHertz, 0.1); + Assert.AreEqual(600, @event.HighFrequencyHertz, 0.1); + Assert.AreEqual("ev2", @event.Name); + + @event = (SpectralEvent)events[1]; + Assert.AreEqual(400, @event.LowFrequencyHertz, 0.1); + Assert.AreEqual(500, @event.HighFrequencyHertz, 0.1); + Assert.AreEqual("ev3", @event.Name); + } + + [TestMethod] + public void TestFilterEventsOnBandwidth2() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 2000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 9, + EventStartSeconds = 1, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterOnBandwidth(listOfEvents, average: 150, sd: 20, sigmaThreshold: 3.0); + + Assert.AreEqual(2, events.Count); + var @event = (SpectralEvent)events[0]; + + Assert.AreEqual(400, @event.LowFrequencyHertz, 0.1); + Assert.AreEqual(600, @event.HighFrequencyHertz, 0.1); + Assert.AreEqual("ev2", @event.Name); + + @event = (SpectralEvent)events[1]; + Assert.AreEqual(400, @event.LowFrequencyHertz, 0.1); + Assert.AreEqual(500, @event.HighFrequencyHertz, 0.1); + Assert.AreEqual("ev3", @event.Name); + } + + [TestMethod] + public void TestFilterShortEvents() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 6.001, + EventStartSeconds = 1.0, + HighFrequencyHertz = 3000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9.5, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 6.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 4.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterShortEvents(listOfEvents, minimumDurationSeconds: 5.0); + + Assert.AreEqual(3, events.Count); + var @event = (SpectralEvent)events[2]; + Assert.AreEqual(1.0, @event.EventStartSeconds, 0.1); + Assert.AreEqual(6.0, @event.EventEndSeconds, 0.1); + Assert.AreEqual("ev3", @event.Name); + } + + [TestMethod] + public void TestFilterLongEvents() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 6.001, + EventStartSeconds = 1.0, + HighFrequencyHertz = 3000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9.5, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 6.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 4.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterLongEvents(listOfEvents, maximumDurationSeconds: 5.0); + + Assert.AreEqual(2, events.Count); + var @event = (SpectralEvent)events[0]; + Assert.AreEqual(1.0, @event.EventStartSeconds, 0.1); + Assert.AreEqual(6.0, @event.EventEndSeconds, 0.1); + Assert.AreEqual("ev3", @event.Name); + } + + [TestMethod] + public void TestFilterOnDuration1() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 6.001, + EventStartSeconds = 1.0, + HighFrequencyHertz = 3000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9.5, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 6.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 4.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterOnDuration(listOfEvents, minimumDurationSeconds: 4.0, maximumDurationSeconds: 5.0); + + Assert.AreEqual(1, events.Count); + var @event = (SpectralEvent)events[0]; + Assert.AreEqual(1.0, @event.EventStartSeconds, 0.1); + Assert.AreEqual(6.0, @event.EventEndSeconds, 0.1); + Assert.AreEqual("ev3", @event.Name); + } + + [TestMethod] + public void TestFilterOnDuration2() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 6.001, + EventStartSeconds = 1.0, + HighFrequencyHertz = 3000, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 9.5, + EventStartSeconds = 1, + HighFrequencyHertz = 600, + LowFrequencyHertz = 400, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 6.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 500, + LowFrequencyHertz = 400, + }; + + var ev4 = new SpectralEvent() + { + Name = "ev4", + EventEndSeconds = 4.0, + EventStartSeconds = 1.0, + HighFrequencyHertz = 450, + LowFrequencyHertz = 400, + }; + + // test with combination of events + var listOfEvents = new List() { ev1, ev2, ev3, ev4 }; + var events = EventFilters.FilterOnDuration(listOfEvents, average: 5.0, sd: 0.1, sigmaThreshold: 3.0); + + Assert.AreEqual(2, events.Count); + var @event = (SpectralEvent)events[0]; + Assert.AreEqual(1.0, @event.EventStartSeconds, 0.1); + Assert.AreEqual(6.0, @event.EventEndSeconds, 0.1); + Assert.AreEqual("ev1", @event.Name); + } + + [TestMethod] + public void TestTemporalFootprintOfCompositeEvent() + { + var ev1 = new SpectralEvent() + { + Name = "ev1", + EventEndSeconds = 1.5, + EventStartSeconds = 1.0, + HighFrequencyHertz = 300, + LowFrequencyHertz = 200, + }; + + var ev2 = new SpectralEvent() + { + Name = "ev2", + EventEndSeconds = 2.5, + EventStartSeconds = 2.0, + HighFrequencyHertz = 310, + LowFrequencyHertz = 210, + }; + + var ev3 = new SpectralEvent() + { + Name = "ev3", + EventEndSeconds = 3.5, + EventStartSeconds = 3.0, + HighFrequencyHertz = 320, + LowFrequencyHertz = 220, + }; + + // test with combination of events + var events = new List() { ev1, ev2, ev3 }; + var compositeEvents = CompositeEvent.CombineProximalEvents(events: events, startDifference: TimeSpan.FromSeconds(1.1), hertzDifference: 50); + var compositeEvent = compositeEvents[0]; + (bool[] footprint, double timesScale) = EventFilters.GetTemporalFootprint(compositeEvent); + + Assert.AreEqual(100, footprint.Length); + Assert.AreEqual(0.025, timesScale); + Assert.AreEqual(true, footprint[10]); + Assert.AreEqual(false, footprint[20]); + Assert.AreEqual(false, footprint[30]); + Assert.AreEqual(true, footprint[40]); + Assert.AreEqual(true, footprint[50]); + Assert.AreEqual(false, footprint[60]); + Assert.AreEqual(false, footprint[70]); + Assert.AreEqual(true, footprint[80]); + Assert.AreEqual(true, footprint[90]); + Assert.AreEqual(true, footprint[99]); + } } }