Skip to content

Commit

Permalink
Fix various bugs in the drawing of events on spectrograms.
Browse files Browse the repository at this point in the history
Issue #297
  • Loading branch information
towsey committed Apr 30, 2020
1 parent cc768a2 commit 65a980c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/AnalysisBase/AnalysisResult2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public AnalysisResult2(AnalysisSettings settingsUsed, SegmentSettingsBase segmen
/// </summary>
public EventBase[] Events { get; set; }

/// <summary>
/// Gets or sets event results.
/// Should typically contain many results.
/// </summary>
public EventBase[] NewEvents { get; set; }

/// <summary>
/// Gets or sets summary indices results.
/// Should typically contain just 1 result.
Expand Down
20 changes: 3 additions & 17 deletions src/AnalysisBase/ResultBases/ResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ namespace AnalysisBase.ResultBases
/// </summary>
public abstract class ResultBase : IComparable<ResultBase>, IComparable
{
private double resultStartSeconds;

/// <summary>
/// Gets or sets the filename of the audio file this result produced.
/// </summary>
Expand All @@ -35,19 +33,7 @@ public abstract class ResultBase : IComparable<ResultBase>, IComparable
/// E.g. Given segment 78 of a 120min audio file, with a segment size of 60 seconds, this property would hold 78 minutes.
/// And again: StartOffset is the time offset between the start of the recording and the start of the current result.
/// </remarks>
public double ResultStartSeconds
{
get
{
return this.resultStartSeconds;
}

set
{
this.ResultMinute = (int)(value / 60.0);
this.resultStartSeconds = value;
}
}
public virtual double ResultStartSeconds { get; set; }

/// <summary>
/// Gets or sets the duration of audio segment that produced this result.
Expand All @@ -58,9 +44,9 @@ public double ResultStartSeconds

/// <summary>
/// Gets the ResultMinute.
/// This is an integer representation of <see cref="ResultStartSeconds"/>.
/// This is the floored integer minute of <see cref="ResultStartSeconds"/>.
/// </summary>
public int ResultMinute { get; private set; }
public int ResultMinute => (int)(this.ResultStartSeconds / 60.0);

/// <summary>
/// Defines an innate order of Analysis results based on the <c>SegmentStartOffset</c>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,15 @@ private static void Cleanup(Arguments args, FileInfo configFile)
{
if (args.WhenExitCopyConfig && args.Config.NotNull())
{
configFile.CopyTo(Path.Combine(args.Output.FullName, Path.GetFileName(args.Config)), true);
var destination = Path.Combine(args.Output.FullName, Path.GetFileName(args.Config));
if (configFile.FullName == destination)
{
Log.Warn("Cannot copy config file to output directory because source = destination.");
}
else
{
configFile.CopyTo(Path.Combine(args.Output.FullName, Path.GetFileName(args.Config)), true);
}
}

if (args.WhenExitCopyLog && MainEntry.Logging.LogFileName.NotNull())
Expand Down
16 changes: 12 additions & 4 deletions src/AnalysisPrograms/Recognizers/Base/RecognizerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace AnalysisPrograms.Recognizers.Base
using AnalysisBase;
using AnalysisBase.ResultBases;
using AudioAnalysisTools;
using AudioAnalysisTools.Events;
using AudioAnalysisTools.Events.Types;
using AudioAnalysisTools.Indices;
using AudioAnalysisTools.StandardSpectrograms;
using AudioAnalysisTools.WavTools;
Expand Down Expand Up @@ -100,12 +102,13 @@ public override AnalysisResult2 Analyze<T>(AnalysisSettings analysisSettings, Se

BaseSonogram sonogram = results.Sonogram;
double[,] hits = results.Hits;
var predictedEvents = results.Events;
var predictedEvents = results.GetAllEvents();

// double check all the events have the right offset in case it was missed
foreach (var predictedEvent in predictedEvents)
{
predictedEvent.SegmentStartSeconds = segmentSettings.SegmentStartOffset.TotalSeconds;
predictedEvent.SegmentDurationSeconds = recording.Duration.TotalSeconds;
}

analysisResults.Events = predictedEvents.ToArray();
Expand Down Expand Up @@ -147,7 +150,10 @@ public override AnalysisResult2 Analyze<T>(AnalysisSettings analysisSettings, Se
const double EventThreshold = 0.1;
var plots = results.Plots ?? new List<Plot>();

Image image = this.DrawSonogram(sonogram, hits, plots, predictedEvents, EventThreshold);
//TODO Remove this when we remove AcousticEvent.
var convertedEvents = predictedEvents.Select(ec => ec is AcousticEvent ae ? EventConverters.ConvertAcousticEventToSpectralEvent(ae) : (EventCommon)ec).ToList();

Image image = this.DrawSonogram(sonogram, hits, plots, convertedEvents, EventThreshold);
image.Save(imagePath);
analysisResults.ImageFile = segmentSettings.SegmentImageFile;

Expand Down Expand Up @@ -292,7 +298,9 @@ private void SummarizeHighResolutionIndices(

public override void WriteEventsFile(FileInfo destination, IEnumerable<EventBase> results)
{
Csv.WriteToCsv(destination, results.Select(x => (AcousticEvent)x));
// TODO check this
//Csv.WriteToCsv(destination, results.Select(x => (AcousticEvent)x));
Csv.WriteToCsv(destination, results.Select(x => (EventCommon)x));
}

public override void WriteSummaryIndicesFile(FileInfo destination, IEnumerable<SummaryIndexBase> results)
Expand Down Expand Up @@ -329,7 +337,7 @@ protected virtual Image DrawSonogram(
BaseSonogram sonogram,
double[,] hits,
List<Plot> scores,
List<AcousticEvent> predictedEvents,
List<EventCommon> predictedEvents,
double eventThreshold)
{
var image = SpectrogramTools.GetSonogramPlusCharts(sonogram, predictedEvents, scores, hits);
Expand Down
2 changes: 2 additions & 0 deletions src/AudioAnalysisTools/Events/Types/CompositeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public CompositeEvent(List<SpectralEvent> events)
public override double EventStartSeconds =>
this.ComponentEvents.Min(x => x.EventStartSeconds);

public override double ResultStartSeconds => this.EventStartSeconds;

public override double EventEndSeconds =>
this.ComponentEvents.Max(x => (x as ITemporalEvent)?.EventEndSeconds) ?? double.PositiveInfinity;

Expand Down
5 changes: 3 additions & 2 deletions src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT
// This will help in some cases to combine related events.
if (parameters.CombinePossibleSyllableSequence)
{
returnEvents = CompositeEvent.CombineSimilarProximalEvents(events, parameters.SyllableStartDifference, parameters.SyllableHertzGap);
var timeDiff = TimeSpan.FromSeconds(parameters.SyllableStartDifference);
returnEvents = CompositeEvent.CombineSimilarProximalEvents(events, timeDiff, parameters.SyllableHertzGap);
}

return (returnEvents, combinedIntensityArray);
Expand Down Expand Up @@ -229,7 +230,7 @@ public static Track GetForwardTrack(double[,] peaks, int startRow, int startBin,
track.SetPoint(row, bin, maxValue);

// next line is for debug purposes
var info = track.CheckPoint(row, bin);
//var info = track.CheckPoint(row, bin);
}

return track;
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/Tracks/ForwardTrackParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ForwardTrackParameters : CommonParameters
/// </summary>
public bool CombinePossibleSyllableSequence { get; set; }

public TimeSpan SyllableStartDifference { get; set; }
public double SyllableStartDifference { get; set; }

public int SyllableHertzGap { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public void TestForwardTrackAlgorithm()
HarmonicsStartDifference = TimeSpan.FromSeconds(0.2),
HarmonicsHertzGap = 200,
CombinePossibleSyllableSequence = false,
SyllableStartDifference = TimeSpan.FromSeconds(0.2),
SyllableStartDifference = 0.2,
SyllableHertzGap = 300,
};

Expand Down

0 comments on commit 65a980c

Please sign in to comment.