Skip to content

Commit

Permalink
Fix the normalisastion of event scores
Browse files Browse the repository at this point in the history
Issue #297 Now only store the normalised score in an event.
  • Loading branch information
towsey committed Apr 25, 2020
1 parent 7e4a8e9 commit bca9917
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
19 changes: 18 additions & 1 deletion src/AnalysisBase/ResultBases/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public abstract class EventBase : ResultBase
{
private double eventStartSeconds;

// For events, we store only the normalised event score.
// The setter ensures that the score lies in [0,1]
private double normalisedScore;

/// <summary>
/// Gets or sets the time (in seconds) from start of the file/recording to start of the current audio segment.
/// </summary>
Expand All @@ -27,7 +31,20 @@ public abstract class EventBase : ResultBase
public virtual double SegmentStartSeconds { get; set; }

//AudioAnalysisTools.Keys.EVENT_SCORE,
public virtual double Score { get; set; }
public virtual double Score
{
get
{
return this.normalisedScore;
}

set
{
// ensure the score lies in [0,1]
this.normalisedScore = Math.Max(0.0, value);
this.normalisedScore = Math.Min(1.0, value);
}
}

/// <summary>
/// Gets or sets the Event's Start Seconds.
Expand Down
23 changes: 13 additions & 10 deletions src/AudioAnalysisTools/Events/SpectralEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SpectralEvent : EventCommon, ISpectralEvent, ITemporalEvent
{
public SpectralEvent()
{
// empty constructor to prevent obligatory requiredment for arguments.
// empty constructor to prevent obligatory requirement for arguments.
}

public SpectralEvent(TimeSpan segmentStartOffset, double eventStartRecordingRelative, double eventEndRecordingRelative, double minFreq, double maxFreq)
Expand Down Expand Up @@ -48,22 +48,25 @@ public override void Draw(IImageProcessingContext graphics, EventRenderingOption
graphics.NoAA().DrawBorderInset(options.Border, border);
}

public void Draw(IImageProcessingContext graphics, EventRenderingOptions options, double scoreMax)
public void DrawWithAnnotation(IImageProcessingContext graphics, EventRenderingOptions options)
{
this.Draw(graphics, options);

//draw the score bar to indicate relative score
var topBin = options.Converters.HertzToPixels(this.HighFrequencyHertz);
var bottomBin = (int)Math.Round(options.Converters.HertzToPixels(this.LowFrequencyHertz));
var eventPixelHeight = bottomBin - topBin + 1;
var eventPixelStart = (int)Math.Round(options.Converters.SecondsToPixels(this.EventStartSeconds));
int scoreHt = (int)Math.Round(eventPixelHeight * this.Score / scoreMax);

var scorePen = new Pen(Color.LimeGreen, 1);
if (this.Score > 0.0)
{
//draw the score bar to indicate relative score
var bottomBin = (int)Math.Round(options.Converters.HertzToPixels(this.LowFrequencyHertz));
var eventPixelHeight = bottomBin - topBin + 1;
int scoreHt = (int)Math.Floor(eventPixelHeight * this.Score);
var scorePen = new Pen(Color.LimeGreen, 1);
graphics.NoAA().DrawLine(scorePen, eventPixelStart, bottomBin - scoreHt, eventPixelStart, bottomBin);
}

graphics.NoAA().DrawLine(scorePen, eventPixelStart, bottomBin - scoreHt, eventPixelStart, bottomBin);

graphics.DrawTextSafe(this.Name, Acoustics.Shared.ImageSharp.Drawing.Tahoma6, Color.Gray, new PointF(eventPixelStart, topBin - 4));
//TODO This text is not being drawn????????????????????????????????????????????????????????????????????????????????????????????????????????
graphics.DrawTextSafe(this.Name, Acoustics.Shared.ImageSharp.Drawing.Tahoma6, Color.DarkBlue, new PointF(eventPixelStart, topBin - 4));
}
}
}
3 changes: 3 additions & 0 deletions src/AudioAnalysisTools/Events/Types/ChirpEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class ChirpEvent : SpectralEvent, ITracks<Track>
public ChirpEvent(Track chirp)
{
this.Tracks.Add(chirp);

// set score = to aaverage normalised amplitude score.
//this.Score =
}

public List<Track> Tracks { get; private set; } = new List<Track>(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ public static Image<Rgb24> GetSonogramPlusCharts(
// ############################################################### THIS NEXT LINE NEEDS TO BE FIXED TO ENABLE ANY START TIME.
var segmentStartTime = TimeSpan.Zero;

// ############################################################### FIX THIS SCORE MAX.
double maxScore = 10;

// init with linear frequency scale and draw freq grid lines on image
int hertzInterval = 1000;
if (height < 200)
Expand All @@ -85,7 +82,7 @@ public static Image<Rgb24> GetSonogramPlusCharts(
foreach (SpectralEvent ev in events)
{
var options = new EventRenderingOptions(new UnitConverters(segmentStartTime.TotalSeconds, segmentDuration.TotalSeconds, nyquist, width, height));
spectrogram.Mutate(x => ev.Draw(x, options, maxScore));
spectrogram.Mutate(x => ev.Draw(x, options));
}
}

Expand Down
1 change: 0 additions & 1 deletion src/AudioAnalysisTools/Tracks/TrackExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT
var amplitudeTrack = track.GetAmplitudeOverTimeFrames();
for (int i = 0; i < amplitudeTrack.Length; i++)
{
//combinedIntensityArray[startRow + i] += amplitudeTrack[i];
combinedIntensityArray[startRow + i] = Math.Max(combinedIntensityArray[startRow + i], amplitudeTrack[i]);
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Acoustics.Test/AudioAnalysisTools/AcousticEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public void TestEventMerging()
[TestMethod]
public void TestSonogramWithEventsOverlay()
{
//####################################################################### THIS TEST FAILS BECAUSE NOT DRAWING EVENTS SAME AS PREVIOUSLY.
// make a substitute sonogram image
var imageWidth = 100;
var imageHeight = 256;
Expand All @@ -84,31 +83,33 @@ public void TestSonogramWithEventsOverlay()
var segmentDuration = 10.0; //seconds
var nyquist = 11025; //Hertz

// set a max score to normalise.
double maxScore = 10.0;

// make a list of two events
var events = new List<SpectralEvent>();
var segmentStartTime = TimeSpan.FromSeconds(10);
var event1 = new SpectralEvent(segmentStartOffset: segmentStartTime, eventStartRecordingRelative: 11.0, eventEndRecordingRelative: 16.0, minFreq: 1000, maxFreq: 8000)
{
Score = 10.0,
Score = 10.0 / maxScore,
Name = "Event1",
};

events.Add(event1);
var event2 = new SpectralEvent(segmentStartOffset: segmentStartTime, eventStartRecordingRelative: 17.0, eventEndRecordingRelative: 19.0, minFreq: 1000, maxFreq: 8000)
{
Score = 1.0,
Score = 1.0 / maxScore,
Name = "Event2",
};
events.Add(event2);

// now add events into the spectrogram image with score.
double maxScore = 10.0;
var options = new EventRenderingOptions(new UnitConverters(segmentStartTime.TotalSeconds, segmentDuration, nyquist, imageWidth, imageHeight));
foreach (var ev in events)
{
// because we are testing placement of box not text.
ev.Name = string.Empty;
substituteSonogram.Mutate(x => ev.Draw(x, options, maxScore));
substituteSonogram.Mutate(x => ev.DrawWithAnnotation(x, options));
}

this.ActualImage = substituteSonogram;
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/EventTests_SuperimposeEventsOnImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bca9917

Please sign in to comment.