Beispiel #1
0
        /// <summary>
        /// This test checks a score array (array of doubles) against a standard or benchmark previously stored.
        /// </summary>
        public static void CompareArrayWithBenchmark(string testName, double[] scoreArray, FileInfo scoreFile)
        {
            LoggedConsole.WriteLine("# TESTING: Starting benchmark test for " + testName + ":");
            LoggedConsole.WriteLine("#          Comparing passed array of double with content of file <" + scoreFile.Name + ">");
            bool allOk      = true;
            var  scoreLines = FileTools.ReadTextFile(scoreFile.FullName);

            if (scoreArray.Length != scoreLines.Count)
            {
                LoggedConsole.WriteWarnLine("   FAIL! SCORE ARRAY not same length as Benchmark.");
                return;
            }

            for (int i = 0; i < scoreLines.Count; i++)
            {
                var str = scoreArray[i].ToString(CultureInfo.InvariantCulture);
                if (!scoreLines[i].Equals(str))
                {
                    LoggedConsole.WriteWarnLine($"Line {i}: {str} NOT= benchmark <{scoreLines[i]}>");
                    allOk = false;
                }
            }

            if (allOk)
            {
                LoggedConsole.WriteSuccessLine("   SUCCESS! Passed the SCORE ARRAY TEST.");
            }
            else
            {
                LoggedConsole.WriteWarnLine("   FAILED THE SCORE ARRAY TEST");
            }

            LoggedConsole.WriteLine("Completed benchmark test.");
        }
Beispiel #2
0
        public static void CompareTwoArrays(double[] array1, double[] array2)
        {
            LoggedConsole.WriteLine("# TESTING: Compare two arrays of double");
            bool allOk = true;

            if (array1.Length != array2.Length)
            {
                LoggedConsole.WriteWarnLine("   FAIL! ARRAYS are not of same length.");
                return;
            }

            for (int i = 0; i < array1.Length; i++)
            {
                if (array1[i] != array2[i])
                {
                    LoggedConsole.WriteWarnLine($"Line {i}: {array1[i]} != {array1[i]}");
                    allOk = false;
                }
            }

            if (allOk)
            {
                LoggedConsole.WriteSuccessLine("   SUCCESS! Two arrays are equal.");
            }
            else
            {
                LoggedConsole.WriteWarnLine("   FAILED THE ARRAY TEST");
            }

            LoggedConsole.WriteLine("Completed test.");
        }
Beispiel #3
0
        public static void TestLogging()
        {
            Contract.Requires <InvalidOperationException>(
                configured,
                "The logger system must be initialised before the logging can be tested");

            var log = LogManager.GetLogger(nameof(Logging));

            log.Prompt("Log test PROMPT");
            log.Fatal("Log test FATAL");
            log.Error("Log test ERROR");
            log.Warn("Log test WARN");
            log.Success("Log test SUCCESS");
            log.Info("Log test INFO");
            log.Debug("Log test DEBUG");
            log.Trace("Log test TRACE");
            log.Verbose("Log test VERBOSE");
            LoggedConsole.WriteFatalLine("Clean wrapper FATAL", new Exception("I'm a fake"));
            LoggedConsole.Log.Fatal("Clean log FATAL", new Exception("I'm a fake"));
            LoggedConsole.WriteErrorLine("Clean wrapper ERROR");
            LoggedConsole.Log.Error("Clean log ERROR");
            LoggedConsole.WriteWarnLine("Clean wrapper WARN");
            LoggedConsole.Log.Warn("Clean log WARN");
            LoggedConsole.WriteSuccessLine("Clean wrapper SUCCESS");
            LoggedConsole.Log.Success("Clean log SUCCESS");
            LoggedConsole.WriteLine("Clean wrapper INFO");
            LoggedConsole.Log.Info("Clean log INFO");
        }
Beispiel #4
0
        public static void TestMethod_GenerateSignal1()
        {
            int    sampleRate = 22050;
            double duration   = 20; // signal duration in seconds

            int[]  harmonics  = { 500, 1000, 2000, 4000, 8000 };
            int    windowSize = 512;
            var    freqScale  = new FrequencyScale(sampleRate / 2, windowSize, 1000);
            string path       = @"C:\SensorNetworks\Output\Sonograms\UnitTestSonograms\SineSignal1.png";

            var recording  = GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);
            var sonoConfig = new SonogramConfig
            {
                WindowSize              = freqScale.WindowSize,
                WindowOverlap           = 0.0,
                SourceFName             = "Signal1",
                NoiseReductionType      = NoiseReductionType.Standard,
                NoiseReductionParameter = 0.12,
            };
            var sonogram = new AmplitudeSonogram(sonoConfig, recording.WavReader);

            // pick a row, any row
            var oneSpectrum = MatrixTools.GetRow(sonogram.Data, 40);

            oneSpectrum = DataTools.normalise(oneSpectrum);
            var peaks = DataTools.GetPeaks(oneSpectrum, 0.5);

            for (int i = 2; i < peaks.Length - 2; i++)
            {
                if (peaks[i])
                {
                    LoggedConsole.WriteLine($"bin ={freqScale.BinBounds[i, 0]},  Herz={freqScale.BinBounds[i, 1]}-{freqScale.BinBounds[i + 1, 1]}  ");
                }
            }

            if (peaks[11] && peaks[22] && peaks[45] && peaks[92] && peaks[185])
            {
                LoggedConsole.WriteSuccessLine("Spectral Peaks found at correct places");
            }
            else
            {
                LoggedConsole.WriteErrorLine("Spectral Peaks found at INCORRECT places");
            }

            foreach (int h in harmonics)
            {
                LoggedConsole.WriteLine($"Harmonic {h}Herz  should be in bin  {freqScale.GetBinIdForHerzValue(h)}");
            }

            // spectrogram without framing, annotation etc
            var    image = sonogram.GetImage();
            string title = $"Spectrogram of Harmonics: {DataTools.Array2String(harmonics)}   SR={sampleRate}  Window={windowSize}";

            image = sonogram.GetImageFullyAnnotated(image, title, freqScale.GridLineLocations);
            image.Save(path);
        }
Beispiel #5
0
        /// <summary>
        /// Writes all recognised IAnalysers to Console.
        /// 1. Returns list of available analyses
        /// Signed off: Anthony Truskinger 2016.
        /// </summary>
        public override Task <int> Execute(CommandLineApplication app)
        {
            LoggedConsole.WriteLine("\nListing the available IAnalyzer2 implementations:\n");

            var table = this.GetAnalyzersTable();

            LoggedConsole.WriteLine(table.ToString());

            LoggedConsole.WriteSuccessLine("\nFINISHED");

            return(this.Ok());
        }
Beispiel #6
0
        internal static void HangBeforeExit()
        {
#if DEBUG
            if (AppConfigHelper.IsMono)
            {
                return;
            }

            // if Michael is debugging with visual studio, this will prevent the window closing.
            Process parentProcess = ProcessExtensions.ParentProcessUtilities.GetParentProcess();
            if (parentProcess.ProcessName == "devenv")
            {
                LoggedConsole.WriteSuccessLine("FINISHED: Press RETURN key to exit.");
                Console.ReadLine();
            }
#endif
        }
Beispiel #7
0
        public void TestLogging()
        {
            var log = LogManager.GetLogger(typeof(Logging));

            log.Prompt("Log test PROMPT");
            log.Fatal("Log test FATAL");
            log.Error("Log test ERROR");
            log.Warn("Log test WARN");
            log.Success("Log test SUCCESS");
            log.Info("Log test INFO");
            log.Debug("Log test DEBUG");
            log.Trace("Log test TRACE");
            log.Verbose("Log test VERBOSE");
            LoggedConsole.WriteFatalLine("Clean wrapper FATAL", new Exception("I'm a fake"));
            LoggedConsole.Log.Fatal("Clean log FATAL", new Exception("I'm a fake"));
            LoggedConsole.WriteErrorLine("Clean wrapper ERROR");
            LoggedConsole.Log.Error("Clean log ERROR");
            LoggedConsole.WriteWarnLine("Clean wrapper WARN");
            LoggedConsole.Log.Warn("Clean log WARN");
            LoggedConsole.WriteSuccessLine("Clean wrapper SUCCESS");
            LoggedConsole.Log.Success("Clean log SUCCESS");
            LoggedConsole.WriteLine("Clean wrapper INFO");
            LoggedConsole.Log.Info("Clean log INFO");
        }
Beispiel #8
0
        internal static void HangBeforeExit()
        {
#if DEBUG
            if (IsMsTestRunningMe)
            {
                return;
            }

            // if Michael is debugging with visual studio, this will prevent the window closing.
            Process parentProcess = ProcessExtensions.ParentProcessUtilities.GetParentProcess();
            if (Debugger.IsAttached)
            {
                if (parentProcess == null)
                {
                    LoggedConsole.WriteWarnLine("WARNING: Unable to detect parent process, this is a windows specific tool.");
                }
                else if (parentProcess?.ProcessName == "devenv")
                {
                    LoggedConsole.WriteSuccessLine("FINISHED: Press RETURN key to exit.");
                    Console.ReadLine();
                }
            }
#endif
        }
Beispiel #9
0
        /// <summary>
        /// This test checks two text/csv files to determine if they are the same.
        /// </summary>
        public static void FileEqualityTest(string testName, FileInfo testFile, FileInfo benchmarkFile)
        {
            LoggedConsole.WriteLine("# FILE EQUALITY TEST: Starting benchmark test for " + testName + ":");
            LoggedConsole.WriteLine("#          Comparing file <" + testFile.Name + "> with <" + benchmarkFile.Name + ">");

            if (!testFile.Exists)
            {
                LoggedConsole.WriteWarnLine("   " + testName + "  Test File <" + testFile.Name + "> does not exist.");
                return;
            }

            if (!benchmarkFile.Exists)
            {
                LoggedConsole.WriteWarnLine("   " + testName + ": the Benchmark File <" + benchmarkFile.Name + "> does not exist.");

                // check that the benchmark directory exists - if not create it.
                var benchmarkDir = benchmarkFile.Directory;
                if (!benchmarkDir.Exists)
                {
                    LoggedConsole.WriteWarnLine("    Creating Benchmark Directory");
                    benchmarkDir.Create();
                }

                LoggedConsole.WriteWarnLine("    Writing the Test File as a future Benchmark File");
                File.Copy(testFile.FullName, benchmarkFile.FullName, false);
                return;
            }

            var lines1 = FileTools.ReadTextFile(testFile.FullName);
            var lines2 = FileTools.ReadTextFile(benchmarkFile.FullName);

            if (lines1.Count == 0)
            {
                LoggedConsole.WriteWarnLine("   " + testName + "  File1 contains zero lines.");
                return;
            }

            if (lines2.Count == 0)
            {
                LoggedConsole.WriteWarnLine("   " + testName + "  File2 contains zero lines.");
                return;
            }

            if (lines1.Count != lines2.Count)
            {
                LoggedConsole.WriteWarnLine("   " + testName + "  The two files do not contain the same number of lines.");
                LoggedConsole.WriteWarnLine("   line count 1 <" + lines1.Count + ">  !=  line count 2 <" + lines2.Count + ">");
                return;
            }

            var allOk = true;

            for (int i = 0; i < lines2.Count; i++)
            {
                if (!lines1[i].Equals(lines2[i]))
                {
                    LoggedConsole.WriteWarnLine($"Line {i}: <{lines1[i]}>   !=   benchmark <{lines2[i]}>");
                    allOk = false;
                }
            }

            if (allOk)
            {
                LoggedConsole.WriteSuccessLine("#  SUCCESS! Passed the FILE EQUALITY TEST.");
            }
            else
            {
                LoggedConsole.WriteWarnLine("#  FAILED TEST! FILES ARE NOT THE SAME!");
            }

            LoggedConsole.WriteLine("#  Completed benchmark test.");
        }
Beispiel #10
0
        public static void TestMethod_GenerateSignal2()
        {
            int    sampleRate = 64000;
            double duration   = 30; // signal duration in seconds

            int[]  harmonics = { 500, 1000, 2000, 4000, 8000 };
            var    freqScale = new FrequencyScale(FreqScaleType.Linear125Octaves7Tones28Nyquist32000);
            string path      = @"C:\SensorNetworks\Output\Sonograms\UnitTestSonograms\SineSignal2.png";
            var    recording = GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);

            // init the default sonogram config
            var sonoConfig = new SonogramConfig
            {
                WindowSize              = freqScale.WindowSize,
                WindowOverlap           = 0.2,
                SourceFName             = "Signal2",
                NoiseReductionType      = NoiseReductionType.None,
                NoiseReductionParameter = 0.0,
            };
            var sonogram = new AmplitudeSonogram(sonoConfig, recording.WavReader);

            sonogram.Data = OctaveFreqScale.ConvertAmplitudeSpectrogramToDecibelOctaveScale(sonogram.Data, freqScale);

            // pick a row, any row
            var oneSpectrum = MatrixTools.GetRow(sonogram.Data, 40);

            oneSpectrum = DataTools.normalise(oneSpectrum);
            var peaks = DataTools.GetPeaks(oneSpectrum, 0.5);

            var peakIds = new List <int>();

            for (int i = 5; i < peaks.Length - 5; i++)
            {
                if (peaks[i])
                {
                    int peakId = freqScale.BinBounds[i, 0];
                    peakIds.Add(peakId);
                    LoggedConsole.WriteLine($"Spectral peak located in bin {peakId},  Herz={freqScale.BinBounds[i, 1]}");
                }
            }

            //if (peaks[129] && peaks[257] && peaks[513] && peaks[1025] && peaks[2049])
            if (peakIds[0] == 129 && peakIds[1] == 257 && peakIds[2] == 513 && peakIds[3] == 1025 && peakIds[4] == 2049)
            {
                LoggedConsole.WriteSuccessLine("Spectral Peaks found at correct places");
            }
            else
            {
                LoggedConsole.WriteErrorLine("Spectral Peaks found at INCORRECT places");
            }

            foreach (int h in harmonics)
            {
                LoggedConsole.WriteLine($"Harmonic {h}Hertz should be in bin {freqScale.GetBinIdForHerzValue(h)}");
            }

            // spectrogram without framing, annotation etc
            var    image = sonogram.GetImage();
            string title = $"Spectrogram of Harmonics: {DataTools.Array2String(harmonics)}   SR={sampleRate}  Window={freqScale.WindowSize}";

            image = sonogram.GetImageFullyAnnotated(image, title, freqScale.GridLineLocations);
            image.Save(path);
        }
        public static async Task <int> Execute(RibbonPlot.Arguments arguments)
        {
            if (arguments.InputDirectories.IsNullOrEmpty())
            {
                throw new CommandLineArgumentException(
                          $"{nameof(arguments.InputDirectories)} is null or empty - please provide at least one source directory");
            }

            var doNotExist = arguments.InputDirectories.Where(x => !x.Exists);

            if (doNotExist.Any())
            {
                throw new CommandLineArgumentException(
                          $"The following directories given to {nameof(arguments.InputDirectories)} do not exist: " + doNotExist.FormatList());
            }

            if (arguments.OutputDirectory == null)
            {
                arguments.OutputDirectory = arguments.InputDirectories.First();
                Log.Warn(
                    $"{nameof(arguments.OutputDirectory)} was not provided and was automatically set to source directory {arguments.OutputDirectory}");
            }

            if (arguments.Midnight == null || arguments.Midnight == TimeSpan.Zero)
            {
                // we need this to be width of day and not zero for rounding functions later on
                arguments.Midnight = RibbonPlotDomain;
                Log.Debug($"{nameof(arguments.Midnight)} was reset to {arguments.Midnight}");
            }

            if (arguments.Midnight < TimeSpan.Zero || arguments.Midnight > RibbonPlotDomain)
            {
                throw new InvalidStartOrEndException($"{nameof(arguments.Midnight)} cannot be less than `00:00` or greater than `{RibbonPlotDomain}`");
            }

            LoggedConsole.Write("Begin scanning directories");

            var allIndexFiles = arguments.InputDirectories.SelectMany(IndexGenerationData.FindAll);

            if (allIndexFiles.IsNullOrEmpty())
            {
                throw new MissingDataException($"Could not find `{IndexGenerationData.FileNameFragment}` files in:" + arguments.InputDirectories.FormatList());
            }

            Log.Debug("Checking files have dates");
            var indexGenerationDatas = allIndexFiles.Select(IndexGenerationData.Load);
            var datedIndices         = FileDateHelpers.FilterObjectsForDates(
                indexGenerationDatas,
                x => x.Source,
                y => y.RecordingStartDate,
                arguments.TimeSpanOffsetHint);

            LoggedConsole.WriteLine($"{datedIndices.Count} index generation data files were loaded");
            if (datedIndices.Count == 0)
            {
                throw new MissingDataException("No index generation files had dates, cannot proceed");
            }

            // now find the ribbon plots for these images - there are typically two color maps per index generation
            var datesMappedToColorMaps = new Dictionary <string, Dictionary <DateTimeOffset, FileInfo> >(2);

            foreach (var(date, indexData) in datedIndices)
            {
                Add(indexData.LongDurationSpectrogramConfig.ColorMap1);
                Add(indexData.LongDurationSpectrogramConfig.ColorMap2);

                void Add(string colorMap)
                {
                    if (!datesMappedToColorMaps.ContainsKey(colorMap))
                    {
                        datesMappedToColorMaps.Add(colorMap, new Dictionary <DateTimeOffset, FileInfo>(datedIndices.Count));
                    }

                    // try to find the associated ribbon
                    var searchPattern = "*" + colorMap + LdSpectrogramRibbons.SpectralRibbonTag + "*";

                    if (Log.IsVerboseEnabled())
                    {
                        Log.Verbose($"Searching `{indexData.Source?.Directory}` with pattern `{searchPattern}`.");
                    }

                    var ribbonFile = indexData.Source?.Directory?.EnumerateFiles(searchPattern).FirstOrDefault();

                    if (ribbonFile == null)
                    {
                        Log.Warn($"Did not find expected ribbon file for color map {colorMap} in directory `{indexData.Source?.Directory}`."
                                 + "This can happen if the ribbon is missing or if more than one file matches the color map.");
                    }

                    datesMappedToColorMaps[colorMap].Add(date, ribbonFile);
                }
            }

            // get the min and max dates and other things
            var stats = new RibbonPlotStats(datedIndices, arguments.Midnight.Value);

            Log.Debug($"Files found between {stats.Min:O} and {stats.Max:O}, rendering between {stats.Start:O} and {stats.End:O}, in {stats.Buckets} buckets");

            bool success = false;

            foreach (var(colorMap, ribbons) in datesMappedToColorMaps)
            {
                Log.Info($"Rendering ribbon plot for color map {colorMap}");
                if (ribbons.Count(x => x.Value.NotNull()) == 0)
                {
                    Log.Error($"There are no ribbon files found for color map {colorMap} - skipping this color map");
                    continue;
                }

                var image = CreateRibbonPlot(datedIndices, ribbons, stats);

                var midnight = arguments.Midnight == RibbonPlotDomain
                    ? string.Empty
                    : "Midnight=" + arguments.Midnight.Value.ToString("hhmm");
                var path = FilenameHelpers.AnalysisResultPath(
                    arguments.OutputDirectory,
                    arguments.OutputDirectory.Name,
                    "RibbonPlot",
                    "png",
                    colorMap,
                    midnight);

                using (var file = File.Create(path))
                {
                    image.SaveAsPng(file);
                }

                image.Dispose();

                success = true;
            }

            if (success == false)
            {
                throw new MissingDataException("Could not find any ribbon files for any of the color maps. No ribbon plots were produced.");
            }

            LoggedConsole.WriteSuccessLine("Completed");
            return(ExceptionLookup.Ok);
        }