Skip to content

Commit

Permalink
new method to load index matrices
Browse files Browse the repository at this point in the history
1: Wrote new method ZoomCommon.LoadSpectra() to load only required spectral index matrices. Cuts down the time to load.
2: Also reworked code which cerates a focused stack of zooming spectrograms.
3: In particular comment out the code that used to do the lowest level of the stack down to standard temporal scale.
4: Had to play with ZioExtensions and class AnalysisIoInputDirectory.
  • Loading branch information
towsey committed Apr 4, 2019
1 parent 4c55275 commit 3f9fe4f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 25 deletions.
12 changes: 11 additions & 1 deletion src/Acoustics.Shared/Extensions/ZioExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ZioExtensions.cs" company="QutEcoacoustics">
// <copyright file="ZioExtensions.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 @@ -64,6 +64,16 @@ public static FileInfo ToFileInfo(this FileEntry file)
return new FileInfo(file.Path.ToOsPath());
}

public static DirectoryInfo ToDirectoryInfo(this DirectoryEntry directory)
{
Contract.Requires(directory != null);
Contract.Requires(
directory.FileSystem is PhysicalFileSystem,
$"To convert the path {directory} back to a physical filesystem, it must be from a physical file system");

return new DirectoryInfo(directory.Path.ToOsPath());
}

public static DirectoryEntry Combine(this DirectoryEntry directoryInfo, params string[] str)
{
Contract.Requires(directoryInfo != null);
Expand Down
10 changes: 8 additions & 2 deletions src/AnalysisPrograms/HerveGlotinCollaboration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace AnalysisPrograms
using System.Drawing;
using System.IO;
using System.Linq;
using AnalysisPrograms.Production;
using AudioAnalysisTools;
using AudioAnalysisTools.LongDurationSpectrograms;
using AudioAnalysisTools.LongDurationSpectrograms.Zooming;
Expand Down Expand Up @@ -172,6 +173,10 @@ public static void HiRes1()
LoggedConsole.WriteLine("# Output Directory : " + zoomingArguments.Output);

var common = new ZoomParameters(zoomingArguments.SourceDirectory.ToDirectoryEntry(), zoomingArguments.SpectrogramZoomingConfig.ToFileEntry(), false);
var io = FileSystemProvider.GetInputOutputFileSystems(
zoomingArguments.SourceDirectory,
FileSystemProvider.MakePath(zoomingArguments.Output, common.OriginalBasename, zoomingArguments.OutputFormat, "Tiles"))
.EnsureInputIsDirectory();

// Create directory if not exists
if (!Directory.Exists(zoomingArguments.Output))
Expand All @@ -182,10 +187,11 @@ public static void HiRes1()
ZoomFocusedSpectrograms.DrawStackOfZoomedSpectrograms(
zoomingArguments.SourceDirectory.ToDirectoryInfo(),
zoomingArguments.Output.ToDirectoryInfo(),
io,
common,
AcousticIndices.TowseyAcoustic,
TimeSpan.FromMinutes(focalMinute),
imageWidth,
AcousticIndices.TowseyAcoustic);
imageWidth);

// DRAW THE VARIOUS IMAGES
string fileStem = fileName;
Expand Down
46 changes: 36 additions & 10 deletions src/AnalysisPrograms/Sandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace AnalysisPrograms
using Acoustics.Shared.Csv;
using Acoustics.Tools.Wav;
using AnalyseLongRecordings;
using AnalysisPrograms.Draw.Zooming;
using AudioAnalysisTools;
using AudioAnalysisTools.DSP;
using AudioAnalysisTools.Indices;
Expand All @@ -36,7 +37,7 @@ namespace AnalysisPrograms
/// audio2sonogram - Calls AnalysisPrograms.Audio2Sonogram.Main(): Produces a sonogram from an audio file - EITHER custom OR via SOX.Generates multiple spectrogram images and oscilllations info
/// indicescsv2image - Calls DrawSummaryIndexTracks.Main(): Input csv file of summary indices. Outputs a tracks image.
/// colourspectrogram - Calls DrawLongDurationSpectrograms.Execute(): Produces LD spectrograms from matrices of indices.
/// zoomingspectrograms - Calls DrawZoomingSpectrograms.Execute(): Produces LD spectrograms on different time scales.
/// drawzoomingspectrograms - Calls DrawZoomingSpectrograms.Execute(): Produces LD spectrograms on different time scales.
/// differencespectrogram - Calls DifferenceSpectrogram.Execute(): Produces Long duration difference spectrograms
///
/// audiofilecheck - Writes information about audio files to a csv file.
Expand Down Expand Up @@ -74,7 +75,7 @@ public override Task<int> Execute(CommandLineApplication app)
//CodeToPlaceScoreTracksUnderLdfcSpectrograms();
//CodeToPlaceScoreTracksUnderSingleImage();

ConcatenateIndexFilesAndSpectrograms();
//ConcatenateIndexFilesAndSpectrograms();
//ConcatenateGreyScaleSpectrogramImages();
//ConcatenateMarineImages();
//ConcatenateImages();
Expand All @@ -83,6 +84,7 @@ public override Task<int> Execute(CommandLineApplication app)
//DrawLongDurationSpectrogram();
//DrawClusterSequence();
//DrawStandardSpectrograms();
DrawZoomingSpectrogramPyramid();

//ExtractSpectralFeatures();
//HerveGlotinMethods();
Expand Down Expand Up @@ -612,21 +614,15 @@ public static void DrawLongDurationSpectrogram()
//string opdir = @"C:\SensorNetworks\Output\QueenMaryUL\concatenated";

// false-colour spectrograms
//string ipdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\Farmstay_ECLIPSE3_20121114_060001TEST\Indices\Towsey.Acoustic";
//string opdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\Farmstay_ECLIPSE3_20121114_060001TEST\Spectrograms";
//string ipFileName = "Farmstay_ECLIPSE3_20121114_060001TEST"; //exclude the analysis type from file name i.e. "Towsey.Acoustic.Indices"
//string ipdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic.60sppx.EclipseFarmstay";
//string opdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic";
//string ipFileName = "Farmstay_ECLIPSE3_20121114-060001+1000_TEST"; //exclude the analysis type from file name i.e. "Towsey.Acoustic.Indices"
//string ipdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic.60sppx.EclipseFarmstay";
//string opdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic";

//string ipdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\Farmstay_ECLIPSE3_20121114_060001TEST\Indices\Towsey.Acoustic";
//string opdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\Farmstay_ECLIPSE3_20121114_060001TEST\Spectrograms";

// zoomable spectrograms
//string ipFileName = "TEST_TUITCE_20091215_220004"; //exclude the analysis type from file name i.e. "Towsey.Acoustic.Indices"
//string ipdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic";
//string opdir = @"C:\SensorNetworks\Output\FalseColourSpectrograms\SpectrogramZoom\Towsey.Acoustic";

//2010 Oct 13th
//var ipdir = @"C:\SensorNetworks\Output\TsheringDema\Towsey.Acoustic_OLD4";
//var opdir = @"C:\SensorNetworks\Output\TsheringDema\Towsey.Acoustic";
Expand Down Expand Up @@ -674,6 +670,36 @@ public static void DrawLongDurationSpectrogram()
DrawLongDurationSpectrograms.Execute(args);
}

/// <summary>
/// This action item = "DrawZoomingSpectrograms".
/// AnalysisPrograms.Draw.Zooming.DrawZoomingSpectrograms
/// AnalysisPrograms.Draw.Zooming.Arguments
/// Note: the path to the IndexPropertiesConfig is included in the SpectrogramZoomingConfig File.
/// It should be @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\IndexPropertiesConfig.yml".
/// </summary>
public static void DrawZoomingSpectrogramPyramid()
{
//string ipFileName = "TEST_TUITCE_20091215_220004"; //exclude the analysis type from file name i.e. "Towsey.Acoustic.Indices"
//string ipdir = @"C:\Ecoacoustics\Output\FalseColourSpectrograms\SpectrogramFocalZoom\Towsey.Acoustic.200ms.EclipseFarmstayOLD";
//MAP "\\Sef-bigdata-10\d$\tasmania_mez\output_zooming_indices2019\Towsey.Acoustic" to Q drive
string ipdir = @"Q:\TasmaniaMez";
string opdir = @"C:\Ecoacoustics\Output\FalseColourSpectrograms\SpectrogramFocalZoom\TasmaniaMezTest";

// The default zooming LDFC spectrogram config file
//var spectrogramConfigFile = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\SpectrogramZoomingConfig.yml";
var zoomingConfigFile = @"C:\Ecoacoustics\Output\FalseColourSpectrograms\SpectrogramFocalZoom\SpectrogramZoomingConfig.yml";

var args = new DrawZoomingSpectrograms.Arguments
{
SourceDirectory = ipdir,
Output = opdir,
SpectrogramZoomingConfig = zoomingConfigFile,
ZoomAction = DrawZoomingSpectrograms.Arguments.ZoomActionType.Focused,
FocusMinute = 60,
};
DrawZoomingSpectrograms.Execute(args);
}

/// <summary>
/// This action item = "concatenateIndexFiles".
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,39 @@ namespace AudioAnalysisTools.LongDurationSpectrograms.Zooming
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using Acoustics.Shared;
using Acoustics.Shared.Contracts;
using Fasterflect;
using Indices;
using TowseyLibrary;

public static class ZoomCommon
{
public static (Dictionary<string, double[,]>, Dictionary<string, IndexProperties>) LoadSpectra(
AnalysisIoInputDirectory io,
string analysisTag,
string fileStem,
LdSpectrogramConfig config,
Dictionary<string, IndexProperties> indexProperties)
{
var keys = config.GetKeys().Distinct();

// add two necessary keys for zooming
keys = keys.ToList().Append("SUM");
keys = keys.ToList().Append("DIF");

var relevantIndexProperties = keys.ToDictionary(x => x, x => indexProperties[x]);

Dictionary<string, double[,]> spectra = IndexMatrices.ReadSpectralIndices(
io.InputBase,
fileStem,
analysisTag,
keys.ToArray());

return (spectra, relevantIndexProperties);
}

public static Image DrawIndexSpectrogramCommon(
LdSpectrogramConfig config,
IndexGenerationData indexGenerationData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,38 @@ namespace AudioAnalysisTools.LongDurationSpectrograms
using StandardSpectrograms;
using TowseyLibrary;
using Zooming;
using Zio;
using Zio.FileSystems;

public static class ZoomFocusedSpectrograms
{
public static void DrawStackOfZoomedSpectrograms(DirectoryInfo inputDirectory, DirectoryInfo outputDirectory, ZoomParameters common, TimeSpan focalTime, int imageWidth, string analysisType)
public static void DrawStackOfZoomedSpectrograms(
DirectoryInfo inputDirectory,
DirectoryInfo outputDirectory,
AnalysisIoInputDirectory io,
ZoomParameters common,
string analysisTag,
TimeSpan focalTime,
int imageWidth)
{
var zoomConfig = common.SpectrogramZoomingConfig;
LdSpectrogramConfig ldsConfig = common.SpectrogramZoomingConfig.LdSpectrogramConfig;

var distributions = common.IndexDistributions;
var indexGeneration = common.IndexGenerationData;

//var distributions = common.IndexDistributions;
string fileStem = common.OriginalBasename;

var indexGeneration = common.IndexGenerationData;
TimeSpan dataScale = indexGeneration.IndexCalculationDuration;

// ####################### DERIVE ZOOMED OUT SPECTROGRAMS FROM SPECTRAL INDICES

string[] keys = { "ACI", "BGN", "CVR", "DIF", "ENT", "EVN", "PMN", "PMN", "RHZ", "RVT", "RPS", "RNG", "SUM", "SPT" };
var indexProperties = InitialiseIndexProperties.FilterIndexPropertiesForSpectralOnly(zoomConfig.IndexProperties);
Dictionary<string, double[,]> spectra = IndexMatrices.ReadSpectralIndices(inputDirectory, fileStem, analysisType, keys);
//var indexGenerationData = common.IndexGenerationData;
var indexProperties = zoomConfig.IndexProperties;
var (spectra, filteredIndexProperties) = ZoomCommon.LoadSpectra(io, analysisTag, fileStem, zoomConfig.LdSpectrogramConfig, indexProperties);

Stopwatch sw = Stopwatch.StartNew();

// standard scales in seconds per pixel.
double[] imageScales = { 60, 24, 12, 6, 2, 1, 0.6, 0.2 };
// Set the default time-scales in seconds per pixel.
// These were changed on 3rd April 2019 to better match those in the current zooming config file.
double[] imageScales = { 60, 30, 15, 7.5, 3.2, 1.6, 0.8, 0.4, 0.2 };
if (zoomConfig.SpectralIndexScale != null)
{
imageScales = zoomConfig.SpectralIndexScale;
Expand All @@ -71,7 +78,7 @@ public static void DrawStackOfZoomedSpectrograms(DirectoryInfo inputDirectory, D
for (int i = 0; i < scaleCount; i++)
{
var imageScale = TimeSpan.FromSeconds(imageScales[i]);
var image = DrawIndexSpectrogramAtScale(ldsConfig, indexGeneration, indexProperties, focalTime, dataScale, imageScale, imageWidth, spectra, fileStem);
var image = DrawIndexSpectrogramAtScale(ldsConfig, indexGeneration, filteredIndexProperties, focalTime, dataScale, imageScale, imageWidth, spectra, fileStem);
if (image != null)
{
imageList.Add(image);
Expand All @@ -84,6 +91,8 @@ public static void DrawStackOfZoomedSpectrograms(DirectoryInfo inputDirectory, D
LoggedConsole.WriteLine("Finished spectrograms derived from spectral indices. Elapsed time = " + sw.Elapsed.TotalSeconds + " seconds");

// ####################### DERIVE ZOOMED IN SPECTROGRAMS FROM STANDARD SPECTRAL FRAMES

/*
int[] compressionFactor = { 8, 4, 2, 1 };
int compressionCount = compressionFactor.Length;
sw = Stopwatch.StartNew();
Expand Down Expand Up @@ -136,6 +145,7 @@ public static void DrawStackOfZoomedSpectrograms(DirectoryInfo inputDirectory, D
sw.Stop();
LoggedConsole.WriteLine("Finished spectrograms derived from standard frames. Elapsed time = " + sw.Elapsed.TotalSeconds + " seconds");
*/

// combine the images into a stack
Image combinedImage = ImageTools.CombineImagesVertically(imageList);
Expand Down

0 comments on commit 3f9fe4f

Please sign in to comment.