You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.6 KiB
Plaintext
177 lines
4.6 KiB
Plaintext
TITLE:: FluidMDS
|
|
summary:: Dimensionality Reduction with Multidimensional Scaling
|
|
categories:: Libraries>FluidCorpusManipulation
|
|
related:: Classes/FluidPCA, Classes/FluidDataSet
|
|
|
|
DESCRIPTION::
|
|
|
|
Multidimensional scaling of a link::Classes/FluidDataSet::
|
|
|
|
https://scikit-learn.org/stable/modules/manifold.html#multi-dimensional-scaling-mds
|
|
|
|
|
|
CLASSMETHODS::
|
|
|
|
|
|
METHOD:: new
|
|
Make a new instance
|
|
ARGUMENT:: server
|
|
The server on which to run this model
|
|
ARGUMENT:: numDimensions
|
|
The number of dimensions to reduce to
|
|
ARGUMENT:: distanceMetric
|
|
The distance metric to use (integer, 0-6, see utility constants below)
|
|
|
|
METHOD:: euclidean
|
|
Euclidean distance (default)
|
|
|
|
METHOD:: sqeuclidean
|
|
Squared Euclidean distance
|
|
|
|
METHOD:: manhattan
|
|
Manhattan distance
|
|
|
|
METHOD:: max
|
|
Minkowski max
|
|
|
|
METHOD:: min
|
|
Minkowski max
|
|
|
|
METHOD:: kl
|
|
Symmetric Kulback Leiber divergance (only makes sense with non-negative data)
|
|
|
|
METHOD:: cosine
|
|
Cosine distance
|
|
|
|
INSTANCEMETHODS::
|
|
|
|
PRIVATE:: init
|
|
|
|
METHOD:: fitTransform
|
|
Fit the model to a link::Classes/FluidDataSet:: and write the new projected data to a destination FluidDataSet.
|
|
ARGUMENT:: sourceDataSet
|
|
Source data, or the DataSet name
|
|
ARGUMENT:: destDataSet
|
|
Destination data, or the DataSet name
|
|
ARGUMENT:: action
|
|
Run when done
|
|
|
|
EXAMPLES::
|
|
|
|
code::
|
|
|
|
//Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers, a FluidStandardize and a FluidMDS
|
|
(
|
|
~audiofile = File.realpath(FluidMDS.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
|
|
~raw = FluidDataSet(s);
|
|
~standardized = FluidDataSet(s);
|
|
~reduced = FluidDataSet(s);
|
|
~audio = Buffer.read(s,~audiofile);
|
|
~mfcc_feature = Buffer.new(s);
|
|
~stats = Buffer.alloc(s, 7, 12);
|
|
~standardizer = FluidStandardize(s);
|
|
~mds = FluidMDS(s);
|
|
)
|
|
|
|
// Load audio and run an mfcc analysis, which gives us 13 points (we'll throw the 0th away)
|
|
(
|
|
~audio = Buffer.read(s,~audiofile);
|
|
FluidBufMFCC.process(s,~audio, features: ~mfcc_feature);
|
|
)
|
|
|
|
// Divide the time series in 100, and take the mean of each segment and add this as a point to
|
|
// the 'raw' FluidDataSet
|
|
(
|
|
{
|
|
var trig = LocalIn.kr(1, 1);
|
|
var buf = LocalBuf(12, 1);
|
|
var count = PulseCount.kr(trig) - 1;
|
|
var chunkLen = (~mfcc_feature.numFrames / 100).asInteger;
|
|
var stats = FluidBufStats.kr(
|
|
source: ~mfcc_feature, startFrame: count * chunkLen,
|
|
startChan:1, numFrames: chunkLen, stats: ~stats, trig: trig, blocking:1
|
|
);
|
|
var rd = BufRd.kr(12, ~stats, DC.kr(0), 0, 1);
|
|
var bufWr, dsWr;
|
|
12.do{|i|
|
|
bufWr = BufWr.kr(rd[i], buf, DC.kr(i));
|
|
};
|
|
dsWr = FluidDataSetWr.kr(~raw, buf: buf, idNumber: count, trig: Done.kr(stats),blocking:1);
|
|
LocalOut.kr(Done.kr(dsWr));
|
|
FreeSelf.kr(count - 99);
|
|
Poll.kr(trig,(100-count));
|
|
}.play;
|
|
)
|
|
// wait for the count to reaches 0 in the post window.
|
|
|
|
//First standardize our DataSet, so that the MFCC dimensions are on comensurate scales
|
|
//Then apply the MDS in-place on the standardized data to get 2 dimensions, using a Euclidean distance metric
|
|
//Download the DataSet contents into an array for plotting
|
|
(
|
|
~reducedarray = Array.new(100);
|
|
~standardizer.fitTransform(~raw, ~standardized);
|
|
~mds.fitTransform(~standardized, ~reduced, action:{
|
|
~reduced.dump{|x| 100.do{|i|
|
|
~reducedarray.add(x["data"][i.asString])
|
|
}}});
|
|
)
|
|
|
|
//Visualise the 2D projection of our original 12D data
|
|
(
|
|
d = ~reducedarray.flop.deepCollect(1, { |x| x.normalize});
|
|
w = Window("scatter", Rect(128, 64, 200, 200));
|
|
w.drawFunc = {
|
|
Pen.use {
|
|
d[0].size.do{|i|
|
|
var x = (d[0][i]*200);
|
|
var y = (d[1][i]*200);
|
|
var r = Rect(x,y,5,5);
|
|
Pen.fillColor = Color.blue;
|
|
Pen.fillOval(r);
|
|
}
|
|
}
|
|
};
|
|
w.refresh;
|
|
w.front;
|
|
)
|
|
|
|
//we can change the distance computation
|
|
~mds.distanceMetric = FluidMDS.kl;
|
|
|
|
//recompute the reduction and recover the points
|
|
(
|
|
~reducedarray2 = Array.new(100);
|
|
~mds.fitTransform(~standardized, ~reduced, action:{
|
|
~reduced.dump{|x| 100.do{|i|
|
|
~reducedarray2.add(x["data"][i.asString])
|
|
}}});
|
|
)
|
|
|
|
//draw the new projection in red above the other
|
|
//Visualise the 2D projection of our original 12D data
|
|
(
|
|
d = ~reducedarray.flop.deepCollect(1, { |x| x.normalize});
|
|
e = ~reducedarray2.flop.deepCollect(1, { |x| x.normalize});
|
|
w.drawFunc = {
|
|
Pen.use {
|
|
d[0].size.do{|i|
|
|
var x = (d[0][i]*200);
|
|
var y = (d[1][i]*200);
|
|
var r = Rect(x,y,5,5);
|
|
Pen.fillColor = Color.blue;
|
|
Pen.fillOval(r);
|
|
};
|
|
e[0].size.do{|i|
|
|
var x = (e[0][i]*200);
|
|
var y = (e[1][i]*200);
|
|
var r = Rect(x,y,5,5);
|
|
Pen.fillColor = Color.red;
|
|
Pen.fillOval(r);
|
|
}
|
|
}
|
|
};
|
|
w.refresh;
|
|
w.front;
|
|
)
|
|
::
|