Skip to content

Commit

Permalink
Adds unit tests for ribbon plots
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Apr 28, 2019
1 parent 8f9e727 commit ff0e123
Show file tree
Hide file tree
Showing 19 changed files with 880 additions and 53 deletions.
16 changes: 14 additions & 2 deletions src/Acoustics.Shared/Extensions/DateTimeAndTimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace System
{
using System.IO;
using Acoustics.Shared;
using Collections.Generic;
using Linq;

Expand Down Expand Up @@ -126,6 +127,19 @@ public static string ToTimeZoneString(this TimeSpan ts)
return (ts < TimeSpan.Zero ? "-" : string.Empty) + ts.ToString(@"mm\:ss");
}

/// <summary>
/// Formats a date in an ISO8601 format that is compact
/// and does not contain colons.
/// </summary>
/// <param name="date">The date to convert to a string.</param>
/// <returns>The resulting string.</returns>
public static string ToIso8601SafeString(this DateTimeOffset date)
{
return date
.ToString(AppConfigHelper.Iso8601FileCompatibleDateFormat)
.Replace(":", string.Empty);
}

/// <summary>
/// Gets the DateTimeOffset formatted as a javascript timestamp.
/// </summary>
Expand Down Expand Up @@ -421,8 +435,6 @@ public static TimeSpan Max(this TimeSpan t1, TimeSpan t2)
return t1 >= t2 ? t1 : t2;
}



public enum RoundingDirection
{
Floor,
Expand Down
13 changes: 12 additions & 1 deletion src/Acoustics.Shared/Extensions/ExtensionsString.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="ExtensionsString.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>
Expand Down Expand Up @@ -404,5 +404,16 @@ public static string NormalizeToCrLf(this string str)
string normalized = Regex.Replace(str, @"\r\n|\n\r|\n|\r", "\r\n");
return normalized;
}

public static string FormatList(this IEnumerable<string> strings)
{
var builder = new StringBuilder("\n", 1000);
foreach (var value in strings)
{
builder.AppendFormat("\t- {0}\n", value);
}

return builder.ToString();
}
}
}
8 changes: 1 addition & 7 deletions src/Acoustics.Shared/Extensions/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,7 @@ public static string BaseName(this FileInfo file)

public static string FormatList(this IEnumerable<FileSystemInfo> infos)
{
var builder = new StringBuilder("\n", 1000);
foreach (var info in infos)
{
builder.AppendFormat("\t- {0}\n", info.FullName);
}

return builder.ToString();
return infos.Select(x => x.FullName).FormatList();
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/Acoustics.Shared/FileNameHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FileNameHelpers.cs" company="QutEcoacoustics">
// <copyright file="FileNameHelpers.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

Expand Down Expand Up @@ -79,7 +79,18 @@ public static string AnalysisResultName(string baseName, string analysisTag, str

if (otherSegments.Length > 0)
{
filename += otherSegments.Aggregate(string.Empty, (aggregate, item) => aggregate + SegmentSeparator + item);
string result = string.Empty;
foreach (var segment in otherSegments)
{
if (segment.IsNullOrEmpty())
{
continue;;
}

result = result + SegmentSeparator + segment;
}

filename += result;
}

if (!string.IsNullOrWhiteSpace(newExtension))
Expand Down
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/AcousticIndices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void SummariseResults(AnalysisSettings settings, FileSegment inputFileSeg

string basename = Path.GetFileNameWithoutExtension(sourceAudio.Name);

// output to disk (so other analysers can use the data,
// output to disk (so other analyzers can use the data,
// only data - configuration settings that generated these indices
// this data can then be used by post-process analyses
/* NOTE: The value for FrameStep is used only when calculating a standard spectrogram
Expand Down
4 changes: 2 additions & 2 deletions src/AnalysisPrograms/AnalysisPrograms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@
<Compile Include="Recognizers\LitoriaOlong.cs" />
<Compile Include="Recognizers\LitoriaFallax.cs" />
<Compile Include="Recognizers\RhinellaMarina.cs" />
<Compile Include="RibbonPlots\RibbonPlot.Arguments.cs" />
<Compile Include="RibbonPlots\RibbonPlot.Entry.cs" />
<Compile Include="Draw\RibbonPlots\RibbonPlot.Arguments.cs" />
<Compile Include="Draw\RibbonPlots\RibbonPlot.Entry.cs" />
<Compile Include="SourcePreparers\RemoteSourcePreparer.cs" />
<Compile Include="SourcePreparers\LocalSourcePreparer.cs" />
<Compile Include="SourcePreparers\RemoteSourcePreparerException.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace AnalysisPrograms.RibbonPlots
namespace AnalysisPrograms.Draw.RibbonPlots
{
using System;
using System.IO;
Expand All @@ -27,8 +27,8 @@ public class Arguments : SubCommandBase
{
[Argument(
0,
Description = "One or more directories where that contain ribbon FCS files.")]
public DirectoryInfo[] SourceDirectories { get; set; }
Description = "One or more directories where that contain ribbon FCS files (index results).")]
public DirectoryInfo[] InputDirectories { get; set; }

[Option(
CommandOptionType.SingleValue,
Expand All @@ -52,7 +52,7 @@ public class Arguments : SubCommandBase

public override Task<int> Execute(CommandLineApplication app)
{
return RibbonPlot.Execute(this);
return Draw.RibbonPlots.RibbonPlot.Execute(this);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>

namespace AnalysisPrograms.RibbonPlots
namespace AnalysisPrograms.Draw.RibbonPlots
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Acoustics.Shared;
using AnalysisPrograms.Production;
using AudioAnalysisTools.Indices;
using AudioAnalysisTools.LongDurationSpectrograms;
using log4net;
using log4net.Util;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.Primitives;
using SixLabors.Shapes;

Expand All @@ -33,24 +29,24 @@ public partial class RibbonPlot
private static readonly TimeSpan RibbonPlotDomain = TimeSpan.FromHours(24);
private static readonly ILog Log = LogManager.GetLogger(nameof(RibbonPlot));

public static async Task<int> Execute(Arguments arguments)
public static async Task<int> Execute(RibbonPlot.Arguments arguments)
{
if (arguments.SourceDirectories.IsNullOrEmpty())
if (arguments.InputDirectories.IsNullOrEmpty())
{
throw new CommandLineArgumentException(
$"{nameof(arguments.SourceDirectories)} is null or empty - please provide at least one source directory");
$"{nameof(arguments.InputDirectories)} is null or empty - please provide at least one source directory");
}

var doNotExist = arguments.SourceDirectories.Where(x => !x.Exists);
var doNotExist = arguments.InputDirectories.Where(x => !x.Exists);
if (doNotExist.Any())
{
throw new CommandLineArgumentException(
$"The following directories given to {nameof(arguments.SourceDirectories)} do not exist: " + doNotExist.FormatList());
$"The following directories given to {nameof(arguments.InputDirectories)} do not exist: " + doNotExist.FormatList());
}

if (arguments.OutputDirectory == null)
{
arguments.OutputDirectory = arguments.SourceDirectories.First();
arguments.OutputDirectory = arguments.InputDirectories.First();
Log.Warn(
$"{nameof(arguments.OutputDirectory)} was not provided and was automatically set to source directory {arguments.OutputDirectory}");
}
Expand All @@ -69,11 +65,11 @@ public static async Task<int> Execute(Arguments arguments)

LoggedConsole.Write("Begin scanning directories");

var allIndexFiles = arguments.SourceDirectories.SelectMany(IndexGenerationData.FindAll);
var allIndexFiles = arguments.InputDirectories.SelectMany(IndexGenerationData.FindAll);

if (allIndexFiles.IsNullOrEmpty())
{
throw new MissingDataException($"Could not find `{IndexGenerationData.FileNameFragment}` files in:" + arguments.SourceDirectories.FormatList());
throw new MissingDataException($"Could not find `{IndexGenerationData.FileNameFragment}` files in:" + arguments.InputDirectories.FormatList());
}

Log.Debug("Checking files have dates");
Expand Down Expand Up @@ -142,7 +138,14 @@ void Add(string colorMap)
var midnight = arguments.Midnight == RibbonPlotDomain
? string.Empty
: "Midnight=" + arguments.Midnight.Value.ToString("hhmm");
var path = FilenameHelpers.AnalysisResultPath(arguments.OutputDirectory, arguments.OutputDirectory.Name, "RibbonPlot", "png", colorMap, midnight);
var path = FilenameHelpers.AnalysisResultPath(
arguments.OutputDirectory,
arguments.OutputDirectory.Name,
"RibbonPlot",
"png",
colorMap,
midnight);

using (var file = File.Create(path))
{
image.SaveAsPng(file);
Expand Down
3 changes: 2 additions & 1 deletion src/AnalysisPrograms/Production/Arguments/MainArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AnalysisPrograms.Production.Arguments
using System.Threading.Tasks;
using Acoustics.Shared;
using AnalyseLongRecordings;
using AnalysisPrograms.Draw.RibbonPlots;
using Draw.Zooming;
using EventStatistics;
using McMaster.Extensions.CommandLineUtils;
Expand Down Expand Up @@ -36,7 +37,7 @@ namespace AnalysisPrograms.Production.Arguments
[Subcommand(ConcatenateIndexFiles.CommandName, typeof(ConcatenateIndexFiles.Arguments))]
[Subcommand(DrawLongDurationSpectrograms.CommandName, typeof(DrawLongDurationSpectrograms.Arguments))]
[Subcommand(DrawZoomingSpectrograms.CommandName, typeof(DrawZoomingSpectrograms.Arguments))]
[Subcommand(RibbonPlots.RibbonPlot.CommandName, typeof(RibbonPlots.RibbonPlot.Arguments))]
[Subcommand(RibbonPlot.CommandName, typeof(RibbonPlot.Arguments))]
[Subcommand(DrawEasyImage.CommandName, typeof(DrawEasyImage.Arguments))]
[Subcommand(Audio2InputForConvCnn.CommandName, typeof(Audio2InputForConvCnn.Arguments))]
[Subcommand(DifferenceSpectrogram.CommandName, typeof(DifferenceSpectrogram.Arguments))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace AudioAnalysisTools.LongDurationSpectrograms
public class LDSpectrogramRGB
{
public const string SpectralRibbonTag = ".SpectralRibbon";
public const int RibbonPlotHeight = 32;

// Below is some history about how indices were assigned to the RGB channels to make long-duration false-colour spectrograms
// string[] keys = { "ACI", "TEN", "CVR", "BGN", "AVG", "VAR" }; // the OLDEST default i.e. used in 2014
Expand Down Expand Up @@ -1583,9 +1584,9 @@ public static Tuple<Image, string>[] DrawSpectrogramsFromSpectralIndices(
//ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap2 + ".SummaryRibbon", "png"));

// Spectrogram ribbons are very useful for viewing multiple days of recording.
var ribbon = cs1.GetSpectrogramRibbon(colorMap1, 32);
var ribbon = cs1.GetSpectrogramRibbon(colorMap1, RibbonPlotHeight);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap1 + SpectralRibbonTag, "png"));
ribbon = cs1.GetSpectrogramRibbon(colorMap2, 32);
ribbon = cs1.GetSpectrogramRibbon(colorMap2, RibbonPlotHeight);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap2 + SpectralRibbonTag, "png"));

// only return images if chromeless
Expand Down
36 changes: 35 additions & 1 deletion tests/Acoustics.Test/Acoustics.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,30 @@
<Reference Include="Moq, Version=4.2.1502.911, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\..\packages\Moq.4.2.1502.0911\lib\net40\Moq.dll</HintPath>
</Reference>
<Reference Include="MoreLinq, Version=3.1.0.0, Culture=neutral, PublicKeyToken=384d532d7e88985d, processorArchitecture=MSIL">
<HintPath>..\..\packages\morelinq.3.1.0\lib\net451\MoreLinq.dll</HintPath>
</Reference>
<Reference Include="MSTestExtensions, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\MSTestExtensions.4.0.0\lib\MSTestExtensions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SixLabors.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SixLabors.Core.1.0.0-beta0007\lib\netstandard2.0\SixLabors.Core.dll</HintPath>
</Reference>
<Reference Include="SixLabors.Fonts, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SixLabors.Fonts.1.0.0-beta0008\lib\netstandard2.0\SixLabors.Fonts.dll</HintPath>
</Reference>
<Reference Include="SixLabors.ImageSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SixLabors.ImageSharp.1.0.0-beta0006\lib\netstandard2.0\SixLabors.ImageSharp.dll</HintPath>
</Reference>
<Reference Include="SixLabors.ImageSharp.Drawing, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SixLabors.ImageSharp.Drawing.1.0.0-beta0006\lib\netstandard2.0\SixLabors.ImageSharp.Drawing.dll</HintPath>
</Reference>
<Reference Include="SixLabors.Shapes, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\SixLabors.Shapes.1.0.0-beta0008\lib\netstandard2.0\SixLabors.Shapes.dll</HintPath>
</Reference>
<Reference Include="SmartFormat, Version=2.1.0.2, Culture=neutral, PublicKeyToken=568866805651201f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SmartFormat.Net.2.1.0.2\lib\net45\SmartFormat.dll</HintPath>
</Reference>
Expand All @@ -129,6 +147,9 @@
<HintPath>..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
Expand All @@ -140,6 +161,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -173,6 +195,9 @@
<HintPath>..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http">
<HintPath>..\..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll</HintPath>
Expand All @@ -190,6 +215,9 @@
<Private>True</Private>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Reflection">
<HintPath>..\..\packages\System.Reflection.4.3.0\lib\net462\System.Reflection.dll</HintPath>
<Private>True</Private>
Expand All @@ -201,6 +229,9 @@
<HintPath>..\..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Extensions">
<HintPath>..\..\packages\System.Runtime.Extensions.4.3.0\lib\net462\System.Runtime.Extensions.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -272,6 +303,7 @@
<Compile Include="AnalysisPrograms\AcousticWorkbench.Orchestration\EventMetadataResolverTests.cs" />
<Compile Include="AnalysisPrograms\AnalyzeLongRecordings\TestAnalyzeLongRecording.cs" />
<Compile Include="AnalysisPrograms\Concatenation\ConcatenationTests.cs" />
<Compile Include="AnalysisPrograms\Draw\RibbonPlots\RibbonPlotTests.cs" />
<Compile Include="AnalysisPrograms\Draw\Zooming\DrawZoomingTests.cs" />
<Compile Include="AnalysisPrograms\MainEntryTests.cs" />
<Compile Include="AnalysisPrograms\Production\FileSystemProviderTests.cs" />
Expand Down Expand Up @@ -302,12 +334,14 @@
<Compile Include="RuntimesTests.cs" />
<Compile Include="Shared\AppConfigHelperTests.cs" />
<Compile Include="Shared\ConfigTests.cs" />
<Compile Include="Shared\Extensions\DateTimeAndTimeSpanExtensions.cs" />
<Compile Include="Shared\Extensions\DateTimeAndTimeSpanExtensionsTests.cs" />
<Compile Include="Shared\LoggingTests\LoggingTests.cs" />
<Compile Include="Shared\PathUtilsTests.cs" />
<Compile Include="Shared\ProcessRunnerTests.cs" />
<Compile Include="Shared\RangeTests.cs" />
<Compile Include="TestHelpers\Factories\AudioRecordingFactory.cs" />
<Compile Include="TestHelpers\ImageHelpers.cs" />
<Compile Include="TestHelpers\TestImage.cs" />
<Compile Include="TestHelpers\TestSetup.cs" />
<Compile Include="Tools\AudioFilePreparerTests.cs" />
<Compile Include="Shared\FileDateHelpersTests.cs" />
Expand Down
Loading

0 comments on commit ff0e123

Please sign in to comment.