Skip to content

Commit

Permalink
Refactor code which draws histograms
Browse files Browse the repository at this point in the history
Use the full range of the y-axis scale when drawing histogram.
Add the number of the modal bin to the collected statistics.
Change method arguments to accept modalBin.
Remove duplicated lines from SpectrogramConstants.cs.
Write SandPit method to test drawing of histograms.
this can be turned into a unit test.
NOTE: The "gap" in the histograms noted in Issue #186 is due to way Rectangular bars were drawn. Nothing to do with inadequate size of int.
  • Loading branch information
towsey committed Mar 20, 2019
1 parent b373f68 commit d0228f1
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 42 deletions.
315 changes: 312 additions & 3 deletions src/AnalysisPrograms/Sandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public override Task<int> Execute(CommandLineApplication app)
//CodeToPlaceScoreTracksUnderLdfcSpectrograms();
//CodeToPlaceScoreTracksUnderSingleImage();

ConcatenateIndexFilesAndSpectrograms();
//ConcatenateIndexFilesAndSpectrograms();
//ConcatenateGreyScaleSpectrogramImages();
//ConcatenateMarineImages();
//ConcatenateImages();
Expand All @@ -89,6 +89,7 @@ public override Task<int> Execute(CommandLineApplication app)
//ResourcesForRheobatrachusSilusRecogniser();
//TestAnalyseLongRecordingUsingArtificialSignal();
//TestArbimonSegmentationAlgorithm();
TestDrawHistogram();
//TestEigenValues();
//TestChannelIntegrity();
//TestDct();
Expand Down Expand Up @@ -742,7 +743,7 @@ public static void ConcatenateIndexFilesAndSpectrograms()
colorMap2 = "BGN-POW-CLS";
// ########################## END of Yvonne's recordings of SM2 and SM4
*/

/*
// ########################## CONCATENATION of 24-hour TEST recordings from Liz Znidersic
// top level directory
string[] dataDirs =
Expand All @@ -761,7 +762,7 @@ public static void ConcatenateIndexFilesAndSpectrograms()
// there are three options for rendering of gaps/missing data: NoGaps, TimedGaps and EchoGaps.
gapRendering = "TimedGaps";

*/
// ########################## END of 24-hour TEST recordings

/*
Expand Down Expand Up @@ -1496,6 +1497,314 @@ public static void TestAnalyseLongRecordingUsingArtificialSignal()
image2.Save(ldsBgnSpectrumFile.FullName);
}

/// <summary>
/// Tests the drawing of a histogram of BGN values dervied from data located at:
/// Z:\tasmania_mez\output_zooming_indices\Towsey.Acoustic
/// This test is by way of attending to QUTEcoacoustics Issue #186
/// The distribution statistics are:
/// "Minimum": -110.74302631902833,
/// "Maximum": -44.00885043566803,
/// "Mode": -96.9512966364672,
/// "StandardDeviation": 4.0040505530016182,
/// "UpperPercentile": 98,
/// "UpperPercentileBin": 174,
/// "Count": 221181440.
/// </summary>
public static void TestDrawHistogram()
{
int[] distribution =
{
1, 2, 7, 8, 73, 83, 195, 432, 682,1644,2621,4655,7813,11935,17246,24803,33591,43249,54333,65208,75729,87135,96440,104096,110214,
121722,
128390,
138336,
147016,
156372,
163644,
166318,
164770,
163155,
158976,
155657,
155933,
156507,
158176,
149535,
131686,
103811,
81364,
71492,
77750,
102405,
141582,
174329,
203910,
239008,
286775,
356480,
475234,
639319,
881903,
1383086,
2448051,
4478415,
7651295,
11607067,
15415577,
17915490,
18405485,
16783249,
13824441,
10585914,
7907210,
6030484,
4794337,
3974758,
3357695,
2940875,
2620874,
2349632,
2122577,
1932630,
1769112,
1649037,
1547783,
1459096,
1379839,
1303302,
1218706,
1141774,
1065302,
989003,
915842,
849973,
792082,
751371,
705448,
673538,
640900,
609732,
590720,
570525,
554886,
542704,
525214,
511713,
504054,
489061,
482371,
477460,
469684,
465019,
464784,
463130,
471294,
472988,
477931,
487694,
490929,
504352,
506022,
514559,
524511,
532120,
539667,
549030,
550959,
549555,
543018,
533275,
528735,
511541,
505002,
503153,
494837,
488639,
479920,
468004,
458980,
452198,
439419,
427299,
419077,
412942,
402728,
392793,
380144,
371799,
363792,
355276,
346957,
338039,
328357,
324295,
316964,
305427,
303147,
296545,
293390,
282624,
274414,
268267,
258093,
252943,
247530,
238958,
232172,
226631,
219153,
211740,
206198,
201907,
194437,
189735,
187535,
182418,
177998,
171918,
164774,
162404,
158788,
154316,
150446,
146819,
144324,
143652,
142663,
140900,
138487,
135326,
135268,
131577,
130240,
130077,
129643,
124460,
125488,
122459,
120075,
117914,
116033,
112523,
109994,
103546,
101266,
97021,
92399,
87952,
82482,
78217,
73882,
68510,
65289,
60752,
57658,
53378,
50235,
47692,
43817,
40615,
38038,
32463,
29407,
26603,
24261,
21458,
17756,
15624,
13682,
11949,
9779,
9067,
7517,
6549,
6308,
5118,
4441,
3533,
3466,
2931,
2529,
1994,
2099,
1820,
1388,
1128,
1141,
1005,
809,
737,
675,
472,
522,
423,
515,
268,
286,
175,
178,
197,
180,
121,
138,
110,
104,
104,
80,
85,
50,
59,
102,
50,
39,
38,
30,
37,
24,
25,
21,
63,
19,
8,
9,
14,
18,
15,
11,
12,
10,
7,
66,6,4,3,1,4,2,2,4,2,3,1,1,3,1,51,
};

string label = "Test";
double min = -110.74302631902833;
double max = -44.00885043566803;
double mode = -96.9512966364672;
double sd = 4.0040505530016182;
// "UpperPercentile": 98,
int upperPercentileBin = 174;
double upperPercentileValue = -72.037;
//int count = 221181440;
int count = distribution.Sum();
var dict = new Dictionary<string, double>()
{
{ "min", min },
{ "max", max },
{ "mode", mode },
{ "sd", sd },
{ "98%", upperPercentileValue },
{ "count", count },
};
int imageWidth = 300;
int imageHeight = 100;
var dirPath = new DirectoryInfo("C:\\Temp");

var image = GraphsAndCharts.DrawHistogram(label, distribution, upperPercentileBin, dict, imageWidth, imageHeight);
var path = Path.Combine(dirPath.FullName, "name.png");
image.Save(path);
}

/// <summary>
/// experiments with Mitchell-Aide ARBIMON segmentation algorithm
/// Three steps: (1) Flattening spectrogram by subtracting the median bin value from each freq bin.
Expand Down
10 changes: 6 additions & 4 deletions src/AudioAnalysisTools/Indices/IndexDistributions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class SpectralStats

public double Mode { get; set; }

public int ModalBin { get; set; }

public double StandardDeviation { get; set; }

public int UpperPercentile { get; set; }
Expand Down Expand Up @@ -219,15 +221,15 @@ public static SpectralStats GetModeAndOneTailedStandardDeviation(double[,] matri
int upperPercentile = 0;
double[] values = DataTools.Matrix2Array(matrix);
const bool displayHistogram = false;
double min, max, mode, sd;
DataTools.GetModeAndOneTailedStandardDeviation(values, displayHistogram, out min, out max, out mode, out sd);
DataTools.GetModeAndOneTailedStandardDeviation(values, displayHistogram, out var min, out var max, out var modalBin, out var mode, out var sd);
int[] histogram = Histogram.Histo(matrix, binCount);

return new SpectralStats()
{
Minimum = min,
Maximum = max,
Mode = mode,
ModalBin = modalBin,
StandardDeviation = sd,
UpperPercentile = upperPercentile,
Distribution = histogram,
Expand All @@ -243,14 +245,14 @@ public static SpectralStats GetModeAndOneTailedStandardDeviation(double[,] matri
public static SpectralStats GetModeAndOneTailedStandardDeviation(double[] values, int binCount, int upperPercentile)
{
const bool displayHistogram = false;
double min, max, mode, sd;
DataTools.GetModeAndOneTailedStandardDeviation(values, displayHistogram, out min, out max, out mode, out sd);
DataTools.GetModeAndOneTailedStandardDeviation(values, displayHistogram, out var min, out var max, out var modalBin, out var mode, out var sd);
int[] histogram = Histogram.Histo(values, binCount);

return new SpectralStats()
{
Minimum = min,
Maximum = max,
ModalBin = modalBin,
Mode = mode,
StandardDeviation = sd,
UpperPercentile = upperPercentile,
Expand Down
Loading

0 comments on commit d0228f1

Please sign in to comment.