Skip to content

Commit

Permalink
Add json output for new events
Browse files Browse the repository at this point in the history
Also, simplify ScoreNormalized (and now uses american spelling).

Also, removes erroneous entry from audio analysis tools csproj
  • Loading branch information
atruskie committed May 11, 2020
1 parent 08de2da commit c63aa6c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/Acoustics.Shared/Extensions/IntervalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public static Interval<double> Invert(this Interval<double> range)
return new Interval<double>(1 / range.Maximum, 1 / range.Minimum, range.Topology);
}

public static double Normalize(this Interval<double> range, double value)
{
return (value - range.Minimum) / (range.Maximum - range.Minimum);
}

public static Interval<T> AsInterval<T>(this (T Minimum, T Maximum) pair, Topology topology = Topology.Default)
where T : struct, IComparable<T>
{
Expand Down
27 changes: 24 additions & 3 deletions src/AnalysisPrograms/Recognizers/Base/RecognizerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,30 @@ private void SummarizeHighResolutionIndices(

public override void WriteEventsFile(FileInfo destination, IEnumerable<EventBase> results)
{
// TODO check this
//Csv.WriteToCsv(destination, results.Select(x => (AcousticEvent)x));
Csv.WriteToCsv(destination, results.Select(x => (EventCommon)x));
// TODO improve API so more flexible file names can be created
Csv.WriteToCsv(
destination,
results
.Where(x => x is AcousticEvent)
.Select(x => (AcousticEvent)x));

// HACK: improve filename creation
var destinationBetaCsv = destination
.FullName
.Replace(".csv", ".beta.csv")
.ToFileInfo();
Csv.WriteToCsv(
destinationBetaCsv,
results
.Where(x => x is EventCommon)
.Select(x => (EventCommon)x));

// HACK: improve filename creation
var destinationJson = destination
.FullName
.Replace(".csv", ".beta.json")
.ToFileInfo();
Json.Serialise(destinationJson, results);
}

public override void WriteSummaryIndicesFile(FileInfo destination, IEnumerable<SummaryIndexBase> results)
Expand Down
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/Recognizers/KoalaV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class KoalaV3 : RecognizerBase

public override string SpeciesName => "PhascolarctosCinereus";

public override string Description => "[ALPHA] Detects acoustic events for the Koala.";
public override string Description => "[WIP] Detects acoustic events for the Koala.";

public override AnalyzerConfig ParseConfig(FileInfo file)
{
Expand Down
5 changes: 0 additions & 5 deletions src/AudioAnalysisTools/AudioAnalysisTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Events\Tracks\**" />
<EmbeddedResource Remove="Events\Tracks\**" />
<None Remove="Events\Tracks\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Acoustics.Shared\Acoustics.Shared.csproj" />
<ProjectReference Include="..\Acoustics.Tools\Acoustics.Tools.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/Events/Drawing/EventDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void DrawScoreIndicator(this SpectralEvent @event, IImageProcessin
return;
}

var normalisedScore = @event.GetScoreNormalised();
var normalisedScore = @event.ScoreNormalized.Clamp(0, 1);

if (normalisedScore == 0)
{
Expand Down
13 changes: 4 additions & 9 deletions src/AudioAnalysisTools/Events/EventCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,22 @@ public abstract class EventCommon : EventBase, IDrawableEvent
/// <summary>
/// Gets or sets the event score.
/// This is a score in absolute units as determined by context.
/// ScoreMax determines the scale.
/// <see cref="ScoreRange"/> determines the scale.
/// </summary>
public override double Score { get; set; }

/// <summary>
/// Gets or sets a min-max range of values for the score for this event.
/// This is used to establish a score scale and thereby normalise the score.
/// This is used to establish a score scale and thereby normalize the score.
/// By default the minimum value of range = zero.
/// </summary>
public Interval<double> ScoreRange { get; set; }

/// <summary>
/// Gets a score in the range 0,1.
/// Gets a score in the range [0, 1].
/// Up to user to determine a suitable range maximum.
/// Minimum of range assumed to be zero.
/// </summary>
public double GetScoreNormalised()
{
var range = this.ScoreRange.Maximum - this.ScoreRange.Minimum;
return (this.Score / range).Clamp(0, 1);
}
public virtual double ScoreNormalized => this.ScoreRange.Normalize(this.Score);

/// <summary>
/// Draw this event on an image.
Expand Down
16 changes: 15 additions & 1 deletion src/AudioAnalysisTools/Events/Types/CompositeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CompositeEvent : SpectralEvent
public CompositeEvent(List<SpectralEvent> events)
{
this.ComponentEvents.AddRange(events);
this.ScoreRange = (0, 1);
}

public List<EventCommon> ComponentEvents { get; set; } = new List<EventCommon>();
Expand Down Expand Up @@ -57,7 +58,20 @@ public IEnumerable<ITrack> Tracks
}
}

public override double ScoreNormalised => this.ComponentEvents.Average()
public override double ScoreNormalized
{
get
{
// because we are averaging normalized scores,
// we can just multiply the values
return this
.ComponentEvents
.Aggregate(
1.0,
(previous, current) => previous * current.ScoreNormalized);
}
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
foreach (var @event in this.ComponentEvents)
Expand Down

0 comments on commit c63aa6c

Please sign in to comment.