Skip to content

Commit

Permalink
Implements alternating background colors for summary indices image
Browse files Browse the repository at this point in the history
Arrange for alternating rows in image and changed labelling

Fixes #212
  • Loading branch information
towsey authored and atruskie committed Jan 31, 2019
1 parent 8a239e0 commit 2fd2ea1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
16 changes: 11 additions & 5 deletions src/AnalysisPrograms/Sandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override Task<int> Execute(CommandLineApplication app)
Log.WriteLine("# Start Time = " + tStart.ToString(CultureInfo.InvariantCulture));

//AnalyseFrogDataSet();
//Audio2CsvOverOneFile();
Audio2CsvOverOneFile();
//Audio2CsvOverMultipleFiles();

// used to get files from availae for Black rail and Least Bittern papers.
Expand Down Expand Up @@ -103,8 +103,8 @@ public override Task<int> Execute(CommandLineApplication app)
//TestTernaryPlots();
//TestDirectorySearchAndFileSearch();
//TestNoiseReduction();
Oscillations2014.TESTMETHOD_DrawOscillationSpectrogram();
Oscillations2014.TESTMETHOD_GetSpectralIndex_Osc();
//Oscillations2014.TESTMETHOD_DrawOscillationSpectrogram();
//Oscillations2014.TESTMETHOD_GetSpectralIndex_Osc();
//Test_DrawFourSpectrograms();

Console.WriteLine("# Finished Sandpit Task! Press any key to exit.");
Expand Down Expand Up @@ -380,8 +380,14 @@ public static void Audio2CsvOverOneFile()

// SERF RECORDINGS FROM 19th June 2013
// these are six hour recordings
string recordingPath = @"G:\Ecoacoustics\WavFiles\SERF\2013June19\SERF_20130619_064615_000.wav";
string outputPath = @"C:\Ecoacoustics\Output\SERF\SERFIndicesNew_2013June19";
//string recordingPath = @"G:\Ecoacoustics\WavFiles\SERF\2013June19\SERF_20130619_064615_000.wav";
//string outputPath = @"C:\Ecoacoustics\Output\SERF\SERFIndicesNew_2013June19";
//string configPath = @"C:\Work\GitHub\audio-analysis\src\AnalysisConfigFiles\Towsey.Acoustic.yml";

// USE 24-hour data or parts of from MEZ, TASMAn ISLAND, liz Znidersic
// these are six hour recordings
string recordingPath = @"C:\Ecoacoustics\WavFiles\LizZnidersic\TasmanIsland2015_Unit2_Mez\SM304256_0+1_20151114_031652.wav";
string outputPath = @"C:\Ecoacoustics\Output\Test\Test24HourRecording\TasmanIslandMez\04";
string configPath = @"C:\Work\GitHub\audio-analysis\src\AnalysisConfigFiles\Towsey.Acoustic.yml";

// GROUND PARROT
Expand Down
41 changes: 20 additions & 21 deletions src/AudioAnalysisTools/Indices/IndexDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AudioAnalysisTools.Indices
using System.Linq;
using System.Reflection;
using AnalysisBase.ResultBases;
using AudioAnalysisTools.StandardSpectrograms;
using StandardSpectrograms;
using log4net;
using LongDurationSpectrograms;
using TowseyLibrary;
Expand Down Expand Up @@ -74,11 +74,11 @@ public static Bitmap DrawImageOfSummaryIndices(
titleText,
indexCalculationDuration,
recordingStartDate,
sunriseDataFile = null);
sunriseDataFile);
}

/// <summary>
/// Converts summary indices to a tracks image
/// Converts summary indices to a tracks image.
/// </summary>
public static Bitmap DrawImageOfSummaryIndices(
Dictionary<string, IndexProperties> listOfIndexProperties,
Expand All @@ -98,6 +98,7 @@ public static Bitmap DrawImageOfSummaryIndices(
var bitmapList = new List<Tuple<IndexProperties, Image>>(dictionaryOfSummaryIndices.Keys.Count);

// accumulate the individual tracks in a List
var backgroundColour = Color.White;
foreach (string key in dictionaryOfSummaryIndices.Keys)
{
string correctKey = key;
Expand Down Expand Up @@ -130,53 +131,51 @@ public static Bitmap DrawImageOfSummaryIndices(
continue;
}

string name = ip.Name;
double[] array = dictionaryOfSummaryIndices[key];
scaleLength = array.Length;
Image bitmap = ip.GetPlotImage(array, errors);

// alternate rows have different colour to make tracks easier to read
backgroundColour = backgroundColour == Color.LightGray ? Color.White : Color.LightGray;
var bitmap = ip.GetPlotImage(array, backgroundColour, errors);
bitmapList.Add(Tuple.Create(ip, bitmap));
}

var listOfBitmaps = bitmapList
.OrderBy(tuple => tuple.Item1.Order)
// .OrderBy(tuple => tuple.Item1.Order) // don't order because want to preserve alternating gray/white rows.
.Select(tuple => tuple.Item2)
.Where(b => b != null).ToList();

//set up the composite image parameters
int X_offset = 2;
int graphWidth = X_offset + scaleLength;
int imageWidth = X_offset + scaleLength + TrackEndPanelWidth;
TimeSpan scaleDuration = TimeSpan.FromMinutes(scaleLength);
int imageHt = trackHeight * (listOfBitmaps.Count + 4); //+3 for title and top and bottom time tracks
int x_offset = 2;
int graphWidth = x_offset + scaleLength;
int imageWidth = x_offset + scaleLength + TrackEndPanelWidth;
Bitmap titleBmp = ImageTrack.DrawTitleTrack(imageWidth, trackHeight, titleText);

//Bitmap time1Bmp = ImageTrack.DrawTimeTrack(scaleDuration, TimeSpan.Zero, DrawSummaryIndices.TimeScale, graphWidth, TrackHeight, "Time (hours)");
TimeSpan xAxisPixelDuration = indexCalculationDuration;
TimeSpan fullDuration = TimeSpan.FromTicks(xAxisPixelDuration.Ticks * graphWidth);
Bitmap timeBmp1 = ImageTrack.DrawTimeRelativeTrack(fullDuration, graphWidth, trackHeight);
Bitmap timeBmp2 = timeBmp1;
Bitmap suntrack = null;
DateTimeOffset? dateTimeOffset = recordingStartDate;
if (dateTimeOffset.HasValue)
{
// draw extra time scale with absolute start time. AND THEN Do SOMETHING WITH IT.
timeBmp2 = ImageTrack.DrawTimeTrack(fullDuration, dateTimeOffset, graphWidth, trackHeight);
suntrack = SunAndMoon.AddSunTrackToImage(scaleLength, dateTimeOffset, sunriseDataFile);
}

//draw the composite bitmap
var imageList = new List<Image>();
imageList.Add(titleBmp);
imageList.Add(timeBmp1);
for (int i = 0; i < listOfBitmaps.Count; i++)
var imageList = new List<Image>
{
imageList.Add(listOfBitmaps[i]);
titleBmp,
timeBmp1,
};

foreach (var image in listOfBitmaps)
{
imageList.Add(image);
}

imageList.Add(timeBmp2);
imageList.Add(suntrack);
Bitmap compositeBmp = (Bitmap)ImageTools.CombineImagesVertically(imageList);
var compositeBmp = (Bitmap)ImageTools.CombineImagesVertically(imageList);
return compositeBmp;
}

Expand Down
12 changes: 5 additions & 7 deletions src/AudioAnalysisTools/Indices/IndexProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,25 @@ public string GetPlotAnnotation()
return string.Format(" {0} ({1:f2} .. {2:f2} {3})", this.Name, this.NormMin, this.NormMax, this.Units);
}

public Image GetPlotImage(double[] array, List<GapsAndJoins> errors = null) => this.GetPlotImage(array, Color.White, errors);

/// <summary>
/// For writing this method:
/// See CLASS: DrawSummaryIndices
/// METHOD: Bitmap ConstructVisualIndexImage(DataTable dt, string title, int timeScale, double[] order, bool doNormalise)
/// </summary>
public Image GetPlotImage(double[] array, List<GapsAndJoins> errors = null)
public Image GetPlotImage(double[] array, Color backgroundColour, List<GapsAndJoins> errors = null)
{
int dataLength = array.Length;
string annotation = this.GetPlotAnnotation();
double[] values = DataTools.NormaliseInZeroOne(array, this.NormMin, this.NormMax);

int trackWidth = dataLength + IndexDisplay.TrackEndPanelWidth;
int trackHeight = IndexDisplay.DefaultTrackHeight;
Color[] grayScale = ImageTools.GrayScale();

Bitmap bmp = new Bitmap(trackWidth, trackHeight);
Graphics g = Graphics.FromImage(bmp);
g.Clear(grayScale[240]);
g.Clear(backgroundColour);

// for pixels in the line
for (int i = 0; i < dataLength; i++)
Expand Down Expand Up @@ -312,11 +313,8 @@ public Image GetPlotImage(double[] array, List<GapsAndJoins> errors = null)
bmp.SetPixel(i, 0, Color.Gray);
}

// end over all pixels
int endWidth = trackWidth - dataLength;
var font = new Font("Arial", 9.0f, FontStyle.Regular);
g.FillRectangle(Brushes.Black, dataLength + 1, 0, endWidth, trackHeight);
g.DrawString(annotation, font, Brushes.White, new PointF(dataLength + 5, 2));
g.DrawString(annotation, font, Brushes.Black, new PointF(dataLength, 5));

// now add in image patches for possible erroneous index segments
if (errors != null && errors.Count > 0)
Expand Down

0 comments on commit 2fd2ea1

Please sign in to comment.