Skip to content

Commit

Permalink
Made correction of blue colour an optional parameter
Browse files Browse the repository at this point in the history
Issue #274 Made correction of blue colour an optional parameter in the LDSpectorgramConfig file.
Some Resharper motivated changes.
  • Loading branch information
towsey committed Dec 4, 2019
1 parent e69360f commit 18c7bb6
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 197 deletions.
2 changes: 1 addition & 1 deletion src/AnalysisConfigFiles/IndexPropertiesConfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ CVR:
DefaultValue: 0.0
DoDisplay: true
NormMin: 0.0
NormMax: 0.3
NormMax: 0.4
CalculateNormBounds: false
ProjectID: Acoustic Indices
Units: ""
Expand Down
7 changes: 7 additions & 0 deletions src/AnalysisConfigFiles/SpectrogramFalseColourConfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ ColorMap2: "BGN-PMN-CVR"
# Generally usage suggests that a value of -0.25 is suitable. i.e. a slight de-emphasis.
ColourFilter: -0.25

# The third index in the color map is always mapped to blue. The eye is less sensitive to blue and it can be difficult to see in dark background.
# Therefore we enhance the blue by making it brighter, but only when the red and green values are low.
# This could be done better but can be a helpful! The intention is to create a more visible light blue color.
# The default value for BlueEnhanceParameter = 0.0 i.e. do no enhancement.
# Suggested value is 0.4 when want to enhance visualisation of the "blue" index.
BlueEnhanceParameter: 0.4

# minutes x-axis scale
XAxisTicIntervalSeconds: 3600

Expand Down
74 changes: 34 additions & 40 deletions src/AnalysisPrograms/DrawLongDurationSpectrograms.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DrawLongDurationSpectrograms.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 @@ -26,26 +26,23 @@ namespace AnalysisPrograms
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Acoustics.Shared;
using Acoustics.Shared.Csv;
using AnalyseLongRecordings;
using AnalysisPrograms.Production;
using AnalysisPrograms.Production.Arguments;
using AnalysisPrograms.Production.Validation;
using AudioAnalysisTools;
using AudioAnalysisTools.Indices;
using AudioAnalysisTools.LongDurationSpectrograms;
using AudioAnalysisTools.LongDurationSpectrograms.Zooming;
using AudioAnalysisTools.StandardSpectrograms;
using log4net;
using McMaster.Extensions.CommandLineUtils;
using Production;
using Production.Arguments;
using Production.Validation;
using TowseyLibrary;

using Zio;

/// <summary>
Expand Down Expand Up @@ -149,10 +146,7 @@ public static void Execute(Arguments arguments)
config = LdSpectrogramConfig.ReadYamlToConfig(arguments.FalseColourSpectrogramConfig.ToFileInfo());
}

string originalBaseName;
string[] otherSegments;
string analysisTag;
FilenameHelpers.ParseAnalysisFileName(indexGenerationDataFile, out originalBaseName, out analysisTag, out otherSegments);
FilenameHelpers.ParseAnalysisFileName(indexGenerationDataFile, out var originalBaseName, out var _, out var _);

// CHECK FOR ERROR SEGMENTS - get zero signal array
var input = arguments.InputDataDirectory.ToDirectoryInfo();
Expand Down Expand Up @@ -194,43 +188,40 @@ public static int DrawAggregatedSpectrograms(Arguments arguments, string fileSte
int frameCount = spectra[keys[0]].GetLength(1);
double spectrogramScale = 0.1;
TimeSpan timeScale = TimeSpan.FromSeconds(spectrogramScale);
DirectoryInfo outputDirectory = arguments.OutputDirectory.ToDirectoryInfo();
var outputDirectory = arguments.OutputDirectory.ToDirectoryInfo();

Image combinedImage = DrawGrayScaleSpectrograms(arguments, fileStem, timeScale, spectra);
string fileName = Path.Combine(outputDirectory.FullName, fileStem + ".CombinedGreyScale.png");
combinedImage.Save(fileName);

// Draw False-colour Spectrograms
combinedImage = DrawFalseColourSpectrograms(fileStem, timeScale, arguments.IndexPropertiesConfig.ToFileInfo(), spectra);
// Draw False-color Spectrograms
combinedImage = DrawFalseColorSpectrograms(fileStem, timeScale, arguments.IndexPropertiesConfig.ToFileInfo(), spectra);
fileName = Path.Combine(outputDirectory.FullName, fileStem + ".TwoMaps.png");
combinedImage.Save(fileName);
return frameCount;
} // method DrawAggregatedSpectrograms()

public static Image DrawGrayScaleSpectrograms(Arguments arguments, string fileStem, TimeSpan dataScale, Dictionary<string, double[,]> spectra = null)
{
// default values
int sampleRate = 22050;
int frameWidth = 512;

//double backgroundFilter = 0.0; // 0.0 means small values are removed.
double backgroundFilter = 0.75; // 0.75 means small values are accentuated.
string analysisType = AcousticIndices.TowseyAcoustic;
string[] keys = LDSpectrogramRGB.GetArrayOfAvailableKeys();

//LoggedConsole.WriteLine("# Spectrogram Config file: " + arguments.SpectrogramConfigPath);
//LoggedConsole.WriteLine("# Index Properties Config file: " + arguments.IndexPropertiesConfig);
var inputDirectory = arguments.InputDataDirectory;
Dictionary<string, IndexProperties> indexProperties = IndexProperties.GetIndexProperties(arguments.IndexPropertiesConfig.ToFileInfo());

if (spectra == null)
{
//C:\SensorNetworks\Output\BIRD50\Training\ID0001\Towsey.Acoustic\ID0001__Towsey.Acoustic.ACI
spectra = IndexMatrices.ReadSpectralIndices(inputDirectory.ToDirectoryInfo(), fileStem, analysisType, keys);
}

// note: the spectra are oriented as per visual orientation, i.e. xAxis = time frames
//int frameCount = spectra[keys[0]].GetLength(1);
var cs1 = new LDSpectrogramRGB(minuteOffset: TimeSpan.Zero, xScale: dataScale, sampleRate: sampleRate, frameWidth: frameWidth, colourMap: null)
var cs1 = new LDSpectrogramRGB(minuteOffset: TimeSpan.Zero, xScale: dataScale, sampleRate: sampleRate, frameWidth: frameWidth, colorMap: null)
{
FileName = fileStem,
BackgroundFilter = backgroundFilter,
Expand Down Expand Up @@ -273,15 +264,15 @@ public static Image DrawGrayScaleSpectrograms(Arguments arguments, string fileSt
public static Image DrawFalseColourSpectrograms(Arguments args, string fileStem, Dictionary<string, double[,]> spectra = null)
{
//DirectoryInfo inputDirectory = args.InputDataDirectory;
FileInfo indexPropertiesConfig = args.IndexPropertiesConfig.ToFileInfo();
var indexPropertiesConfig = args.IndexPropertiesConfig.ToFileInfo();
Dictionary<string, IndexProperties> indexProperties = IndexProperties.GetIndexProperties(indexPropertiesConfig);
return DrawFalseColourSpectrograms(args, fileStem, indexProperties, spectra);
return DrawFalseColorSpectrograms(args, fileStem, indexProperties, spectra);
}

/// <summary>
/// Draws two false colour spectrograms using a default set of arguments
/// Draws two false-color spectrograms using a default set of arguments.
/// </summary>
public static Image DrawFalseColourSpectrograms(string fileStem, TimeSpan dataScale, FileInfo indexPropertiesConfig, Dictionary<string, double[,]> spectra = null)
public static Image DrawFalseColorSpectrograms(string fileStem, TimeSpan dataScale, FileInfo indexPropertiesConfig, Dictionary<string, double[,]> spectra = null)
{
// read in index properties and create a new entry for "PHN"
Dictionary<string, IndexProperties> indexProperties = IndexProperties.GetIndexProperties(indexPropertiesConfig);
Expand All @@ -296,10 +287,10 @@ public static Image DrawFalseColourSpectrograms(string fileStem, TimeSpan dataSc
args.ColourMap2 = LDSpectrogramRGB.DefaultColorMap2;
args.TemporalScale = dataScale;

return DrawFalseColourSpectrograms(args, fileStem, indexProperties, spectra);
return DrawFalseColorSpectrograms(args, fileStem, indexProperties, spectra);
}

public static Image DrawFalseColourSpectrograms(Arguments args, string fileStem, Dictionary<string, IndexProperties> indexProperties, Dictionary<string, double[,]> spectra = null)
public static Image DrawFalseColorSpectrograms(Arguments args, string fileStem, Dictionary<string, IndexProperties> indexProperties, Dictionary<string, double[,]> spectra = null)
{
// note: the spectra are oriented as per visual orientation, i.e. xAxis = time framesDictionary<string, Int16>.KeyCollection keys = AuthorList.Keys
// string[] keys = spectra.Keys.ToCommaSeparatedList().Split(',');
Expand All @@ -310,53 +301,56 @@ public static Image DrawFalseColourSpectrograms(Arguments args, string fileStem,
double backgroundFilter = 0.75; // 0.75 means small values are accentuated.
var minuteOffset = TimeSpan.Zero;
var dataScale = args.TemporalScale;
string colourMap = args.ColourMap1 ?? LDSpectrogramRGB.DefaultColorMap1;
var cs1 = new LDSpectrogramRGB(minuteOffset, dataScale, sampleRate, frameWidth, colourMap)
string colorMap = args.ColourMap1 ?? LDSpectrogramRGB.DefaultColorMap1;
var cs1 = new LDSpectrogramRGB(minuteOffset, dataScale, sampleRate, frameWidth, colorMap)
{
FileName = fileStem,
BackgroundFilter = backgroundFilter,
IndexCalculationDuration = dataScale,
};
cs1.SetSpectralIndexProperties(indexProperties); // set the relevant dictionary of index properties

// set the relevant dictionary of index properties
cs1.SetSpectralIndexProperties(indexProperties);
cs1.SpectrogramMatrices = spectra;

var image1 = cs1.DrawFalseColourSpectrogramChromeless("NEGATIVE", colourMap);
// get parameter from the config file.
var configFile = args.FalseColourSpectrogramConfig.ToFileInfo();
var config = LdSpectrogramConfig.ReadYamlToConfig(configFile);
var blueEnhanceParameter = config.BlueEnhanceParameter ?? 0.0;

var image1 = cs1.DrawFalseColorSpectrogramChromeless("NEGATIVE", colorMap, blueEnhanceParameter);
var fullDuration = TimeSpan.FromSeconds(image1.Width * dataScale.TotalSeconds);

string title = fileStem;
var titleImage = LDSpectrogramRGB.DrawTitleBarOfFalseColourSpectrogram(title, image1.Width);
int trackHeight = 20;
var timeScale = ImageTrack.DrawTimeRelativeTrack(fullDuration, image1.Width, trackHeight);

colourMap = args.ColourMap2 ?? LDSpectrogramRGB.DefaultColorMap2;
var image2 = cs1.DrawFalseColourSpectrogramChromeless("NEGATIVE", colourMap);
colorMap = args.ColourMap2 ?? LDSpectrogramRGB.DefaultColorMap2;
var image2 = cs1.DrawFalseColorSpectrogramChromeless("NEGATIVE", colorMap, blueEnhanceParameter);
var list = new List<Image> { titleImage, image1, timeScale, image2 };
var combinedImage = ImageTools.CombineImagesVertically(list.ToArray());
return combinedImage;
}

/// <summary>
/// The integer returned from this method is the number of seconds duration of the spectrogram.
/// Note that this method is called only when spectrogramScale = 0.1
/// Note that this method is called only when spectrogramScale = 0.1.
/// </summary>
public static int DrawRidgeSpectrograms(Arguments arguments, string fileStem, Dictionary<string, double[,]> spectra = null)
{
//LoggedConsole.WriteLine("# Spectrogram Config file: " + arguments.SpectrogramConfigPath);
//LoggedConsole.WriteLine("# Index Properties Config file: " + arguments.IndexPropertiesConfig);
var inputDirectory = arguments.InputDataDirectory;
var outputDirectory = arguments.OutputDirectory;
var indexPropertiesConfig = arguments.IndexPropertiesConfig;
double spectrogramScale = 0.1;

// var dataScale = TimeSpan.FromSeconds(spectrogramScale);

// draw the spectrogram images
var labelledImage = DrawRidgeSpectrograms(inputDirectory.ToDirectoryInfo(), indexPropertiesConfig.ToFileInfo(), fileStem, spectrogramScale, spectra = null);
var labeledImage = DrawRidgeSpectrograms(inputDirectory.ToDirectoryInfo(), indexPropertiesConfig.ToFileInfo(), fileStem, spectrogramScale, spectra = null);

// combine and save
string fileName = Path.Combine(outputDirectory.ToDirectoryInfo().FullName, fileStem + ".Ridges.png");
labelledImage.Save(fileName);
return (int)Math.Round(labelledImage.Width * spectrogramScale);
labeledImage.Save(fileName);
return (int)Math.Round(labeledImage.Width * spectrogramScale);
} // method DrawRidgeSpectrograms()

public static Image DrawRidgeSpectrograms(DirectoryInfo inputDirectory, FileInfo ipConfig, string fileStem, double scale, Dictionary<string, double[,]> spectra = null)
Expand All @@ -377,7 +371,7 @@ public static Image DrawRidgeSpectrograms(DirectoryInfo inputDirectory, FileInfo
spectra = IndexMatrices.ReadSpectralIndices(inputDirectory, fileStem, analysisType, keys);
}

var cs1 = new LDSpectrogramRGB(minuteOffset: TimeSpan.Zero, xScale: dataScale, sampleRate: 22050, frameWidth: 512, colourMap: null)
var cs1 = new LDSpectrogramRGB(minuteOffset: TimeSpan.Zero, xScale: dataScale, sampleRate: 22050, frameWidth: 512, colorMap: null)
{
FileName = fileStem,
BackgroundFilter = backgroundFilter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public LdSpectrogramConfig()
this.ColorMap1 = LDSpectrogramRGB.DefaultColorMap1;
this.ColorMap2 = LDSpectrogramRGB.DefaultColorMap2;
this.ColourFilter = SpectrogramConstants.BACKGROUND_FILTER_COEFF;
this.BlueEnhanceParameter = 0.0;
this.XAxisTicInterval = SpectrogramConstants.X_AXIS_TIC_INTERVAL;
this.FreqScale = "Linear";
this.YAxisTicInterval = 1000;
Expand All @@ -41,22 +42,28 @@ public LdSpectrogramConfig()
public string FreqScale { get; set; }

/// <summary>
/// Gets or sets parameter to manipulate the colour map and appearance of the false-colour spectrogram.
/// Gets or sets parameter to manipulate the color map and appearance of the false-colour spectrogram.
/// </summary>
public string ColorMap1 { get; set; }

/// <summary>
/// Gets or sets parameter to manipulate the colour map and appearance of the false-colour spectrogram
/// Pass two colour maps because two maps convey more information.
/// Pass two color maps because two maps convey more information.
/// </summary>
public string ColorMap2 { get; set; }

/// <summary>
/// Gets or sets value of the colour filter.
/// Gets or sets value of the color filter.
/// Its value must be less than 1.0. Good value is 0.75.
/// </summary>
public double? ColourFilter { get; set; }

/// <summary>
/// Gets or sets value of the blue enhancement parameter.
/// Its value must be in 0.0 to 1.0. Current suggested value is 0.5.
/// </summary>
public double? BlueEnhanceParameter { get; set; }

/// <summary>
/// Gets or sets the default XAxisTicInterval.
/// The default assumes one minute spectra i.e. 60 per hour
Expand Down Expand Up @@ -119,13 +126,14 @@ public static LdSpectrogramConfig GetDefaultConfig()
}

/// <summary>
/// Gets a default config for long-duration false-colour spectrograms.
/// Gets a default config for long-duration false-color spectrograms.
/// But allows caller to substitute custom color maps.
/// </summary>
public static LdSpectrogramConfig GetDefaultConfig(string colourMap1, string colourMap2)
public static LdSpectrogramConfig GetDefaultConfig(string colorMap1, string colorMap2)
{
var ldSpectrogramConfig = GetDefaultConfig();
ldSpectrogramConfig.ColorMap1 = colourMap1;
ldSpectrogramConfig.ColorMap2 = colourMap2;
ldSpectrogramConfig.ColorMap1 = colorMap1;
ldSpectrogramConfig.ColorMap2 = colorMap2;
return ldSpectrogramConfig;
}

Expand Down Expand Up @@ -164,9 +172,6 @@ public static string[] GetKeys(string colorMap)
return keys;
}

public string[] GetKeys()
{
return GetKeys(this.ColorMap1, this.ColorMap2);
}
public string[] GetKeys() => GetKeys(this.ColorMap1, this.ColorMap2);
}
}
Loading

0 comments on commit 18c7bb6

Please sign in to comment.