Skip to content

Commit

Permalink
Merge pull request #137 from QutBioacoustics/zoom-refactor
Browse files Browse the repository at this point in the history
Zooming index spectrograms improvments
Original hash: 83642c7
  • Loading branch information
atruskie authored Nov 30, 2017
2 parents d3d3c1f + b311fc3 commit 4d29af5
Show file tree
Hide file tree
Showing 332 changed files with 4,175 additions and 4,703 deletions.
11 changes: 8 additions & 3 deletions Acoustics/Acoustics.Shared/Acoustics.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Acoustics.Shared</RootNamespace>
<AssemblyName>Acoustics.Shared</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\EcoSounds\</SolutionDir>
<TargetFrameworkProfile />
Expand Down Expand Up @@ -203,10 +203,11 @@
<Reference Include="System.Core" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\AudioAnalysis\packages\System.ValueTuple.4.4.0-preview2-25405-01\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
<HintPath>..\..\AudioAnalysis\packages\System.ValueTuple.4.4.0-preview2-25405-01\lib\net461\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
Expand All @@ -220,8 +221,12 @@
<Reference Include="YamlDotNet.Dynamic">
<HintPath>..\..\Extra Assemblies\YamlDotNet\YamlDotNet.Dynamic.dll</HintPath>
</Reference>
<Reference Include="Zio, Version=0.3.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\AudioAnalysis\packages\Zio.0.3.6\lib\net45\Zio.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AnalysisIo.cs" />
<Compile Include="AppConfigHelper.cs" />
<Compile Include="Base58.cs" />
<Compile Include="Binary.cs" />
Expand Down Expand Up @@ -257,6 +262,7 @@
<Compile Include="Extensions\StringCaseExtensions.cs" />
<Compile Include="Extensions\TaskExtensions.cs" />
<Compile Include="Extensions\TupleExtensions.cs" />
<Compile Include="Extensions\ZioExtensions.cs" />
<Compile Include="FileDateHelpers.cs" />
<Compile Include="FileNameHelpers.cs" />
<Compile Include="Formatters.cs" />
Expand All @@ -275,7 +281,6 @@
<Compile Include="PropertyComparer.cs" />
<Compile Include="Range.cs" />
<Compile Include="ResultProperty.cs" />
<Compile Include="SqlServerTypes\Loader.cs" />
<Compile Include="StatDescriptive.cs" />
<Compile Include="StringKeyValueStore.cs" />
<Compile Include="TempFileHelper.cs" />
Expand Down
66 changes: 66 additions & 0 deletions Acoustics/Acoustics.Shared/AnalysisIo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <copyright file="AnalysisIo.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>

namespace Acoustics.Shared
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Acoustics.Shared.Contracts;

using Zio;

public class AnalysisIo
{
public AnalysisIo((IFileSystem FileSystem, FileSystemEntry Base) input, (IFileSystem FileSystem, DirectoryEntry Base) output, (IFileSystem FileSystem, DirectoryEntry Base)? temp)
{
this.Input = input.FileSystem;
this.InputBase = input.Base;

this.Output = output.FileSystem;
this.OutputBase = output.Base;

this.Temp = (temp ?? input).FileSystem;
this.TempBase = (DirectoryEntry)(temp ?? input).Base;
}

public FileSystemEntry InputBase { get; }

public DirectoryEntry OutputBase { get; }

public DirectoryEntry TempBase { get; }

public IFileSystem Input { get; }

public IFileSystem Output { get; }

public IFileSystem Temp { get; }

public AnalysisIoInputDirectory EnsureInputIsDirectory()
{
Contract.Requires(this.InputBase != null, $"{nameof(this.InputBase)} must not be null");

if (this.Input.DirectoryExists(this.InputBase.Path))
{
return new AnalysisIoInputDirectory((this.Input, this.InputBase), (this.Output, this.OutputBase), (this.Temp, this.TempBase));
}

throw new ArgumentException($"Expected `{this.InputBase}` to be a directory, but it is not");
}
}

public class AnalysisIoInputDirectory
: AnalysisIo
{
internal AnalysisIoInputDirectory((IFileSystem FileSystem, FileSystemEntry Base) input, (IFileSystem FileSystem, DirectoryEntry Base) output, (IFileSystem FileSystem, DirectoryEntry Base)? temp)
: base(input, output, temp)
{
}

public new DirectoryEntry InputBase => (DirectoryEntry)base.InputBase;
}
}
3 changes: 3 additions & 0 deletions Acoustics/Acoustics.Shared/AppConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public static class AppConfigHelper
/// </summary>
public const string Iso8601FileCompatibleDateFormat = "yyyyMMddTHHmmsszzz";

public const string Iso8601FileCompatibleDateFormatUtcWithFractionalSeconds = "yyyyMMddTHHmmss.FFF\\Z";

public const string StandardDateFormatUtc = "yyyyMMdd-HHmmssZ";

public const string StandardDateFormatUtcWithFractionalSeconds = "yyyyMMdd-HHmmss.FFFZ";

public static string FileDateFormatUtc
Expand Down
6 changes: 5 additions & 1 deletion Acoustics/Acoustics.Shared/Base58.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Acoustics.Shared
// <copyright file="Base58.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>

namespace Acoustics.Shared
{
using System;

Expand Down
41 changes: 22 additions & 19 deletions Acoustics/Acoustics.Shared/ConfigFile/ConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Acoustics.Shared.ConfigFile
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -19,6 +20,8 @@ namespace Acoustics.Shared.ConfigFile
using YamlDotNet.Dynamic;
using YamlDotNet.RepresentationModel;

using Zio;

public static class ConfigFile
{
public const string ProfilesKey = "Profiles";
Expand Down Expand Up @@ -46,10 +49,7 @@ public static string ConfigFolder
$"Cannot find currently set ConfigFiles directory. {defaultConfigFolder} does not exist!");
}

set
{
defaultConfigFolder = value;
}
set => defaultConfigFolder = value;
}

public static dynamic GetProfile(dynamic configuration, string profileName)
Expand Down Expand Up @@ -143,12 +143,11 @@ public static FileInfo ResolveConfigFile(FileInfo file, params DirectoryInfo[] s

public static FileInfo ResolveConfigFile(string file, params DirectoryInfo[] searchPaths)
{
FileInfo configFile;
var success = TryResolveConfigFile(file, searchPaths, out configFile);
var success = TryResolveConfigFile(file, searchPaths.Select(x => x.ToDirectoryEntry()), out FileEntry configFile);

if (success)
{
return configFile;
return configFile.ToFileInfo();
}

var searchedIn =
Expand All @@ -159,44 +158,48 @@ public static FileInfo ResolveConfigFile(string file, params DirectoryInfo[] sea
throw new ConfigFileException(message, file);
}

public static bool TryResolveConfigFile(string file, DirectoryInfo[] searchPaths, out FileInfo configFile)
public static bool TryResolveConfigFile(string file, IEnumerable<DirectoryEntry> searchPaths, out FileEntry configFile)
{
configFile = null;
if (string.IsNullOrWhiteSpace(file))
{
return false;
}

if (File.Exists(file))
// this is a holdover from concrete file systems. The concept of a working directory has no real
// equivalent in a virtual file system but this is implemented for compatibility
// GetFullPath should take care of relative paths relative to current working directory
var fullPath = Path.GetFullPath(file);
if (File.Exists(fullPath))
{
configFile = new FileInfo(file);
configFile = fullPath.ToFileEntry();
return true;
}

// if it does not exist
// and is rooted, it can't exist
if (Path.IsPathRooted(file))
{
// if absolute on concrete file system and can't be found then we can't resolve automatically
return false;
}

if (searchPaths != null && searchPaths.Length > 0)
if (searchPaths != null)
{
foreach (var directoryInfo in searchPaths)
foreach (var directory in searchPaths)
{
var searchPath = Path.GetFullPath(Path.Combine(directoryInfo.FullName, file));
if (File.Exists(searchPath))
var searchPath = directory.CombineFile(file);
if (searchPath.Exists)
{
configFile = new FileInfo(searchPath);
configFile = searchPath;
return true;
}
}
}

// config files are always packaged with the app so use a physical file system
var defaultConfigFile = Path.GetFullPath(Path.Combine(ConfigFolder, file));
if (File.Exists(defaultConfigFile))
{
configFile = new FileInfo(defaultConfigFile);
configFile = defaultConfigFile.ToFileEntry();
return true;
}

Expand All @@ -206,7 +209,7 @@ public static bool TryResolveConfigFile(string file, DirectoryInfo[] searchPaths
var nestedDefaultConfigFile = Path.Combine(directory, file);
if (File.Exists(nestedDefaultConfigFile))
{
configFile = new FileInfo(nestedDefaultConfigFile);
configFile = nestedDefaultConfigFile.ToFileEntry();
return true;
}
}
Expand Down
16 changes: 16 additions & 0 deletions Acoustics/Acoustics.Shared/Contracts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ namespace Acoustics.Shared.Contracts
/// </summary>
public class Contract
{
/// <summary>
/// Require the supplied value to be not null, otherwise throw a argument null exception.
/// </summary>
/// <param name="value">Whether or not the exception should be thrown</param>
/// <param name="name">The name of the argument that was null</param>
/// <param name="message">The message to add to the exception if the check fails</param>
[DebuggerHidden]
[ContractAnnotation("value:null => halt")]
public static void RequiresNotNull(object value, string name = "", string message = "Precondition failed - value was null")
{
if (value == null)
{
throw new ArgumentNullException(name, message);
}
}

/// <summary>
/// Require the supplied boolean to be true, otherwise throw an exception.
/// </summary>
Expand Down
45 changes: 30 additions & 15 deletions Acoustics/Acoustics.Shared/Csv/Csv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Acoustics.Shared.Csv
using Fasterflect;
using log4net;

using Zio;

/// <summary>
/// Generic methods for reading and writing Csv file.
/// .
Expand Down Expand Up @@ -242,6 +244,7 @@ private static List<T[]> DecodeMatrix<T>(this CsvReader reader, bool includeRowI
{
throw new CsvHelperException("Expected an index header and there was none");
}

if (!includeRowIndex && headers[0] == "Index")
{
throw new CsvHelperException("Did not expect an index header and there was one");
Expand Down Expand Up @@ -276,36 +279,44 @@ private static List<T[]> DecodeMatrix<T>(this CsvReader reader, bool includeRowI
int columnCount;
var csvRows = DecodeMatrix<T>(reader, includeRowIndex, out rowCount, out columnCount);

var result = dimensionality == TwoDimensionalArray.RowMajor
var result = dimensionality == TwoDimensionalArray.None
? new T[rowCount, columnCount]
: new T[columnCount, rowCount];

for (int i = 0; i < csvRows.Count; i++)
for (int r = 0; r < csvRows.Count; r++)
{
var row = csvRows[i];
for (int j = 0; j < row.Length; j++)
var row = csvRows[r];
for (int c = 0; c < row.Length; c++)
{
switch (dimensionality)
{
case TwoDimensionalArray.RowMajor:
result[i, j] = row[j];
case TwoDimensionalArray.None:
result[r, c] = row[c];
break;
case TwoDimensionalArray.ColumnMajor:
result[j, i] = row[j];
case TwoDimensionalArray.Transpose:
result[c, r] = row[c];
break;
case TwoDimensionalArray.ColumnMajorFlipped:
result[columnCount - 1 - j, i] = row[j];
case TwoDimensionalArray.Rotate90ClockWise:
// note these operations are reversed and look wrong, but because they
// are being done on the LHS of assignment, the operations need to be inversed
result[c, rowCount - 1 - r] = row[c];
break;
case TwoDimensionalArray.Rotate90AntiClockWise:
// note these operations are reversed and look wrong, but because they
// are being done on the LHS of assignment, the operations need to be inversed
result[columnCount - 1 - c, r] = row[c];

break;
default:
throw new NotImplementedException("Other dimensionalities not implemented");
}
}
}

return result;
}


public static void WriteMatrixToCsv<T>(FileInfo destination, T[,] matrix, TwoDimensionalArray dimensionality = TwoDimensionalArray.RowMajor)
public static void WriteMatrixToCsv<T>(FileInfo destination, T[,] matrix, TwoDimensionalArray dimensionality = TwoDimensionalArray.None)
{
Contract.Requires(destination != null);

Expand All @@ -320,16 +331,20 @@ public static void WriteMatrixToCsv<T>(FileInfo destination, T[,] matrix, TwoDim
}
}

public static T[,] ReadMatrixFromCsv<T>(FileInfo source, TwoDimensionalArray dimensionality = TwoDimensionalArray.RowMajor)
public static T[,] ReadMatrixFromCsv<T>(FileInfo source, TwoDimensionalArray transform = TwoDimensionalArray.None)
{
return ReadMatrixFromCsv<T>(source.ToFileEntry(), transform);
}

public static T[,] ReadMatrixFromCsv<T>(FileEntry source, TwoDimensionalArray transform = TwoDimensionalArray.None)
{
Contract.Requires(source != null);

// not tested!
using (var stream = source.OpenText())
{
var reader = new CsvReader(stream, DefaultConfiguration);

return reader.DecodeMatrix<T>(dimensionality, true);
return reader.DecodeMatrix<T>(transform, true);
}
}

Expand Down
Loading

0 comments on commit 4d29af5

Please sign in to comment.