Skip to content

Commit

Permalink
added random sampling without replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholghi committed Sep 11, 2018
1 parent c951ea4 commit ed2a488
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/AnalysisPrograms/MahnooshSandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Execute(Arguments arguments)
{
LoggedConsole.WriteLine("feature learning process");

var inputDir = @"D:\Mahnoosh\Liz\"; //@"M:\Postdoc\Liz\"; //@"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; //
var inputDir = @"D:\Mahnoosh\Liz\"; //@"C:\Users\kholghim\Mahnoosh\Liz\"; // @"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; // @"M:\Postdoc\Liz\"; //
var resultDir = Path.Combine(inputDir, "FeatureLearning");
var inputPath = Path.Combine(inputDir, "TrainSet"); //PatchSamplingSegments //PatchSampling
var trainSetPath = Path.Combine(inputDir, "TrainSet");
Expand Down Expand Up @@ -934,23 +934,23 @@ public class Arguments : SubCommandBase
{
public override Task<int> Execute(CommandLineApplication app)
{
//var instance = new MahnooshSandpit();
//instance.Execute(this);
var instance = new MahnooshSandpit();
instance.Execute(this);
//GenerateSpectrograms();
ExtractClusteringFeatures();
//ExtractClusteringFeatures();

return this.Ok();
}
}

public static void ExtractClusteringFeatures()
{
LoggedConsole.WriteLine("feature extraction process");
var inputDir = @"D:\Mahnoosh\Liz\"; //@"M:\Postdoc\Liz\"; //@"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; //
LoggedConsole.WriteLine("feature extraction process...");
var inputDir = @"D:\Mahnoosh\Liz\"; //@"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; //@"M:\Postdoc\Liz\"; //
var resultDir = Path.Combine(inputDir, "FeatureLearning");
var trainSetPath = Path.Combine(inputDir, "TrainSet");
//var trainSetPath = Path.Combine(inputDir, "TrainSet");
var testSetPath = Path.Combine(inputDir, "TestSet");
var configPath = @"D:\Mahnoosh\Liz\AnalysisConfigFiles\FeatureLearningConfig.yml"; // @"C:\Work\GitHub\audio-analysis\src\AnalysisConfigFiles\FeatureLearningConfig.yml"; //
var configPath = @"D:\Mahnoosh\Liz\AnalysisConfigFiles\FeatureLearningConfig.yml"; //@"C:\Work\GitHub\audio-analysis\src\AnalysisConfigFiles\FeatureLearningConfig.yml"; //
var centroidsPath = Path.Combine(resultDir, "ClusterCentroids0.csv");

var configFile = configPath.ToFileInfo();
Expand Down
17 changes: 17 additions & 0 deletions src/AudioAnalysisTools/DSP/FeatureLearning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,28 @@ public static class FeatureLearning

// Second: selecting random patches from each freq band matrix and add them to the corresponding patch list
int count = 0;

// file counter
//int no = 0;

while (count < allSubmatrices.Count)
{
randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
.GetPatches(allSubmatrices.ToArray()[count], patchWidth, patchHeight, numRandomPatches,
PatchSampling.SamplingMethod.Random).ToMatrix());
/*
// take the total number of frames out of each second minute paper
if (no / 2 == 0)
{
int rows = allSubmatrices.ToArray()[count].GetLength(0);
int columns = allSubmatrices.ToArray()[count].GetLength(1);
randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
.GetPatches(allSubmatrices.ToArray()[count], patchWidth, patchHeight, (rows / patchHeight) * (columns / patchWidth),
PatchSampling.SamplingMethod.Sequential).ToMatrix());
no++;
}
*/
count++;
}
}
Expand Down
29 changes: 23 additions & 6 deletions src/AudioAnalysisTools/DSP/PatchSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace AudioAnalysisTools.DSP
{
using System;
using System.Collections.Generic;
using System.Linq;
using Accord.Math;
using TowseyLibrary;

Expand Down Expand Up @@ -348,21 +349,37 @@ private static List<double[]> GetSequentialPatches(double[,] matrix, int patchWi
/// </summary>
private static List<double[]> GetRandomPatches(double[,] matrix, int patchWidth, int patchHeight, int numberOfPatches)
{
// Note: to make the method more flexible in terms of selecting a random patch with any height and width,
// first a random number generator is defined for both patchHeight and patchWidth.
// However, the possibility of selecting duplicates especially when selecting too many random numbers from
// a range (e.g., 1000 out of 1440) is high with a a random generator.
// Since, we are mostly interested in full-band patches, i.e., patchWidth = (maxFreqBin - minFreqBin + 1) / numFreqBand,
// it is important to select non-duplicate patchHeights. Hence, instead of a random generator for patchHeight,
// a better solution is to make a sequence of numbers to be selected, shuffle them, and
// finally, a first n (number of required patches) numbers could be selected.

int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);

int seed = 100;
Random randomNumber = new Random(seed);

// not sure whether it is better to use new Guid() instead of randomNumber.Next()
var randomRowNumbers = Enumerable.Range(0, rows - patchHeight).OrderBy(x => randomNumber.Next()).Take(numberOfPatches).ToList();
List<double[]> patches = new List<double[]>();

int rows = matrix.GetLength(0);
int columns = matrix.GetLength(1);
for (int i = 0; i < numberOfPatches; i++)
for (int i = 0; i < randomRowNumbers.Count; i++) //for (int i = 0; i < numberOfPatches; i++)
{
// selecting a random number from the height of the matrix
int rowRandomNumber = randomNumber.Next(0, rows - patchHeight);
//int rowRandomNumber = randomNumber.Next(0, rows - patchHeight);

// selecting a random number from the width of the matrix
int columnRandomNumber = randomNumber.Next(0, columns - patchWidth);
double[,] submatrix = MatrixTools.Submatrix(matrix, rowRandomNumber, columnRandomNumber,
rowRandomNumber + patchHeight - 1, columnRandomNumber + patchWidth - 1);

double[,] submatrix = MatrixTools.Submatrix(matrix, randomRowNumbers[i], columnRandomNumber,
randomRowNumbers[i] + patchHeight - 1, columnRandomNumber + patchWidth - 1);

//double[,] submatrix = MatrixTools.Submatrix(matrix, rowRandomNumber, columnRandomNumber, rowRandomNumber + patchHeight - 1, columnRandomNumber + patchWidth - 1);

// convert a matrix to a vector by concatenating columns and
// store it to the array of vectors
Expand Down

0 comments on commit ed2a488

Please sign in to comment.