Skip to content

Commit

Permalink
More work on zomming spectrograms
Browse files Browse the repository at this point in the history
See #120
Tests pass. Writing images to Sqlite files seems to works.

Now just running through bugs associated with tiles and super tiles.

This patch:
- Modifies the default zoomg spectrograms code for production settings
- References SqliteFileSystem as a package so that dependencies are imported automatically
- Fixes a few  minor bugs
  • Loading branch information
atruskie committed Nov 7, 2017
1 parent 1c43162 commit e907eb6
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace Acoustics.Test.AnalysisPrograms.Draw.Zooming
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelpers;

using Zio;
using Zio.FileSystems.Community.SqliteFileSystem;

[TestClass]
public class DrawZoomingTests : OutputDirectoryTest
{
Expand All @@ -41,9 +44,9 @@ public static void ClassInitialize(TestContext context)
};

context.WriteLine($"{DateTime.Now} generating indices fixture data");
MainEntry.SetLogVerbosity(LogVerbosity.Warn, true);
MainEntry.SetLogVerbosity(LogVerbosity.Warn, false);
AnalyseLongRecording.Execute(arguments);
MainEntry.SetLogVerbosity(LogVerbosity.Debug, true);
MainEntry.SetLogVerbosity(LogVerbosity.Debug, false);
context.WriteLine($"{DateTime.Now} finished generting fixture");

ResultsDirectory = SharedDirectory.Combine("Towsey.Acoustic");
Expand Down Expand Up @@ -77,31 +80,47 @@ public void TestGenerateTiles()
ZoomAction = DrawZoomingSpectrograms.Arguments.ZoomActionType.Tile,
});

Assert.Fail();
var filesProduced = zoomOutput.EnumerateFiles().ToArray();

// there are 16 zoom levels, but the test recording is only 2min long
// at scale 240, we're rendering <1px of content and that tile is not generated
Assert.AreEqual(15, filesProduced.Length);

// not sure what else to test - generally exceptions should be thrown if anything goes wrong
}

/// <summary>
/// Tests the rendering of zooming spectrograms for a minute of audio indices
/// </summary>
[TestMethod]
[Timeout(45_000)]
//[Timeout(45_000)]
public void TestGenerateTilesSqlite()
{
// generate the zooming spectrograms
var zoomOutput = this.outputDirectory.CombineFile("Zooming/tiles.sqlite3");
var zoomOutput = this.outputDirectory.Combine("Zooming");
DrawZoomingSpectrograms.Execute(
new DrawZoomingSpectrograms.Arguments()
{
Output = zoomOutput.FullName,
OutputFormat = "sqlite3",
SourceDirectory = ResultsDirectory.FullName,
SpectrogramZoomingConfig = PathHelper.ResolveConfigFile("SpectrogramZoomingConfig.yml"),
ZoomAction = DrawZoomingSpectrograms.Arguments.ZoomActionType.Tile,
});

Assert.IsTrue(File.Exists(zoomOutput.FullName));
Assert.Fail();
}
var tiles = zoomOutput.CombineFile("OxleyCreek_site_1_1060_244333_20140529T081358+1000_120_0__Tiles.sqlite3");
Assert.IsTrue(tiles.Exists);

using (var fs = new SqliteFileSystem(tiles.FullName, OpenMode.ReadOnly))
{
var files = fs.EnumerateFiles(UPath.Root).ToArray();


// there are 16 zoom levels, but the test recording is only 2min long
// at scale 240, we're rendering <1px of content and that tile is not generated
Assert.AreEqual(15, files.Length);
}

// not sure what else to test - generally exceptions should be thrown if anything goes wrong
}
}
}
33 changes: 16 additions & 17 deletions AudioAnalysis/AnalysisConfigFiles/SpectrogramZoomingConfig.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
---
SpectralFrameDuration: 0.02
TileWidth: 300
TileWidth: 180
# How to tile, valid values:
# - PanoJsTilingProfile
# - AbsoluteDateTilingProfile
TilingProfile: AbsoluteDateTilingProfile
SpectralIndexScale:
- 240
- 120
- 60
- 24
- 12
- 6
- 4
- 2
- 1
- 0.6
- 0.4
- 0.2
SpectralFrameScale:
- 0.07
- 0.04
- 0.02
- 30
- 15
- 7.5
- 3.750
- 1.875
- 1.0
- 0.5
- 0.250
- 0.125
SpectralFrameScale: []
MaxTilesPerSuperTile: 12
UseDistributionsForNormalization: false
LdSpectrogramConfig:
ColorMap1: "ACI-ENT-EVN"
ColorMap2: "BGN-POW-EVN"
# minutes
ColorMap2: "BGN-PMN-EVN"
# seconds
XAxisTicIntervalSeconds: 3600
# Hertz
YAxisTicInterval: 1000
IndexPropertiesConfig: './IndexPropertiesConfig.yml'
IndexPropertiesConfig: './IndexPropertiesConfig.HiRes.yml'
...
1 change: 1 addition & 0 deletions AudioAnalysis/AnalysisPrograms/AcousticIndices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ public void SummariseResults(AnalysisSettings settings, FileSegment inputFileSeg
RecordingType = inputFileSegment.Source.Extension,
RecordingBasename = basename,
RecordingStartDate = inputFileSegment.TargetFileStartDate,
RecordingDuration = inputFileSegment.TargetFileDuration.Value,
SampleRateOriginal = inputFileSegment.TargetFileSampleRate.Value,
SampleRateResampled = sampleRate,
FrameLength = frameWidth,
Expand Down
33 changes: 27 additions & 6 deletions AudioAnalysis/AnalysisPrograms/AnalysisPrograms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@
<HintPath>..\packages\MathNet.Numerics.2.6.2\lib\net40\MathNet.Numerics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Data.Sqlite, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Sqlite.Core.2.0.0\lib\netstandard2.0\Microsoft.Data.Sqlite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
Expand All @@ -187,6 +190,21 @@
<Reference Include="PowerArgs">
<HintPath>..\..\Extra Assemblies\PowerArgs\PowerArgs.dll</HintPath>
</Reference>
<Reference Include="SqliteFileSystem, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SqliteFileSystem.1.0.0\lib\netstandard2.0\SqliteFileSystem.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.7\lib\net45\SQLitePCLRaw.batteries_green.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.batteries_v2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8226ea5df37bcae9, processorArchitecture=MSIL">
<HintPath>..\packages\SQLitePCLRaw.bundle_green.1.1.7\lib\net45\SQLitePCLRaw.batteries_v2.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1488e028ca7ab535, processorArchitecture=MSIL">
<HintPath>..\packages\SQLitePCLRaw.core.1.1.7\lib\net45\SQLitePCLRaw.core.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.provider.e_sqlite3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9c301db686d0bd12, processorArchitecture=MSIL">
<HintPath>..\packages\SQLitePCLRaw.provider.e_sqlite3.net45.1.1.7\lib\net45\SQLitePCLRaw.provider.e_sqlite3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -221,8 +239,9 @@
<HintPath>..\packages\Microsoft.Tpl.Dataflow.4.5.24\lib\portable-net45+win8+wpa81\System.Threading.Tasks.Dataflow.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.4.0-preview2-25405-01\lib\portable-net40+sl4+win8+wp8\System.ValueTuple.dll</HintPath>
<Reference Include="System.ValueTuple">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
Expand Down Expand Up @@ -392,10 +411,6 @@
<Project>{15741ead-ecd6-42f8-ab13-ea4ae636936e}</Project>
<Name>QutBioacosutics.Xie</Name>
</ProjectReference>
<ProjectReference Include="..\SqLiteFileSystem\SqliteFileSystem.csproj">
<Project>{364b812e-4e3d-415e-be10-2928028ee7b6}</Project>
<Name>SqliteFileSystem</Name>
</ProjectReference>
<ProjectReference Include="..\TowseyLibrary\TowseyLibrary.csproj">
<Project>{7c6831fd-f60c-4f7b-8e6a-35e850174411}</Project>
<Name>TowseyLibrary</Name>
Expand Down Expand Up @@ -517,6 +532,12 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets'))" />
<Error Condition="!Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.linux.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.linux.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SQLitePCLRaw.lib.e_sqlite3.linux.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.linux.targets'))" />
<Error Condition="!Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.osx.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.osx.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SQLitePCLRaw.lib.e_sqlite3.osx.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.osx.targets'))" />
<Error Condition="!Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.v110_xp.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.v110_xp.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SQLitePCLRaw.lib.e_sqlite3.v110_xp.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.v110_xp.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
<Import Project="..\packages\SQLitePCLRaw.lib.e_sqlite3.linux.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.linux.targets" Condition="Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.linux.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.linux.targets')" />
<Import Project="..\packages\SQLitePCLRaw.lib.e_sqlite3.osx.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.osx.targets" Condition="Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.osx.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.osx.targets')" />
<Import Project="..\packages\SQLitePCLRaw.lib.e_sqlite3.v110_xp.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.v110_xp.targets" Condition="Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.v110_xp.1.1.7\build\net35\SQLitePCLRaw.lib.e_sqlite3.v110_xp.targets')" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void Execute(Arguments arguments)
// create file systems for reading input and writing output
var io = FileSystemProvider.GetInputOutputFileSystems(
arguments.SourceDirectory,
FileSystemProvider.MakePath(arguments.Output, common.OriginalBasename, arguments.OutputFormat))
FileSystemProvider.MakePath(arguments.Output, common.OriginalBasename, arguments.OutputFormat, "Tiles"))
.EnsureInputIsDirectory();

switch (arguments.ZoomAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static (IFileSystem, DirectoryEntry) DetermineFileSystem(string path, boo
}
else
{
// ensure parent directory exists on disk
Directory.CreateDirectory(Path.GetDirectoryName(path));

switch (extension)
{
case "." + SqlitePattern:
Expand Down Expand Up @@ -104,7 +107,7 @@ public static AnalysisIo GetInputOutputFileSystems(string inputPath, string outp
return new AnalysisIo(input, output, null);
}

public static string MakePath(string directory, string baseName, string format)
public static string MakePath(string directory, string baseName, string format, string tag)
{
if (string.IsNullOrEmpty(format))
{
Expand All @@ -113,7 +116,7 @@ public static string MakePath(string directory, string baseName, string format)

Contract.Requires(AllFormats.Contains(format));

return Path.Combine(directory, baseName + "." + format);
return Path.Combine(directory, FilenameHelpers.AnalysisResultName(baseName, tag, format));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private static void ModifyVerbosity(MainEntryArguments arguments)
SetLogVerbosity(arguments.LogLevel, arguments.QuietConsole);
}

public static void SetLogVerbosity(LogVerbosity logVerbosity, bool quietConsole)
public static void SetLogVerbosity(LogVerbosity logVerbosity, bool quietConsole = false)
{
Level modifiedLevel;
switch (logVerbosity)
Expand Down
11 changes: 10 additions & 1 deletion AudioAnalysis/AnalysisPrograms/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net40" requireReinstallation="true" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net40" />
<package id="Microsoft.Data.Sqlite" version="2.0.0" targetFramework="net462" />
<package id="Microsoft.Data.Sqlite.Core" version="2.0.0" targetFramework="net462" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net40" requireReinstallation="true" />
<package id="Microsoft.Tpl.Dataflow" version="4.5.24" targetFramework="net452" />
<package id="MSBuildTasks" version="1.5.0.235" targetFramework="net40" developmentDependency="true" />
<package id="SqliteFileSystem" version="1.0.0" targetFramework="net462" />
<package id="SQLitePCLRaw.bundle_green" version="1.1.7" targetFramework="net462" />
<package id="SQLitePCLRaw.core" version="1.1.7" targetFramework="net462" />
<package id="SQLitePCLRaw.lib.e_sqlite3.linux" version="1.1.7" targetFramework="net462" />
<package id="SQLitePCLRaw.lib.e_sqlite3.osx" version="1.1.7" targetFramework="net462" />
<package id="SQLitePCLRaw.lib.e_sqlite3.v110_xp" version="1.1.7" targetFramework="net462" />
<package id="SQLitePCLRaw.provider.e_sqlite3.net45" version="1.1.7" targetFramework="net462" />
<package id="StyleCop.Analyzers" version="1.1.0-beta004" targetFramework="net40" developmentDependency="true" />
<package id="System.ValueTuple" version="4.4.0-preview2-25405-01" targetFramework="net40" requireReinstallation="true" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net462" />
<package id="YamlDotNet" version="3.9.0" targetFramework="net40" />
<package id="Zio" version="0.3.1" targetFramework="net462" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public IndexGenerationData()

public string RecordingBasename { get; set; }

public TimeSpan RecordingDuration { get; set; }

/// <summary>
/// Returns the index generation data from file in passed directory.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void DrawTiles(
var zoomConfig = common.SpectrogramZoomingConfig;
LdSpectrogramConfig ldsConfig = common.SpectrogramZoomingConfig.LdSpectrogramConfig;
var distributions = common.IndexDistributions;
var indexGeneration = common.IndexGenerationData;
var indexGenerationData = common.IndexGenerationData;
var indexProperties = common.IndexProperties;

string fileStem = common.OriginalBasename;
Expand All @@ -60,7 +60,7 @@ public static void DrawTiles(
" we need at least some scales to render zooming spectrograms");
}

var shouldRenderStandardScale = standardScales.IsNullOrEmpty();
var shouldRenderStandardScale = !standardScales.IsNullOrEmpty();
if (shouldRenderStandardScale)
{
Log.Warn("Standard spectrograms will not be rendered");
Expand All @@ -71,7 +71,7 @@ public static void DrawTiles(
Log.Info("Tiling at scales: " + allImageScales.ToCommaSeparatedList());

// determine what naming format to use for tiles
var namingPattern = GetTilingProfile(common, zoomConfig, indexGeneration);
var namingPattern = GetTilingProfile(common, zoomConfig, indexGenerationData);

// pad out image so it produces a whole number of tiles
// this solves the asymmetric right padding of short audio files
Expand All @@ -93,12 +93,31 @@ public static void DrawTiles(
// TODO: does this load all spectra? even ones we don't need...
var (spectra, filteredIndexProperties) = LoadSpectra(io, analysisTag, fileStem, indexProperties);

GenerateIndexSpectrogramTiles(indexScales, ldsConfig, filteredIndexProperties, zoomConfig, spectra, indexGeneration, fileStem, namingPattern, tiler);
// false color index tiles
GenerateIndexSpectrogramTiles(
indexScales,
ldsConfig,
filteredIndexProperties,
zoomConfig,
spectra,
indexGenerationData,
fileStem,
namingPattern,
tiler);

// ####################### DRAW ZOOMED-IN SPECTROGRAMS FROM STANDARD SPECTRAL FRAMES
// standard fft frame spectrograms
if (shouldRenderStandardScale)
{
GenerateStandardSpectrogramTiles(spectra, indexGeneration, ldsConfig, filteredIndexProperties, zoomConfig, standardScales, fileStem, namingPattern, tiler);
GenerateStandardSpectrogramTiles(
spectra,
indexGenerationData,
ldsConfig,
filteredIndexProperties,
zoomConfig,
standardScales,
fileStem,
namingPattern,
tiler);
}

Log.Success("Tiling complete");
Expand Down Expand Up @@ -185,7 +204,7 @@ private static void GenerateIndexSpectrogramTiles(
Dictionary<string, IndexProperties> filteredIndexProperties,
SpectrogramZoomingConfig zoomConfig,
Dictionary<string, double[,]> spectra,
IndexGenerationData indexGeneration,
IndexGenerationData indexGenerationData,
string fileStem,
TilingProfile namingPattern,
Tiler tiler)
Expand All @@ -196,14 +215,23 @@ private static void GenerateIndexSpectrogramTiles(
foreach (double scale in indexScales)
{
Log.Info("Starting scale: " + scale);

if (indexGenerationData.RecordingDuration.TotalSeconds < scale)
{
Log.Warn($"Skipping scale step {scale} because recording duration ({indexGenerationData.RecordingDuration}) is less than 1px of scale");
continue;
}

// TODO: eventual optimisation, remove concept of super tiles
// TODO: optimisation, cache aggregation layers (currently every layer is raggregated from base level)
TimeSpan imageScale = TimeSpan.FromSeconds(scale);
var superTiles = DrawSuperTilesAtScaleFromIndexSpectrograms(
ldsConfig,
filteredIndexProperties,
zoomConfig,
imageScale,
spectra,
indexGeneration,
indexGenerationData,
fileStem,
namingPattern.ChromeOption);

Expand Down
Loading

0 comments on commit e907eb6

Please sign in to comment.