FluidRobustScale: first copying of normalise's code

nix
Pierre Alexandre Tremblay 5 years ago
parent bbe715287e
commit b003b7a759

@ -0,0 +1,68 @@
FluidRobustScale : FluidRealTimeModel {
var <>min, <>max, <>invert;
*new {|server, min = 0, max = 1, invert = 0|
^super.new(server,[min,max,invert])
.min_(min).max_(max).invert_(invert);
}
prGetParams{
^[this.min,this.max,this.invert,-1,-1];
}
fitMsg{|dataSet|
^this.prMakeMsg(\fit,id,dataSet.id)
}
fit{|dataSet, action|
actions[\fit] = [nil,action];
this.prSendMsg(this.fitMsg(dataSet));
}
transformMsg{|sourceDataSet, destDataSet|
^this.prMakeMsg(\transform,id,sourceDataSet.id,destDataSet.id);
}
transform{|sourceDataSet, destDataSet, action|
actions[\transform] = [nil,action];
this.prSendMsg(this.transformMsg(sourceDataSet, destDataSet));
}
fitTransformMsg{|sourceDataSet, destDataSet|
^this.prMakeMsg(\fitTransform,id,sourceDataSet.id,destDataSet.id)
}
fitTransform{|sourceDataSet, destDataSet, action|
actions[\fitTransform] = [nil,action];
this.prSendMsg(this.fitTransformMsg(sourceDataSet, destDataSet));
}
transformPointMsg{|sourceBuffer, destBuffer|
^this.prMakeMsg(\transformPoint,id,
this.prEncodeBuffer(sourceBuffer),
this.prEncodeBuffer(destBuffer),
["/b_query",destBuffer.asUGenInput]
);
}
transformPoint{|sourceBuffer, destBuffer, action|
actions[\transformPoint] = [nil,{action.value(destBuffer)}];
this.prSendMsg(this.transformPointMsg(sourceBuffer, destBuffer));
}
kr{|trig, inputBuffer,outputBuffer,min,max,invert|
min = min ? this.min;
max = max ? this.max;
invert = invert ? this.invert;
this.min_(min).max_(max).invert_(invert);
^FluidProxyUgen.kr(this.class.name.asString++'/query', K2A.ar(trig),
id, this.min, this.max, this.invert, this.prEncodeBuffer(inputBuffer), this.prEncodeBuffer(outputBuffer));
}
}

@ -1,7 +1,7 @@
TITLE:: FluidNormalize
summary:: Normalize a FluidDataSet
categories:: FluidManipulation
related:: Classes/FluidStandardize, Classes/FluidDataSet
related:: Classes/FluidStandardize, Classes/FluidRobustScale, Classes/FluidDataSet
DESCRIPTION::
Normalize the entries of a link::Classes/FluidDataSet::, or normalize a data point according to the learned bounds of a data set. On the server.

@ -0,0 +1,157 @@
TITLE:: FluidRobustScale
summary:: Apply Robust Scaling to FluidDataSet
categories:: FluidManipulation
related:: Classes/FluidStandardize, Classes/FluidDataSet
DESCRIPTION::
Normalize the entries of a link::Classes/FluidDataSet::, or normalize a data point according to the learned bounds of a data set. On the server.
See http://www.faqs.org/faqs/ai-faq/neural-nets/part2/section-16.html
CLASSMETHODS::
private:: kr
METHOD:: new
Create a new instance
ARGUMENT:: server
The link::Classes/Server:: on which to run
ARGUMENT:: min
Minimum output value, default 0
ARGUMENT:: max
Maximum output value, default 1
ARGUMENT:: invert
The direction in which the normalization will occur for transform and transformpoint. The default 0 is taking in the range of the input used to fit and transforms it towards the normalised range. A value of 1 will expect an input of the normalized range to transform back to the original range.
INSTANCEMETHODS::
METHOD:: fit
Compute the normalization factors from a link::Classes/FluidDataSet:: for later.
ARGUMENT:: dataSet
The link::Classes/FluidDataSet:: to normalize
ARGUMENT:: action
A function to run when processing is complete
METHOD:: transform
Normalize a link::Classes/FluidDataSet:: into another link::Classes/FluidDataSet::, using the learned extrema from a previous call to link::Classes/FluidRobustScale#fit::
ARGUMENT:: sourceDataSet
The link::Classes/FluidDataSet:: to normalize
ARGUMENT:: destDataSet
The link::Classes/FluidDataSet:: to populate with normalized data
ARGUMENT:: action
A function to run when processing is complete
METHOD:: fitTransform
Normalize a link::Classes/FluidDataSet::
ARGUMENT:: sourceDataSet
The link::Classes/FluidDataSet:: to normalize
ARGUMENT:: destDataSet
The link::Classes/FluidDataSet:: to populate with normalized data
ARGUMENT:: action
A function to run when processing is complete
METHOD:: transformPoint
Normalize a new data point, using the learned extrema from a previous call to link::Classes/FluidRobustScale#fit::
ARGUMENT:: sourceBuffer
A link::Classes/Buffer:: with the new data point
ARGUMENT:: destBuffer
A link::Classes/Buffer:: to contain the normalized value
ARGUMENT:: action
A function to run when processing is complete
EXAMPLES::
code::
s.boot;
//Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers and a FluidRobustScale
// FluidRobustScale.dumpAllMethods
(
~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
~raw = FluidDataSet(s,\norm_help_raw);
~norm = FluidDataSet(s,\norm_help_normd);
~pitch_feature = Buffer.new(s);
~stats = Buffer.alloc(s, 7, 2);
~normalizer = FluidRobustScale(s);
)
// Load audio and run a pitch analysis, which gives us pitch and pitch confidence (so a 2D datum)
(
~audio = Buffer.read(s,~audiofile);
FluidBufPitch.process(s,~audio, features: ~pitch_feature);
)
// Divide the time series in to 10, 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(2, 1);
var count = PulseCount.kr(trig) - 1;
var chunkLen = (~pitch_feature.numFrames / 10).asInteger;
var stats = FluidBufStats.kr(
source: ~pitch_feature, startFrame: count * chunkLen,
numFrames: chunkLen, stats: ~stats, trig: (trig * (count <=9)), blocking:1
);
var rd = BufRd.kr(2, ~stats, DC.kr(0), 0, 1);// pick only mean pitch and confidence
var wr1 = BufWr.kr(rd[0], buf, DC.kr(0));
var wr2 = BufWr.kr(rd[1], buf, DC.kr(1));
var dsWr = FluidDataSetWr.kr(~raw, buf: buf, trig: Done.kr(stats));
LocalOut.kr( Done.kr(dsWr));
Poll.kr(trig,count,\count);
FreeSelf.kr(count - 9);
}.play;
)
// Normalize and load to language-side array
(
~rawarray = Array.new(10);
~normedarray= Array.new(10);
~normalizer.fitTransform(~raw,~norm, {
~raw.dump{|x| 10.do{|i|
~rawarray.add(x["data"][i.asString])
}};
~norm.dump{|x| 10.do{|i|
~normedarray.add(x["data"][i.asString])
}};
});
)
//Plot side by side. Before normalization the two dimensions have radically different scales
//which can be unhelpful in many cases
(
~rawarray.flatten(1).unlace.plot("Unnormalized",Rect(0,0,400,400),minval:0,maxval:[5000,1]).plotMode=\bars;
~plot2 = ~normedarray.flatten(1).unlace.plot("Normalized",Rect(410,0,400,400)).plotMode=\bars;
)
// single point transform on arbitrary value
~inbuf = Buffer.loadCollection(s,0.5.dup);
~outbuf = Buffer.new(s);
~normalizer.transformPoint(~inbuf,~outbuf,{|x|x.postln;x.getn(0,2,{|y|y.postln;};)});
OSCFunc.trace(false,true)
//Server side queries
(
{
var audio = BufRd.ar(1,~audio,LFSaw.ar(BufDur.ir(~audio).reciprocal).range(0, BufFrames.ir(~audio)));
var counter = Stepper.ar(Impulse.ar(ControlRate.ir),max:99);
var trig = A2K.kr(HPZ1.ar(counter) < 0);
//average 100 frames: one could use the MovingAverage extension here
var avg;
var inputPoint = LocalBuf(2);
var outputPoint = LocalBuf(2);
var avgBuf = LocalBuf(100,2);
//running average of pitch features
BufWr.kr(FluidPitch.kr(audio),avgBuf,phase:counter);
avg = Mix.new(BufRd.kr(2, avgBuf, phase:100.collect{|x|x})) * 0.01;
//assemble data point
BufWr.kr(avg[0],inputPoint,0);
BufWr.kr(avg[1],inputPoint,1);
~normalizer.kr(trig,inputPoint,outputPoint);
Poll.kr(trig,BufRd.kr(1,inputPoint,[0,1]),["pitch (raw)", "confidence (raw)"]);
Poll.kr(trig,BufRd.kr(1,outputPoint,[0,1]),["pitch (normalized)", "confidence (normalized)"])
}.play;
)
::

@ -1,7 +1,7 @@
TITLE:: FluidStandardize
summary:: Standardize a FluidDataSet
categories:: FluidManipulation
related:: Classes/FluidDataSet, Classes/FluidStandardize
related:: Classes/FluidDataSet, Classes/FluidStandardize, Classes/FluidRobustScale
DESCRIPTION::
Standardize a link::Classes/FluidDataSet::, i.e. rescale using its mean(s) and standard deviation(s) in each dimension.

@ -36,6 +36,8 @@ link::Classes/FluidNormalize::
link::Classes/FluidStandardize::
link::Classes/FluidRobustScale::
section:: Dimension Reduction
Compress data to fewer dimensions for visualisation / efficiency / preprocessing

@ -9,6 +9,7 @@
#include <clients/nrt/KNNClassifierClient.hpp>
#include <clients/nrt/KNNRegressorClient.hpp>
#include <clients/nrt/NormalizeClient.hpp>
#include <clients/nrt/RobustScaleClient.hpp>
#include <clients/nrt/StandardizeClient.hpp>
#include <clients/nrt/PCAClient.hpp>
#include <clients/nrt/MDSClient.hpp>
@ -33,9 +34,11 @@ PluginLoad(FluidSTFTUGen)
makeSCWrapper<RTKNNClassifierClient>("FluidKNNClassifier",ft);
makeSCWrapper<RTKNNRegressorClient>("FluidKNNRegressor",ft);
makeSCWrapper<RTNormalizeClient>("FluidNormalize",ft);
makeSCWrapper<RTRobustScaleClient>("FluidRobustScale",ft);
makeSCWrapper<RTStandardizeClient>("FluidStandardize",ft);
makeSCWrapper<RTPCAClient>("FluidPCA",ft);
makeSCWrapper<NRTThreadedMDSClient>("FluidMDS",ft); makeSCWrapper<NRTThreadedUMAPClient>("FluidUMAP",ft);
makeSCWrapper<NRTThreadedMDSClient>("FluidMDS",ft);
makeSCWrapper<NRTThreadedUMAPClient>("FluidUMAP",ft);
makeSCWrapper<RTAudioTransportClient>("FluidAudioTransport",ft);
makeSCWrapper<NRTThreadedAudioTransportClient>("FluidBufAudioTransp",ft);
makeSCWrapper<NRTThreadedDataSetWriter>("FluidDataSetWr", ft);

Loading…
Cancel
Save