Esempio n. 1
0
        // [Ignore("The update from 864f7a491e2ea0e938161bd390c1c931ecbdf63c possibly broke this test and I do not know how to repair it.
        // TODO @towsey")]
        public void TwoOscillationTests()
        {
            {
                var sourceRecording = PathHelper.ResolveAsset("Recordings", "BAC2_20071008-085040.wav");

                // 1. get the config dictionary
                var configDict = Oscillations2014.GetDefaultConfigDictionary(sourceRecording);

                // 2. Create temp directory to store output
                if (!this.outputDirectory.Exists)
                {
                    this.outputDirectory.Create();
                }

                // 3. Generate the FREQUENCY x OSCILLATIONS Graphs and csv data
                var tuple = Oscillations2014.GenerateOscillationDataAndImages(sourceRecording, configDict, true);

                // construct name of expected image file to save
                var sourceName = Path.GetFileNameWithoutExtension(sourceRecording.Name);
                var stem       = sourceName + ".FreqOscilSpectrogram";

                // construct name of expected matrix osc spectrogram to save file
                var expectedMatrixFile = PathHelper.ResolveAsset("Oscillations2014", stem + ".Matrix.EXPECTED.csv");

                // SAVE THE OUTPUT if true
                // WARNING: this will overwrite fixtures
                if (false)
                {
                    // 1: save image of oscillation spectrogram
                    string imageName = stem + ".EXPECTED.png";
                    string imagePath = Path.Combine(PathHelper.ResolveAssetPath("Oscillations2014"), imageName);
                    tuple.Item1.Save(imagePath, ImageFormat.Png);

                    // 2: Save matrix of oscillation data stored in freqOscilMatrix1
                    //Csv.WriteMatrixToCsv(expectedMatrixFile, tuple.Item2);
                    Binary.Serialize(expectedMatrixFile, tuple.Item2);
                }

                // Run two tests. Have to deserialise the expected data files
                // 1: Compare image files - check that image dimensions are correct
                Assert.AreEqual(350, tuple.Item1.Width);
                Assert.AreEqual(675, tuple.Item1.Height);

                // 2. Compare matrix data
                var expectedMatrix = Binary.Deserialize <double[, ]>(expectedMatrixFile);

                //TODO  Following test fails when using CSV reader because the reader cuts out first line of the matrix
                //var expectedMatrix = Csv.ReadMatrixFromCsv<double>(expectedMatrixFile);
                CollectionAssert.That.AreEqual(expectedMatrix, tuple.Item2, 0.000001);
            }
        }
        public void TwoOscillationTests()
        {
            {
                var sourceRecording = PathHelper.ResolveAsset("Recordings", "BAC2_20071008-085040.wav");
                var configFile      = PathHelper.ResolveAsset("Oscillations2014", "Towsey.Sonogram.yml");

                // 1. get the config dictionary
                var configDict = Oscillations2014.GetConfigDictionary(configFile, true);
                configDict[ConfigKeys.Recording.Key_RecordingCallName] = sourceRecording.FullName;
                configDict[ConfigKeys.Recording.Key_RecordingFileName] = sourceRecording.Name;

                // 2. Create temp directory to store output
                if (!this.outputDirectory.Exists)
                {
                    this.outputDirectory.Create();
                }

                // 3. Generate the FREQUENCY x OSCILLATIONS Graphs and csv data
                var tuple = Oscillations2014.GenerateOscillationDataAndImages(sourceRecording, configDict, true);

                // Calculate the sample length i.e. number of frames spanned to calculate oscillations per second
                int sampleLength = Oscillations2014.DefaultSampleLength;
                if (configDict.ContainsKey(AnalysisKeys.OscilDetection2014SampleLength))
                {
                    sampleLength = int.Parse(configDict[AnalysisKeys.OscilDetection2014SampleLength]);
                }

                // construct name of expected image file to save
                var sourceName = Path.GetFileNameWithoutExtension(sourceRecording.Name);
                var stem       = sourceName + ".FreqOscilSpectrogram_" + sampleLength;

                // construct name of expected matrix osc spectrogram to save file
                var expectedMatrixFile = PathHelper.ResolveAsset("Oscillations2014", stem + ".Matrix.EXPECTED.bin");

                // construct name of expected matrix osc spectrogram to save file
                var expectedSpectrumFile = PathHelper.ResolveAsset("Oscillations2014", stem + ".Vector.EXPECTED.bin");

                // Run this once to generate expected image and data files (############ IMPORTANT: remember to move saved files OUT of bin/Debug directory!)
                // SAVE THE OUTPUT if true
                if (false)
                {
                    // 1: save image of oscillation spectrogram
                    string imageName = stem + ".EXPECTED.png";
                    string imagePath = Path.Combine(this.outputDirectory.FullName, imageName);
                    tuple.Item1.Save(imagePath, ImageFormat.Png);

                    // 2: Save matrix of oscillation data stored in freqOscilMatrix1
                    // Acoustics.Shared.Csv.Csv.WriteMatrixToCsv(expectedMatrixFile.FullName, tuple.Item2);
                    Binary.Serialize(expectedMatrixFile, tuple.Item2);

                    // 3: save oscillationsSpectrum OR the OSC spectral index.
                    // Acoustics.Shared.Csv.Csv.WriteToCsv(spectralFile, tuple.Item3);
                    // Json.Serialise(expectedSpectrumFile, tuple.Item3);
                    Binary.Serialize(expectedSpectrumFile, tuple.Item3);
                }

                // Run three tests. Have to deserialise the expected data files
                // 1: Compare image files - check that image dimensions are correct
                Assert.AreEqual(356, tuple.Item1.Width);
                Assert.AreEqual(678, tuple.Item1.Height);

                // 2. Compare matrix data
                var expectedMatrix = Binary.Deserialize <double[, ]>(expectedMatrixFile);
                CollectionAssert.AreEqual(expectedMatrix, tuple.Item2);

                // 3. Compare OSC spectral index
                // var expectedVector = Json.Deserialise<double[]>(expectedSpectrumFile);
                var expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
                CollectionAssert.AreEqual(expectedVector, tuple.Item3);
            }
        }