Пример #1
0
        public async Task WithChordsInFolder_GeneratesCsvTrainData()
        {
            const string inputDirectory = "./Resources/trainDataGeneratorFolder10Chords/";
            const string outputCsvFile  = inputDirectory + "trainDataGenerated.csv";

            try
            {
                Directory.CreateDirectory(inputDirectory);
                Assert.AreEqual(Directory.GetFiles(inputDirectory, "*.json").Length, 0);
                Assert.IsFalse(File.Exists(outputCsvFile));

                var respository = new FileSystemChordRepository(inputDirectory);

                var chords = new Chord[10];
                for (var i = 0; i < 10; i++)
                {
                    chords[i] = ChordTest.ChordExample();
                }

                foreach (Chord chord in chords)
                {
                    respository.SaveChord(chord);
                }

                Assert.AreEqual(Directory.GetFiles(inputDirectory, "*.json").Length, 10);
                Assert.IsFalse(File.Exists(outputCsvFile));

                var trainDataGenerator = new FileSystemTrainDataGenerator(inputDirectory, outputCsvFile);
                await trainDataGenerator.GenerateTrainData();

                Assert.AreEqual(Directory.GetFiles(inputDirectory, "*.json").Length, 10);
                Assert.IsTrue(File.Exists(outputCsvFile));

                string[] fileContent = File.ReadAllText(outputCsvFile).Split("\n");

                Assert.AreEqual(fileContent.Length, 12);

                var contentsRegistry = fileContent[4].Split(",");
                Assert.AreEqual(contentsRegistry.Length, 13);

                for (var i = 0; i < contentsRegistry.Length - 1; i++)
                {
                    var isDouble = double.TryParse(contentsRegistry[i], out double _);
                    Assert.IsTrue(isDouble);
                }

                Assert.AreEqual(contentsRegistry[12], chords[4].Name.ToLower());
                Assert.AreEqual(fileContent[0], "c,c#,d,d#,e,f,f#,g,g#,a,a#,b,chord");
            }
            finally
            {
                File.Delete(outputCsvFile);
                var filesToDelete = Directory.GetFiles(inputDirectory, "*.json");
                foreach (string fileToDelete in filesToDelete)
                {
                    File.Delete(fileToDelete);
                }
                Directory.Delete(inputDirectory);
            }
        }
Пример #2
0
        public void SaveChord_Correctly()
        {
            const string directory = "./Resources/storedTestChordsSaveChord/";

            try
            {
                Directory.CreateDirectory(directory);
                IChordRepository repository = new FileSystemChordRepository(directory);

                var chord = ChordTest.ChordExample();

                var existingJsonFilesCount = Directory
                                             .EnumerateFiles(directory, "*.json").Count();

                repository.SaveChord(chord);

                var jsonFiles =
                    Directory.EnumerateFiles(directory, "*.json").ToList();

                Assert.AreEqual(jsonFiles.Count(), existingJsonFilesCount + 1);

                var content = File.ReadAllText(jsonFiles.FirstOrDefault());

                Assert.AreEqual(content, JsonSerializer.Serialize(chord));
            } finally {
                var jsonFiles =
                    Directory.EnumerateFiles(directory, "*.json").ToList();

                File.Delete(jsonFiles.FirstOrDefault());
                Directory.Delete(directory);
            }
        }
Пример #3
0
        public void LoadChord_Correctly()
        {
            const string directory = "./Resources/storedTestChordsLoadChord/";

            try
            {
                Directory.CreateDirectory(directory);
                IChordRepository repository = new FileSystemChordRepository(directory);

                var chord = ChordTest.ChordExample();

                var existingJsonFilesCount = Directory
                                             .EnumerateFiles(directory, "*.json").Count();

                var chordId     = repository.SaveChord(chord);
                var loadedChord = repository.LoadChord(chordId);

                Assert.AreEqual(chord.Name, loadedChord.Name);
                Assert.AreEqual(chord.SampleRate, loadedChord.SampleRate);
                Assert.AreEqual(chord.Samples.Length, loadedChord.Samples.Length);
                Assert.AreEqual(chord.Pcp.Length, loadedChord.Pcp.Length);

                for (var i = 0; i < chord.Samples.Length; i++)
                {
                    Assert.AreEqual(chord.Samples[i], loadedChord.Samples[i]);
                }

                for (var i = 0; i < chord.Pcp.Length; i++)
                {
                    Assert.AreEqual(chord.Pcp[i], loadedChord.Pcp[i]);
                }
            }
            finally
            {
                var jsonFiles =
                    Directory.EnumerateFiles(directory, "*.json").ToList();

                foreach (var jsonFile in jsonFiles)
                {
                    File.Delete(jsonFile);
                }

                Directory.Delete(directory);
            }
        }
Пример #4
0
        public async Task CreateModelGivenInitialDataAndStoredChordsFolder()
        {
            const string trainDataFile    = "./Resources/trainData.csv";
            const string testDataFile     = "./Resources/testData.csv";
            const string inputDirectory   = "./Resources/trainDataGeneratorFolderTraining/";
            const uint   timeoutInSeconds = 1;
            const string outputDirectory  = "./Resources/generatedModels/";

            try
            {
                Directory.CreateDirectory(inputDirectory);
                Directory.CreateDirectory(outputDirectory);
                Assert.AreEqual(Directory.GetFiles(outputDirectory, "*.model").Length, 0);

                var respository = new FileSystemChordRepository(inputDirectory);

                var chords = new Chord[10];
                for (var i = 0; i < 10; i++)
                {
                    chords[i] = ChordTest.ChordExample();
                }

                foreach (Chord chord in chords)
                {
                    respository.SaveChord(chord);
                }

                var(experimentResult, predictionEngine) =
                    await AutoMlModelCreation.CreateModelGivenInitialDataAndStoredChordsFolder(
                        trainDataFile, testDataFile, inputDirectory, timeoutInSeconds, outputDirectory, new Progress <(int, string)>());

                Assert.IsNotNull(experimentResult);

                var metrics =
                    AutoMlModelCreation.EvaluateModel(experimentResult, "./Resources/testData.csv");

                Assert.IsTrue(metrics.LogLoss < 0.5);

                var prediction =
                    predictionEngine.Predict(AutoMlModelCreation.GetChordDataFromPcp(emPcp));

                Assert.IsTrue(prediction.ChordPrediction.ToLower().Equals("em"));

                var dPrediction =
                    predictionEngine.Predict(AutoMlModelCreation.GetChordDataFromPcp(dPcp));

                Assert.IsTrue(dPrediction.ChordPrediction.ToLower().Equals("d"));

                var isModelPresent = Directory.GetFiles(outputDirectory, "*.model").Length == 1;
                Assert.IsTrue(isModelPresent);
            } finally
            {
                File.Delete(Path.Combine(inputDirectory, "trainData.csv"));
                var filesToDelete = Directory.GetFiles(inputDirectory, "*.json");
                foreach (string fileToDelete in filesToDelete)
                {
                    File.Delete(fileToDelete);
                }
                Directory.Delete(inputDirectory);

                var modelsToDelete = Directory.GetFiles(outputDirectory, "*.model");
                foreach (string modelToDelete in modelsToDelete)
                {
                    File.Delete(modelToDelete);
                }
                Directory.Delete(outputDirectory);
            }
        }