Skip to content

Commit

Permalink
Fixes a bug in EventStatistics
Browse files Browse the repository at this point in the history
This patch ensures that the audio recording duration used by event statistics is always rounded down to the nearest second. There is a subtle conflict in logic later on in the event downloading  code that requires audio segments be rounded to the nearest second. If the recording duration is greater than 0.5, and the event ends at the end of the recording, then the requested media is rounded up to the next second, past the end of the recording. This patch prevents that error.
  • Loading branch information
atruskie committed May 8, 2018
1 parent 2ffac42 commit f5e027a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Acoustics.Shared/Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ public static double RoundToSignficantDigits(this double d, int digits)
decimal scale = (decimal)Math.Pow(10, Math.Floor(Math.Log10((double)Math.Abs(dec))) + 1);
return (double)(scale * Math.Round(dec / scale, digits, MidpointRounding.AwayFromZero));
}

public static double Floor(this double d) => Math.Floor(d);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ private async Task<IEnumerable<RemoteSegmentWithData>> DownloadRemoteMetadata(
$"Metadata for audio recording media {audioRecordingId} retrieved, generating segments for associated events");

// now generate the segments
var limit = audioRecording.DurationSeconds.AsRangeFromZero();
// we need to floor the duration to ensure later rounding does round past the limit of the recording
Log.Verbose($"Audio recording duration {audioRecording.DurationSeconds} will be floored");
var limit = audioRecording.DurationSeconds.Floor().AsRangeFromZero();
var results = new List<RemoteSegmentWithData>(20);
foreach (var importedEvent in events)
{
Expand Down
10 changes: 10 additions & 0 deletions src/AnalysisPrograms/EventStatistics/ImportedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AnalysisPrograms.EventStatistics
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Acoustics.Shared;
using AcousticWorkbench;
using CsvHelper.Configuration;
using log4net;
Expand Down Expand Up @@ -44,6 +45,15 @@ public bool IsValid()
&& this.HighFrequencyHertz.HasValue);
}

/// <summary>
/// Returns a JSON encoded string that describes this object.
/// </summary>
/// <returns>The string that describes this object.</returns>
public override string ToString()
{
return Json.SerialiseToString(this);
}

public sealed class ImportedEventNameClassMap : CsvClassMap<ImportedEvent>
{
private static readonly PropertyInfo[] Properties = typeof(ImportedEvent).GetProperties();
Expand Down

0 comments on commit f5e027a

Please sign in to comment.