Skip to content

Commit

Permalink
[WIP] Initial work for drawing ribbon plots
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Apr 28, 2019
1 parent 3c64b3c commit 51f27bd
Show file tree
Hide file tree
Showing 16 changed files with 466 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none
# Expression-bodied members
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#expression_bodied_members
csharp_style_expression_bodied_methods = when_on_single_line:warning
csharp_style_expression_bodied_constructors = when_on_single_line:warning
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
csharp_style_expression_bodied_operators = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_indexers = true:warning
Expand Down
1 change: 1 addition & 0 deletions src/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static class AppConfigHelper
public const string StandardDateFormatNoTimeZone = "yyyyMMdd-HHmmss";
public const string StandardDateFormatUnderscore = "yyyyMMdd_HHmmsszzz";
public const string StandardDateFormatSm2 = "yyyyMMdd_HHmmss";
public const string RenderedDateFormatShort = "yyyy-MM-dd HH:mm";

private static readonly string ExecutingAssemblyPath =
(Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location;
Expand Down
18 changes: 13 additions & 5 deletions src/Acoustics.Shared/Extensions/DateTimeAndTimeSpanExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DateTimeAndTimeSpanExtensions.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 @@ -282,37 +282,45 @@ public static DateTime Round(this DateTime datetime, TimeSpan roundingInterval)
}

/// <summary>
/// Multiplies a timespan by a scalar value
/// Multiplies a timespan by a scalar value.
/// </summary>
public static TimeSpan Multiply(this TimeSpan multiplicand, int multiplier)
{
return TimeSpan.FromTicks(multiplicand.Ticks * multiplier);
}

/// <summary>
/// Divides a timespan by an scalar value
/// Divides a timespan by an scalar value.
/// </summary>
public static TimeSpan Divide(this TimeSpan dividend, int divisor)
{
return TimeSpan.FromTicks(dividend.Ticks / divisor);
}

/// <summary>
/// Multiplies a timespan by a double value
/// Multiplies a timespan by a double value.
/// </summary>
public static TimeSpan Multiply(this TimeSpan multiplicand, double multiplier)
{
return TimeSpan.FromTicks((long)(multiplicand.Ticks * multiplier));
}

/// <summary>
/// Divides a timespan by an scalar value
/// Divides a timespan by an scalar value.
/// </summary>
public static TimeSpan Divide(this TimeSpan dividend, double divisor)
{
return TimeSpan.FromTicks((long)(dividend.Ticks / divisor));
}

/// <summary>
/// Divides a timespan by an scalar value.
/// </summary>
public static double Divide(this TimeSpan dividend, TimeSpan divisor)
{
return dividend.Ticks / (double)divisor.Ticks;
}

// https://github.com/exceptionless/Exceptionless.DateTimeExtensions/blob/master/src/Exceptionless.DateTimeExtensions/DateTimeOffsetExtensions.cs#L222
public static DateTimeOffset Floor(this DateTimeOffset date, TimeSpan interval)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Acoustics.Shared/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="DictionaryExtensions.cs" company="QutEcoacoustics">
// <copyright file="DictionaryExtensions.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>
// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -36,5 +36,14 @@ public static TValue FirstValue<TKey, TValue>(this Dictionary<TKey, TValue> dict

return dictionary.Values.First();
}

public static void Deconstruct<TKey, TValue>(
this KeyValuePair<TKey, TValue> kvp,
out TKey key,
out TValue value)
{
key = kvp.Key;
value = kvp.Value;
}
}
}
4 changes: 2 additions & 2 deletions src/Acoustics.Shared/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EnumerableExtensions.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 All @@ -16,7 +16,7 @@ namespace System

public static class EnumerableExtensions
{
[ContractAnnotation("items:null => true; items:notnull => false")]
[ContractAnnotation("items:null => true")]
public static bool IsNullOrEmpty<T>(this IEnumerable<T> items)
{
return items == null || !items.Any();
Expand Down
11 changes: 11 additions & 0 deletions src/Acoustics.Shared/Extensions/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public static string BaseName(this FileInfo file)
{
return Path.GetFileNameWithoutExtension(file.Name);
}

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();
}
}

public class FileInfoNameComparer : IComparer<FileInfo>, IEqualityComparer<FileInfo>
Expand Down
48 changes: 48 additions & 0 deletions src/Acoustics.Shared/FileDateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,54 @@ public static SortedDictionary<DateTimeOffset, FileInfo> FilterFilesForDates(IEn
return datesAndFiles;
}

/// <summary>
/// sorts a list of files by the date assumed to be encoded in their file names
/// and then returns the list as a sorted dictionary with file DateTime as the keys.
/// </summary>
/// <param name="files">The files to filter.</param>
/// <param name="offsetHint">If you know what timezone you should have, specify a hint to enable parsing of ambiguous dates.</param>
/// <returns>A sorted dictionary FileInfo objects mapped to parsed dates.</returns>
public static SortedDictionary<DateTimeOffset, T> FilterObjectsForDates<T>(IEnumerable<T> objects, Func<T, FileSystemInfo> pathSelector, Func<T, DateTimeOffset?> overrideSelector, TimeSpan? offsetHint = null)
{
var datesAndFiles = new SortedDictionary<DateTimeOffset, T>();
foreach (var @object in objects)
{
var date = overrideSelector?.Invoke(@object);

FileSystemInfo file = null;
if (date == null)
{
file = pathSelector.Invoke(@object);
if (FileNameContainsDateTime(file.Name, out var parsedDate, offsetHint))
{
date = parsedDate;
}
}

if (date.NotNull())
{
if (datesAndFiles.ContainsKey(date.Value))
{
if (file == null)
{
file = pathSelector.Invoke(@object);
}

string message =
$"There was a duplicate date. File {file} with date {date,'r'} conflicts with existing file {datesAndFiles[date.Value]}";
throw new InvalidDataSetException(message);
}

datesAndFiles.Add(date.Value, @object);
}
}

// use following lines to get first and last date from returned dictionary
//DateTimeOffset firstdate = datesAndFiles[datesAndFiles.Keys.First()];
//DateTimeOffset lastdate = datesAndFiles[datesAndFiles.Keys.Last()];
return datesAndFiles;
}

/// <summary>
/// sorts a list of files by the date assumed to be encoded in their file names
/// and then returns the list as a sorted dictionary with file DateTime as the keys.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace AnalysisPrograms.AnalyseLongRecordings

public partial class AnalyseLongRecording
{
private const string ImagefileExt = "png";
private const string ImageFileExt = "png";

private static readonly ILog Log = LogManager.GetLogger(nameof(AnalyseLongRecording));

Expand Down Expand Up @@ -101,7 +101,7 @@ public static void Execute(Arguments arguments)
}

// 2. initialize the analyzer
// we're changing the way resolving config files works. Ideally, we'd like to use staticly typed config files
// we're changing the way resolving config files works. Ideally, we'd like to use statically typed config files
// but we can't do that unless we know which type we have to load first! Currently analyzer to load is in
// the config file so we can't know which analyzer we can use. Thus we will change to using the file name,
// or an argument to resolve the analyzer to load.
Expand Down Expand Up @@ -328,7 +328,7 @@ public static void Execute(Arguments arguments)
imageTitle,
timeScale,
fileSegment.TargetFileStartDate);
var imagePath = FilenameHelpers.AnalysisResultPath(instanceOutputDirectory, basename, "SummaryIndices", ImagefileExt);
var imagePath = FilenameHelpers.AnalysisResultPath(instanceOutputDirectory, basename, "SummaryIndices", ImageFileExt);
tracksImage.Save(imagePath);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/AnalysisPrograms/AnalysisPrograms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.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="SQLitePCLRaw.batteries_green, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a84b7dcfb1391f7f, processorArchitecture=MSIL">
<HintPath>..\..\packages\SQLitePCLRaw.bundle_green.1.1.9\lib\net45\SQLitePCLRaw.batteries_green.dll</HintPath>
</Reference>
Expand All @@ -136,6 +151,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.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.ComponentModel.Annotations.4.5.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -184,6 +202,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 @@ -201,6 +222,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 @@ -209,6 +233,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 @@ -348,6 +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="SourcePreparers\RemoteSourcePreparer.cs" />
<Compile Include="SourcePreparers\LocalSourcePreparer.cs" />
<Compile Include="SourcePreparers\RemoteSourcePreparerException.cs" />
Expand Down
4 changes: 4 additions & 0 deletions src/AnalysisPrograms/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
10 changes: 5 additions & 5 deletions src/AnalysisPrograms/ConcatenateIndexFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static void Execute(Arguments arguments)
Log.Warn(@"
!
! THIS IS A BETA COMMAND.
! It generally works but only for very narrow scenarios. Your milage *will* vary.
! It generally works but only for very narrow scenarios. Your mileage *will* vary.
!");

if (arguments.InputDataDirectory != null)
Expand Down Expand Up @@ -205,10 +205,10 @@ public static void Execute(Arguments arguments)
var subDirectories = LdSpectrogramStitching.GetSubDirectoriesForSiteData(inputDirs, arguments.DirectoryFilter);
if (subDirectories.Length == 0)
{
LoggedConsole.WriteErrorLine("\n\n#WARNING from method ConcatenateIndexFiles.Execute():");
LoggedConsole.WriteErrorLine("\n\n#Error from method ConcatenateIndexFiles.Execute():");
LoggedConsole.WriteErrorLine(" Subdirectory Count with given filter = ZERO");
LoggedConsole.WriteErrorLine(" RETURNING EMPTY HANDED!");
return;
throw new MissingDataException("Could not find any sub directories from input directories:" + inputDirs.FormatList());
}

// 2. PATTERN SEARCH FOR SUMMARY INDEX FILES.
Expand All @@ -219,10 +219,10 @@ public static void Execute(Arguments arguments)

if (csvFiles.Length == 0)
{
LoggedConsole.WriteErrorLine("\n\nWARNING from method ConcatenateIndexFiles.Execute():");
LoggedConsole.WriteErrorLine("\n\nError from method ConcatenateIndexFiles.Execute():");
LoggedConsole.WriteErrorLine(" No SUMMARY index files were found.");
LoggedConsole.WriteErrorLine(" RETURNING EMPTY HANDED!");
return;
throw new MissingDataException($"Could not find any files matching `{pattern}` in:" + subDirectories.FormatList());
}

// Sort the files by date and return as a dictionary: sortedDictionaryOfDatesAndFiles<DateTimeOffset, FileInfo>
Expand Down
20 changes: 17 additions & 3 deletions src/AnalysisPrograms/Production/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ namespace AnalysisPrograms.Production
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;

using System.Linq;
using Acoustics.Shared;
using Acoustics.Shared.ConfigFile;

using AnalysisBase;
using AnalysisPrograms.Production.Arguments;
using McMaster.Extensions.CommandLineUtils;

public static class ExceptionLookup
Expand Down Expand Up @@ -90,7 +91,11 @@ private static Dictionary<Type, ExceptionStyle> CreateExceptionMap()
},
{
typeof(InvalidDataSetException),
new ExceptionStyle() {ErrorCode = 106, PrintUsage = false }
new ExceptionStyle() { ErrorCode = 106, PrintUsage = false }
},
{
typeof(MissingDataException),
new ExceptionStyle() { ErrorCode = 107, PrintUsage = false }
},
{
typeof(AnalysisOptionDevilException),
Expand Down Expand Up @@ -152,7 +157,8 @@ public int ErrorCode

public class CommandLineArgumentException : Exception
{
public CommandLineArgumentException(string message) : base(message)
public CommandLineArgumentException(string message)
: base(message)
{
}
}
Expand Down Expand Up @@ -185,6 +191,14 @@ public InvalidAudioChannelException(string message)
}
}

public class MissingDataException : Exception
{
public MissingDataException(string message)
: base(message)
{
}
}

public class AnalysisOptionDevilException : Exception
{
}
Expand Down
Loading

0 comments on commit 51f27bd

Please sign in to comment.